When working with multiple inheritance, which class does the keyword "super" refer to, the very first class created? or the parent class of the subclass we are working with?
Thank you for you consideration.
When working with multiple inheritance, which class does the keyword "super" refer to, the very first class created? or the parent class of the subclass we are working with?
Thank you for you consideration.
In an inheritance chain, if you call super on an instance of class D, which is derived from C, which itself is derived from B and that from A, a call to super will go to the direct parent. If the method isn't found there, the chain of parents is climbed up:
class A {
void print () { System.out.println ("from A"); }
}
class B extends A {
void print () { System.out.println ("from B"); }
}
class C extends B {
// no own print method
}
class D extends C {
void print () { super.print (); }
}
D d = new D ();
d.print ();
So here, super of D is C, where no print is found, so a lookup is made to B, as if C.print () was called. There a concrete implementation is found which is used to perform the action.
from B
This is normally not called multiple inheritance, but if you derive from multiple parents, which you can't in Java. You can implement multiple interfaces with their methods, and their names are, most of the time, not in naming conflict.
Example:
interface RocknRoller {
void roll ();
}
interface Gambler {
void roll ();
}
class Dice implements Gambler {
public void roll () { System.out.println ("roll a dice"); }
}
class Harrisson implements RocknRoller {
public void roll () { System.out.println ("while my guitar gently weeps"); }
}
class E extends D implements RocknRoller, Gambler {
RocknRoller rr = new Harrisson ();
Gambler g = new Dice ();
public void roll () {
rr.roll ();
g.roll ();
}
}
-> E e = new E ();
| Added variable e of type E with initial value E@47ef968d
-> e.roll ()
while my guitar gently weeps
roll a dice
You can have some kind of multiple inheritance via Interfaces. But interface normally don't have their own implementation of code, so when you implement multiple interfaces, you're only declaring to conform to some contract.
The implentation has to be done by yourself (or explicitly delegated). But by doing so, you're responsible yourself in solving the conflict; there is no mechanism to solve it automatically. In this example, both implementations are called in a specific order.
The super keyword in java is a reference variable that is used to refer parent class objects. This is used when we want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity we use super keyword. This code snippet helps to understand the said usage of super keyword. Suppose we have 2 classes like:
class Parent {
String a ="parent string";
}
class Child extends Parent{
String a= "child string";
void show(){
System.out.println(a);
System.out.println(super.a);
}
}
public class Sample{
public static void main(String []args){
Child c= new Child();
c.show();
}
}
Now output will be :
child string
parent string
Simple a
refers to child class.
super.a
refers to immediate parent class.
Hope it helps. :)
Java supports linear inheritance. This can be of multiple levels but it does not support a class deriving from multiple immediate parent classes.
Now Suppose there is a below hierarchy of classes.
classes : A, B, C
hierarchy : C extends B, B extends A.
When inside any instance method (or instance block) defined in C if we use super
keyword it refers to instance members and fields of immediate parent. If used in C means it refers to instance members or fields of B, if used in B it means it refers to instance members or fields of A and if used in A it means it refers to instance members or fields of Object.java
Also for invoking a particular constructor of the immediate parent class super
can be used from within a constructor of child class, with the condition that it should be the first statement.
P.S. if the instance member or method is not visible to child class then compilation will fail. A class inherites all the properties and behavior from it's parent class. The keyword is useful in cases when we override the behavior in child class and still we want to invoke a behavior defined by parent class.