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?
}
}
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.
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.
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.
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.
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
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..)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With