SlideShare a Scribd company logo
6
Most read
10
Most read
14
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

What's hot (20)

PDF
Arrays in Java
Naz Abdalla
 
PPTX
Inheritance in c++
Vineeta Garg
 
PPTX
Interface in java
PhD Research Scholar
 
PPT
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
PPT
C++ classes tutorials
Mayank Jain
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPTX
Templates in C++
Tech_MX
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPTX
Methods in java
chauhankapil
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
This keyword in java
Hitesh Kumar
 
PPTX
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
PPTX
Packages in java
Elizabeth alexander
 
PPT
Generics in java
suraj pandey
 
PDF
Java Thread Synchronization
Benj Del Mundo
 
PPTX
Inheritance in Java
Tamanna Akter
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Inheritance in java
Tech_MX
 
PPTX
Arrays in Java
Abhilash Nair
 
Arrays in Java
Naz Abdalla
 
Inheritance in c++
Vineeta Garg
 
Interface in java
PhD Research Scholar
 
Interface in java By Dheeraj Kumar Singh
dheeraj_cse
 
C++ classes tutorials
Mayank Jain
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Templates in C++
Tech_MX
 
Classes, objects in JAVA
Abhilash Nair
 
Methods in java
chauhankapil
 
Java abstract class & abstract methods
Shubham Dwivedi
 
This keyword in java
Hitesh Kumar
 
Abstract Class & Abstract Method in Core Java
MOHIT AGARWAL
 
Packages in java
Elizabeth alexander
 
Generics in java
suraj pandey
 
Java Thread Synchronization
Benj Del Mundo
 
Inheritance in Java
Tamanna Akter
 
Constructor in java
Pavith Gunasekara
 
Inheritance in java
Tech_MX
 
Arrays in Java
Abhilash Nair
 

Viewers also liked (20)

PPTX
Inheritance in JAVA PPT
Pooja Jaiswal
 
PPTX
Inheritance
Sapna Sharma
 
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
 
Inheritance in JAVA PPT
Pooja Jaiswal
 
Inheritance
Sapna Sharma
 
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
 
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
 
PPTX
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
PPTX
Inheritance & interface ppt Inheritance
narikamalliy
 
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
 
inheritance and interface in oops with java .pptx
janetvidyaanancys
 
Inheritance & interface ppt Inheritance
narikamalliy
 
Ad

Recently uploaded (20)

PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Designing_the_Future_AI_Driven_Product_Experiences_Across_Devices.pptx
presentifyai
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 

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 } }