Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Why multiple inheritance is not possible in JAVA?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 364
    Comment on it

    Inheritance

    When one class acquires the functionality of some other class, it is called inheritance. i.e reusing the same properties and functions. The class which is inherited is known as the super class and the class which inherits the properties is known as the sub class.

    syntax: [sub-class] extends [super-class]
    extends keyword is used to inherit the properties of a class.

    Multiple inheritance:

    alt text

    When one class derives the properties from two of the super classes, then it is known as multiple inheritance but this is not supported by java.

    There are various reasons for this:

    1) Diamond problem

    Suppose we have a class A.

    class A
    {
    void display()
    {
    statements;
    }
    }
    

    class B and C extends class A

    class B extends A
    {
    void display()
    {
    statements;
    }
    }
    
    class C extends A
    {
    void display()
    {
    statements;
    }
    }
    

    class D extend class B and class C

    class D extends A,B  // not possible in java
    {
    void display()
    {
    statements;
    }
    }
    

    alt text

    It has developed a structure like diamond , thats why we called it diamond problem.

    In this if we create an object of class D and try to call the display() method defined in class A, then its not certain that which version of display() should be called. i.e the one defined inside class B or the one defined inside class C. Its very ambiguous to figure out the correct method call.

    2) Complexity:

    It makes the structure too complex. The cost of complexity is too high. So to avoid complexity it finds in better to avoid using multiple inheritance.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: