SlideShare a Scribd company logo
5
Most read
6
Most read
10
Most read
Multiple inheritance
In Java
• What is inheritance?
• Example of inheritance
• What is meant by multiple inheritance?
• Example of multiple inheritance?
• What is diamond problem?
• What is actual problem?
• Does java multiple inheritance possible with
interfaces?
• Multiple inheritance possible and its example is…
What is inheritance?
• An object which acquires the properties of
another object is called inheritance
• Properties means code & data/ method &
fields
Simple example of inheritance
class First
{
int i=10;
void inherit()
{
System.out.println("Hello");
}
}
class Second extends First
{
void inherit()
{
System.out.println("World");
}
public static void main(String a[])
{
Second s=new Second();
s.inherit();
System.out.println(s.i);
}
}
C:Users>javac First.java
C:Users>java Second
World
10
What is meant by multiple
inheritance?
• An object which acquires properties from two
are more objects at a time.
Class C
Class B
Class A
Example of multiple inheritance?
class First
{
int i=10;
void inherit()
{
System.out.println("Hello");
}
}
class Second extends First
{
void inherit()
{
System.out.println("World");
}
}
class Third extends First,Second
{
void inherit()
{
System.out.println("Does this exists in java?");
}
public static void main(String a[])
{
Second s=new Second();
s.inherit();
System.out.println(s.i);
}
}
C:Users>javac First.java
First.java:16: error: '{' expected
class Third extends First,Second
^
1 error
What is Diamond Problem?
Class A
Class B Class C
Class D
Example of multiple inheritance?
class First
{
int i=10;
void inherit()
{
System.out.println("Hello");
}
}
class Second extends First
{
void inherit()
{
System.out.println("World");
}
}
class Third extends First,Second
{
void inherit()
{
System.out.println("Does this exists in java?");
}
public static void main(String a[])
{
Second s=new Second();
s.inherit();
System.out.println(s.i);
}
}
C:Users>javac First.java
First.java:16: error: '{' expected
class Third extends First,Second
^
1 error
What is actual problem?
• James gosling in February 1995 gives an idea on
why multiple inheritance is not supported in java
• JAVA omits many rarely used, poorly understood,
confusing features of C++ that in our experience
bring more grief than benefit. This primarily
consists of operator overloading (although it does
have method overloading), multiple inheritance,
and extensive automatic coercions
Multiple inheritance using interfaces
interface inter
{
void dis();
}
interface inter1
{
void dis1();
}
class myclass implements inter, inter1
{
public void dis()
{
System.out.println("First interface method");
}
public void dis1()
{
System.out.println("Second interface method");
}
public static void main(String a[])
{
myclass m=new myclass();
m.dis();
m.dis1();
}
}
Does this is the multiple inheritance or
not?
Yes, it is not an actual multiple
inheritance but is just a syntactical
multiple inheritance. Because the
behaviors cannot be defined inside the
methods of the interfaces.
Conclusion
• As per the above, multiple inheritance through
classes in java is not possible and can be
possible through interfaces but is not a full
multiple inheritance.
Pure Multiple Inheritance is possible in java
HOW
• From the version 8 of java, It supports default
methods.
• Default methods are the normal methods
those can be implemented inside the interface
• Ex:
public interface Special
{
default void normalMethod()
{
System.out.println(“I am normal method");
}
}
A class that inherits the behaviors‘ from two parents called multiple
inheritance.
interface First
{
default void firstMethod(){
System.out.println(“I am the first default method in first interface");
}
}
interface Second
{
default void secondMethod(){
System.out.println(“I am the second default method in second
interface");
}
}
public class Multiple implements First, Second
{
public static void main(String[] args)
{
Multiple Obj = new Multiple();
Obj.moveFast();
Obj.crawl();
}
}
What if incase of diamond problem?
Interface First
{
default void same(){
System.out.println(“Same method of first interface");
}
}
interface Second
{
default void same(){
System.out.println(“Same method of second interface");
}
}
public class Third implements First, Second
{
public static void main(String[] args)
{
Third obj=new Third();
First.super.run(); //Call First’s same() method
//or
First.super.run(); //Call Second’s same() method
}
}

More Related Content

PPTX
Access specifiers(modifiers) in java
HrithikShinde
 
PDF
Java threads
Prabhakaran V M
 
PPTX
Java Program Structure
Manish Tiwari
 
PDF
Wrapper classes
Ravi_Kant_Sahu
 
PPT
9. Input Output in java
Nilesh Dalvi
 
PPTX
Data types in java
HarshitaAshwani
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PPTX
Super keyword in java
Hitesh Kumar
 
Access specifiers(modifiers) in java
HrithikShinde
 
Java threads
Prabhakaran V M
 
Java Program Structure
Manish Tiwari
 
Wrapper classes
Ravi_Kant_Sahu
 
9. Input Output in java
Nilesh Dalvi
 
Data types in java
HarshitaAshwani
 
Super keyword in java
Hitesh Kumar
 

What's hot (20)

PDF
Arrays in Java
Naz Abdalla
 
PPTX
Inheritance
Sapna Sharma
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPT
Method overriding
Azaz Maverick
 
PPTX
Chapter 07 inheritance
Praveen M Jigajinni
 
PPT
Thread model in java
AmbigaMurugesan
 
PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Inheritance In Java
Darpan Chelani
 
PPTX
java interface and packages
VINOTH R
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPTX
This keyword in java
Hitesh Kumar
 
PPT
Inheritance C#
Raghuveer Guthikonda
 
PPT
Abstract class in java
Lovely Professional University
 
PPT
Final keyword in java
Lovely Professional University
 
PPTX
Inheritance in java
RahulAnanda1
 
PPTX
Access modifier and inheritance
Dikshyanta Dhungana
 
PPTX
Interface in java
PhD Research Scholar
 
PPTX
Type casting in java
Farooq Baloch
 
PDF
Generics
Ravi_Kant_Sahu
 
PPT
Java And Multithreading
Shraddha
 
Arrays in Java
Naz Abdalla
 
Inheritance
Sapna Sharma
 
Java I/o streams
Hamid Ghorbani
 
Method overriding
Azaz Maverick
 
Chapter 07 inheritance
Praveen M Jigajinni
 
Thread model in java
AmbigaMurugesan
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance In Java
Darpan Chelani
 
java interface and packages
VINOTH R
 
Methods in Java
Jussi Pohjolainen
 
This keyword in java
Hitesh Kumar
 
Inheritance C#
Raghuveer Guthikonda
 
Abstract class in java
Lovely Professional University
 
Final keyword in java
Lovely Professional University
 
Inheritance in java
RahulAnanda1
 
Access modifier and inheritance
Dikshyanta Dhungana
 
Interface in java
PhD Research Scholar
 
Type casting in java
Farooq Baloch
 
Generics
Ravi_Kant_Sahu
 
Java And Multithreading
Shraddha
 
Ad

Viewers also liked (20)

PDF
Sahi-Web Automation and Testing Tool
Kurapati Vishwak
 
PDF
itft-Inheritance in java
Atul Sehdev
 
PDF
Multiple Inheritance
adil raja
 
PPT
04inherit
Waheed Warraich
 
PPTX
Inheritance question class 12th
AAKASH KUMAR
 
PPTX
Mendelian inheretence part 1
mohamed abdukadir
 
PPTX
Notes incomplete codominance
stewart_j
 
PDF
QUALITATIVE AND QUANTITATIVE CHARACTERS OF THREE COMMON CARP (Cyprinus carpio...
Repository Ipb
 
PPT
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
PDF
Outbound Training
pavansriram
 
PPTX
Extension of mendelian inheritance
Omar Jacalne
 
PPTX
Mendel concept of genetics
varsha jain
 
PDF
Inheritance and Method Overriding
CC Nakhon Pathom Rajabhat University
 
PDF
OOP Inheritance
Anastasia Jakubow
 
PPTX
Cell structure and functions
Joy Batang Ü
 
PPT
Medels laws
Amy Allen
 
PPT
Pedigree analysis
sikojp
 
PPT
Heredity
Ravi Kumar
 
Sahi-Web Automation and Testing Tool
Kurapati Vishwak
 
itft-Inheritance in java
Atul Sehdev
 
Multiple Inheritance
adil raja
 
04inherit
Waheed Warraich
 
Inheritance question class 12th
AAKASH KUMAR
 
Mendelian inheretence part 1
mohamed abdukadir
 
Notes incomplete codominance
stewart_j
 
QUALITATIVE AND QUANTITATIVE CHARACTERS OF THREE COMMON CARP (Cyprinus carpio...
Repository Ipb
 
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
Outbound Training
pavansriram
 
Extension of mendelian inheritance
Omar Jacalne
 
Mendel concept of genetics
varsha jain
 
Inheritance and Method Overriding
CC Nakhon Pathom Rajabhat University
 
OOP Inheritance
Anastasia Jakubow
 
Cell structure and functions
Joy Batang Ü
 
Medels laws
Amy Allen
 
Pedigree analysis
sikojp
 
Heredity
Ravi Kumar
 
Ad

Similar to Multiple inheritance possible in Java (20)

DOC
How would you implement multiple inheritance in java
Tyagi2636
 
PPTX
INTERFACES. with machine learning and data
dineshkesav07
 
PPT
Multiple interfaces 9 cm604.31
myrajendra
 
PPTX
it is the quick gest about the interfaces in java
arunkumarg271
 
PPTX
ITTutor Advanced Java (1).pptx
kristinatemen
 
PPT
Java OOP s concepts and buzzwords
Raja Sekhar
 
PPTX
Module--fundamentals and operators in java1.pptx
Radhika Venkatesh
 
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
PDF
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Raffi Khatchadourian
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PPT
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
PPTX
inheritance, Packages and Interfaces.pptx
Vanitha Alagesan
 
PPT
L7 inheritance
teach4uin
 
PPT
L7 inheritance
teach4uin
 
PDF
core java material.pdf
Rasa72
 
PPTX
Unit3 packages & interfaces
Kalai Selvi
 
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
PPT
Java interfaces
Raja Sekhar
 
PPTX
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
How would you implement multiple inheritance in java
Tyagi2636
 
INTERFACES. with machine learning and data
dineshkesav07
 
Multiple interfaces 9 cm604.31
myrajendra
 
it is the quick gest about the interfaces in java
arunkumarg271
 
ITTutor Advanced Java (1).pptx
kristinatemen
 
Java OOP s concepts and buzzwords
Raja Sekhar
 
Module--fundamentals and operators in java1.pptx
Radhika Venkatesh
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Raffi Khatchadourian
 
oops concept in java | object oriented programming in java
CPD INDIA
 
web program-Inheritance,pack&except in Java.ppt
mcjaya2024
 
inheritance, Packages and Interfaces.pptx
Vanitha Alagesan
 
L7 inheritance
teach4uin
 
L7 inheritance
teach4uin
 
core java material.pdf
Rasa72
 
Unit3 packages & interfaces
Kalai Selvi
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
ssuser7f90ae
 
Java interfaces
Raja Sekhar
 
inheritance and interface in oops with java .pptx
janetvidyaanancys
 

Recently uploaded (20)

PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
PDF
This slide provides an overview Technology
mineshkharadi333
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
ChatGPT's Deck on The Enduring Legacy of Fax Machines
Greg Swan
 
This slide provides an overview Technology
mineshkharadi333
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Beyond Automation: The Role of IoT Sensor Integration in Next-Gen Industries
Rejig Digital
 
Doc9.....................................
SofiaCollazos
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
OA presentation.pptx OA presentation.pptx
pateldhruv002338
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 

Multiple inheritance possible in Java

  • 2. • What is inheritance? • Example of inheritance • What is meant by multiple inheritance? • Example of multiple inheritance? • What is diamond problem? • What is actual problem? • Does java multiple inheritance possible with interfaces? • Multiple inheritance possible and its example is…
  • 3. What is inheritance? • An object which acquires the properties of another object is called inheritance • Properties means code & data/ method & fields
  • 4. Simple example of inheritance class First { int i=10; void inherit() { System.out.println("Hello"); } } class Second extends First { void inherit() { System.out.println("World"); } public static void main(String a[]) { Second s=new Second(); s.inherit(); System.out.println(s.i); } } C:Users>javac First.java C:Users>java Second World 10
  • 5. What is meant by multiple inheritance? • An object which acquires properties from two are more objects at a time. Class C Class B Class A
  • 6. Example of multiple inheritance? class First { int i=10; void inherit() { System.out.println("Hello"); } } class Second extends First { void inherit() { System.out.println("World"); } } class Third extends First,Second { void inherit() { System.out.println("Does this exists in java?"); } public static void main(String a[]) { Second s=new Second(); s.inherit(); System.out.println(s.i); } } C:Users>javac First.java First.java:16: error: '{' expected class Third extends First,Second ^ 1 error
  • 7. What is Diamond Problem? Class A Class B Class C Class D
  • 8. Example of multiple inheritance? class First { int i=10; void inherit() { System.out.println("Hello"); } } class Second extends First { void inherit() { System.out.println("World"); } } class Third extends First,Second { void inherit() { System.out.println("Does this exists in java?"); } public static void main(String a[]) { Second s=new Second(); s.inherit(); System.out.println(s.i); } } C:Users>javac First.java First.java:16: error: '{' expected class Third extends First,Second ^ 1 error
  • 9. What is actual problem? • James gosling in February 1995 gives an idea on why multiple inheritance is not supported in java • JAVA omits many rarely used, poorly understood, confusing features of C++ that in our experience bring more grief than benefit. This primarily consists of operator overloading (although it does have method overloading), multiple inheritance, and extensive automatic coercions
  • 10. Multiple inheritance using interfaces interface inter { void dis(); } interface inter1 { void dis1(); } class myclass implements inter, inter1 { public void dis() { System.out.println("First interface method"); } public void dis1() { System.out.println("Second interface method"); } public static void main(String a[]) { myclass m=new myclass(); m.dis(); m.dis1(); } } Does this is the multiple inheritance or not? Yes, it is not an actual multiple inheritance but is just a syntactical multiple inheritance. Because the behaviors cannot be defined inside the methods of the interfaces.
  • 11. Conclusion • As per the above, multiple inheritance through classes in java is not possible and can be possible through interfaces but is not a full multiple inheritance. Pure Multiple Inheritance is possible in java HOW
  • 12. • From the version 8 of java, It supports default methods. • Default methods are the normal methods those can be implemented inside the interface • Ex: public interface Special { default void normalMethod() { System.out.println(“I am normal method"); } }
  • 13. A class that inherits the behaviors‘ from two parents called multiple inheritance. interface First { default void firstMethod(){ System.out.println(“I am the first default method in first interface"); } } interface Second { default void secondMethod(){ System.out.println(“I am the second default method in second interface"); } } public class Multiple implements First, Second { public static void main(String[] args) { Multiple Obj = new Multiple(); Obj.moveFast(); Obj.crawl(); } }
  • 14. What if incase of diamond problem? Interface First { default void same(){ System.out.println(“Same method of first interface"); } } interface Second { default void same(){ System.out.println(“Same method of second interface"); } } public class Third implements First, Second { public static void main(String[] args) { Third obj=new Third(); First.super.run(); //Call First’s same() method //or First.super.run(); //Call Second’s same() method } }