The two major uses of super keyword are:
To
call the superclass constructor. super
() must always be the first statement executed inside a subclass’
constructor.
Ex:
class Base{
int a,b;
Base(int x,int y) {
a=x;
b=y;
}
}
class
Derive extends Base{
int c;
Derive(int x,int y,int z) {
super(x,y);
//Calling Base class constructor
c=z;
}
}
super
keyword can be used to access a member of the superclass that has been hidden
by a member of a subclass. This usage has the following general form:
super.member
Ex:
class Base{
int a;
Base(int x) {
a=x;
}
}
class
Derive extends Base{
int a;
Derive(int x,int y) {
super.a=x;
//Calling Base class constructor
a=y;
}
void Show() {
System.out.println(“ A is superclass: “ +super.a);
System.out.println(“ A is subclass: “ +a);
}
No comments:
Write comments