5
Most read
6
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
}
}

Multiple inheritance possible in Java

  • 1.
  • 2.
    • What isinheritance? • 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 ofinheritance 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 meantby multiple inheritance? • An object which acquires properties from two are more objects at a time. Class C Class B Class A
  • 6.
    Example of multipleinheritance? 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 DiamondProblem? Class A Class B Class C Class D
  • 8.
    Example of multipleinheritance? 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 actualproblem? • 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 usinginterfaces 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 perthe 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 theversion 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 thatinherits 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 incaseof 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 } }