super
keyword can be used to access the members of parent class. Apart from this, super
is also an instance of child class as justified by below code:
class A {
}
class B extends A {
public void method() {
System.out.println(this.getClass().isInstance(new B()));
System.out.println(super.getClass().isInstance(new B()));
}
}
public class InheritanceDemo {
public static void main(String[] args) {
B obj = new B();
obj.method();
}
}
Output: true true
Now, If super
is the instance of child class as justified above and java allows us to return any instance as per return type then why can we not return super
as an instance(I tried it with the help of below code but got compile error). On the other hand, we can return this
as an instance.
class A {
}
class B extends A {
public B methodThis() {
return this;
}
public A methodSuper() {
return super;
}
}