Super Keyword in Java with example. You can find Super keyword example Program (code snippets) in this page.
class Base1{
protected int num;
public Base1(int n){
num = n;
System.out.println(“\n\t1-parameter constructor of Base class.”);
}
public void display(){
System.out.println(“\n\tDisplay of Base class: ” + num);
}
}
class Derived1 extends Base1{
private int num;
public Derived1(int n1,int n2){
super(n1);
//super(n);
num = n2;
System.out.println(“\n\t1-parameter constructor of Derived class.”);
}
public void display(){
super.display();
System.out.println(“\n\tDisplay of Derived class: ” + num);
}
}
public class UseOfSuper{
public static void main(String args[])
{
Derived1 ob = new Derived1(10,20);
ob.display();
}
}
Result:
1-parameter constructor of Base class.
1-parameter constructor of Derived class.
Display of Base class: 10
Display of Derived class: 20