You are on page 1of 8

9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

Related Articles

Java and Multiple Inheritance


Difficulty Level :
Medium ● Last Updated :
09 May, 2020

Multiple Inheritance is a feature of object oriented concept, where a class can inherit

proper ties of more than one parent class. The problem occurs when there exist

methods with same signature in both the super classes and subclass. On calling the

method, the compiler cannot determine which class method to be called and even on

calling which class method gets the priority.

Why Java doesn’t suppor t Multiple Inheritance?

Consider the below Java code. It shows error.

// First Parent class


class Parent1
{
    void fun()
    {
        System.out.println("Parent1");
    }
}
  
// Second Parent Class
class Parent2
{
    void fun()
    {
        System.out.println("Parent2");
    }
}
  
// Error : Test is inheriting from multiple
// classes
class Test extends Parent1, Parent2
{
   public static void main(String args[])
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
   {
you have read and understood our
Cookie Policy &
Privacy Policy
       Test t = new Test();
       t.fun(); Got It !
   }
https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 1/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

Output :

Compiler Error

From the code, we see that, on calling the method fun() using Test object will cause

complications such as whether to call Parent1’s fun() or Parent2’s fun() method.

1. The Diamond Problem:

The below Java program throws compiler error when run. Multiple inheritance causes

diamond problem when allowed in other languages like C++.

Ad

Apache Spark Training - Live Instructor-Led Classes

GrandParent
/ \

/ \

Parent1 Parent2

\ /

\ /

Test

// A Grand parent class in diamond


class GrandParent
{
    void fun()
    {
        System.out.println("Grandparent");
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
    } you have read and understood our
Cookie Policy &
Privacy Policy
}
Got It !
  

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 2/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

// First Parent class


class Parent1 extends GrandParent
{
    void fun()
    {
        System.out.println("Parent1");
    }
}
  
// Second Parent Class
class Parent2 extends GrandParent
{
    void fun()
    {
        System.out.println("Parent2");
    }
}
  
  
// Error : Test is inheriting from multiple
// classes
class Test extends Parent1, Parent2
{
   public static void main(String args[])
   {
       Test t = new Test();
       t.fun();
   }
}

Output :

Error :

prog.java:31: error: '{' expected

class Test extends Parent1, Parent2

1 error

From the code, we see that: On calling the method fun() using Test object will cause

complications such as whether to call Parent1’s fun() or Child’s fun() method.

Therefore, in order to avoid such complications Java does not suppor t multiple

inheritance of classes.

2. Simplicity – Multiple inheritance is not suppor ted by Java using classes , handling

the complexity that causes due to multiple inheritance is ver y complex. It creates

We use cookies
problem to ensure
during you haveoperations
various the best browsing experience
like casting,on our website. By using
constructor our site, you
acknowledge
chaining that
etc and the above

you have read and understood our


Cookie Policy &
Privacy Policy
all reason is that there are ver y few scenarios on which we actually need multiple

Got It !
inheritance, so better to omit it for keeping the things simple and straightfor ward.

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 3/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

How are above problems handled for Default Methods and Inter faces ?

Java 8 suppor ts default methods where inter faces can provide default implementation

of methods. And a class can implement two or more inter faces. In case both the

implemented inter faces contain default methods with same method signature, the

implementing class should explicitly specif y which default method is to be used or it

should override the default method.

// A simple Java program to demonstrate multiple


// inheritance through default methods.
interface PI1
{
    // default method
    default void show()
    {
        System.out.println("Default PI1");
    }
}
  
interface PI2
{
    // Default method
    default void show()
    {
        System.out.println("Default PI2");
    }
}
  
// Implementation class code
class TestClass implements PI1, PI2
{
    // Overriding default show method
    public void show()
    {
        // use super keyword to call the show
        // method of PI1 interface
        PI1.super.show();
  
        // use super keyword to call the show
        // method of PI2 interface
        PI2.super.show();
    }
  
    public static void main(String args[])
    {
        TestClass d = new TestClass();
        d.show();
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
    } you have read and understood our
Cookie Policy &
Privacy Policy
}
Got It !

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 4/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

Output:

Default PI1

Default PI2

If we remove implementation of default method from “ TestClass”, we get compiler

error. See this for a sample run.

If there is a diamond through inter faces, then there is no issue if none of the middle

inter faces provide implementation of root inter face. If they provide implementation,

then implementation can be accessed as above using super keyword.

// A simple Java program to demonstrate how diamond


// problem is handled in case of default methods
  
interface GPI
{
    // default method
    default void show()
    {
        System.out.println("Default GPI");
    }
}
  
interface PI1 extends GPI { }
  
interface PI2 extends GPI { }
  
// Implementation class code
class TestClass implements PI1, PI2
{
    public static void main(String args[])
    {
        TestClass d = new TestClass();
        d.show();
    }
}

Output:

Default GPI

This ar ticle is contributed by Vishal S. If you like GeeksforGeeks and would like to

contribute, you can also write an ar ticle using contribute.geeksforgeeks.org or mail

We use ar
your cookies
ticletoto
ensure you have the best browsing experience on See
contribute@geeksforgeeks.org. our website.
your By arusing
ticleour site, you
acknowledge
appearing on the that

you have read and understood our


Cookie Policy &
Privacy Policy
GeeksforGeeks main page and help other Geeks.

Got It !

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 5/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

Please write comments if you find anything incorrect, or you want to share more

information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the impor tant Java

Foundation and Collections concepts with the Fundamentals of Java and Java

Collections Course at a student-friendly price and become industr y ready. To

complete your preparation from learning a language to DS Algo and many more, 

please refer Complete Inter view Preparation Course.

Like 0

Previous Next

RECOMMENDED ARTICLES Page : 1 2 3

Resolving Conflicts During Multiple Difference between Inheritance


We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie
01 05Policy &
Privacy Policy
Inheritance in Java and Interface in Java
03, Mar 21 Got It ! 20, Jun 20

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 6/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

Multiple Inheritance in C++ Illustrate Class Loading and Static


02 06
21, Sep 12
Blocks in Java Inheritance
18, Jan 21

Inheritance and constructors in


03
Java Interfaces and Inheritance in Java
21, Dec 10
07 28, Mar 17

Comparison of Inheritance in C++ Difference between Inheritance


04 08
and Java and Composition in Java
17, Nov 11 22, Jan 20

Ar ticle Contributed By :

GeeksforGeeks

Vote for difficulty

Current difficulty :
Medium

Easy Normal Medium Hard Expert

Article Tags : java-inheritance, Java, School Programming

Practice Tags : Java

Improve Article Report Issue

Writing code in comment?


Please use ide.geeksforgeeks.org,
generate link and share the link here.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy
Load Comments
Got It !

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 7/8
9/1/2021 Java and Multiple Inheritance - GeeksforGeeks

5th Floor, A-118,

Sector-136, Noida, Uttar Pradesh - 201305


feedback@geeksforgeeks.org

Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Copyright Policy Video Tutorials

Web Development Contribute


HTML Write an Article
CSS Write Interview Experience
JavaScript Internships
Bootstrap Videos

@geeksforgeeks
, Some rights reserved

We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that
you have read and understood our
Cookie Policy &
Privacy Policy

Got It !

https://www.geeksforgeeks.org/java-and-multiple-inheritance/ 8/8

You might also like