
write a java pgm to illustrate the keywords super,static and final

super Keyword: The keyword super is used to refer to the parent class instance of the current class.
class Base{
protected int num;
public Base(int n){
num = n;
System.out.println("parameter constructor of Base class");
}
public void display(){
System.out.println("Num from Base class: " + num);
}
}
class Derived extends Base{
private int num;
public Derived(int n1,int n2){
super(n1);
num = n2;
System.out.println("parameter constructor of Derived class");
}
public void display(){
super.display();
System.out.println("Num from Derived class: " + num);
}
}
public class UseOfSuper{
public static void main(String args[])
{
Derived ob = new Derived(10,20);
ob.display();
}
}
static keyword: If a method, variable is declared as static then it does not require any reference to call.There is only one instance of that particular member variable.
Final Keyword: If a variable is declared final, it can only be assigned once, allowing the compiler to optimize the byte code since it is known that the value of that variable is never going to change once assigned.
public class StaticFinalVariable{
static final int hoursInDay=24;
public static void main(String[] args){
System.out.println("Hours in 5 days = " + hoursInDay * 5);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.