Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Inheritance in Java, class inside class

Tags:

java

we know in Java, there is no multiple inheritance, how will you justify it with this example.

Class A 

{

      has some features

     Class B extends Class C 
        {

              I can access A + C's features, B is child of two parents?
         }

}
like image 768
Randon Avatar asked Jun 11 '11 08:06

Randon


People also ask

Is multiple inheritance possible in Java classes?

Multiple inheritance in java is the capability of creating a single class with multiple superclasses. Unlike some other popular object oriented programming languages like C++, java doesn't provide support for multiple inheritance in classes.

Can multiple classes inherit the same class?

Although classes can inherit only one class, they can implement multiple interfaces. In the example above, we notice the use of the keyword implements to inherit from an interface.

Is multiple inheritance possible in interface?

As we have explained in the inheritance chapter, multiple inheritance is not supported in the case of class because of ambiguity. However, it is supported in case of an interface because there is no ambiguity. It is because its implementation is provided by the implementation class.

Why multiple inheritance is not supported in Java?

The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.


2 Answers

B is an inner class, with respect to A. It is not subclassing A on the other hand.

More accurately, B is a non-static nested class (which makes it an inner class). A nested class is a member of its enclosing class. Inner classes are allowed to have access to all members of the class that nests them. A private access specifier declared on a member of the enclosing class will not prevent an inner class from accessing the member.

The difference between inheritance via a sub-class and access to members via inner classes is that inheritance of members allows for members to shadowed (for attributes) and overridden (for methods). Inner classes cannot shadow enclosing class attributes, they will have access to them.

To simplify this even further, consider the code sample:

class C
{
    void c()
    {}
}

public class A {

    void a(){}

        class B extends C
        {
            // a new member of B
            void b()
            {
            }

            //does not override A.a()
            void a()
            {
                a(); //invokes A.a() as B has access to it.
                super.a(); //illegal                
            }

            //overrides C.c()
            @Override
            void c()
            {
                super.c(); //invokes C.c()
            }
        }
}

Notice that you cannot invoke super.a() from the nested class. That's the difference between inheriting a member and merely accessing it.

Further reading

  • The Java Language tutorial on classes and objects.
like image 118
Vineet Reynolds Avatar answered Sep 28 '22 00:09

Vineet Reynolds


B is not a child of two parents, it's a "child" of C, but has access to A's attributes, it's a part of A but not a subclass of it.
you can run the following code to be sure:

class Main {
    public void bar() {
        Object o = new B();
        System.out.println("is B subclass of Main:" + (o instanceof  Main));
        System.out.println("is B subclass of C:" + (o instanceof  C));
    }
    public class C { }
    public class B extends C { }
    public static void main(String[] argv) {
        Main a = new Main();
        a.bar();
    }
}

it will print false since o (which is a B) is not an instacneof Main for first question, but true for 2nd question, since B is an instance of C
EDIT: editted the code to make it more readable (in my opinion at least..)

like image 45
amit Avatar answered Sep 28 '22 01:09

amit