In Java, the super keyword is used to refer the constructor or a method of a parent class in an inheritance relationship. The super keyword cannot be used within in static context. For example, We cannot use super in the main method. There are three forms of super:
1. super(): Use of this calls the default constructor of the parent class. It should appear as the first line in a constructor of a child class.
2. super.method_name(): Use of this calls a method of the parent class from a child class,
3. super.variable_name: Use of this type refers to a variable of the parent class.
Use of super keyword:
call superclass constructor
class sqr {
sqr() {
System.out.println(“Defaul call”);
}
sqr(int a) {
System.out.println(a*a);
}
}
class cub extends sqr {
cub( int r,int s) {
// super(); // default construct call by default.
super(r);
System.out.println((r+s)*(r+s));
}
}
class overloding3 {
public static void main(String args[]) {
cub obj=new cub(10,12);
}
}
Overloading with super method
class A {
int a=10;
void sqr(int r) {
System.out.println((r*r));
}
}
class B extends A {
int a=20;
void sqr( int r) {
super.sqr(r);
System.out.println(a);
System.out.println(super.a);
System.out.println((r+r));
}
}
class overloding3 {
public static void main(String args[]) {
B obj=new B();
obj.sqr(9);
}
}