5

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

}
5
  • B is a C, B is not an A. You can use B elsewhere as a C, you cannot use it as an A. Commented Jun 11, 2011 at 8:05
  • yes, so B is able to inherit attributes/ features of two classes? ? Commented Jun 11, 2011 at 8:07
  • 2
    That's is called delegation and is a substitute for inheritance. Say you inherit your father's family name, but you can ask your best friends to help you, but that doesn't make you have your best friend's family names too. Commented Jun 11, 2011 at 8:12
  • 1
    +1 -- I can see why this would confused the heck out of someone learning Java Commented Jun 11, 2011 at 8:14
  • Non-static inner classes simulate multiple inheritance using delegation. It "feels" like MI and can be quite useful in some situations. Commented Jul 10, 2013 at 8:27

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

yes, so B is able to inherit attributes/ features of two classes?
@Randon, I've added a paragraph on how inheritance is different from merely accessing a class member.
2

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..)

2 Comments

yes, so B is able to inherit attributes/ features of two classes?
@Randon: it has access to them, but it doesn't inherit from A. look at the code I just provided, which 'proves' C is not a subclass of A.
0

"multiple inheritance" refers to having more than one immediate parent.

In your example, A surrounds B and B is the parent of C, but this does not make A the parent of C.

If it was multiple inheritance then both A and B would be the parents of C (C extends A, B), and this can not be done in Java.

(and as a side note: In Java, if no parent is explicitly specified, then java.lang.Object is silently used. This is why every method has a toString()).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.