Q)What are the different uses of "super" keyword in Java?
=>super keyword is used in a Java program in 3 scenarios.
1)to call a parameterised super class constructor from within the sub class
constructor.
2)to call the overridden method from within the overriding method.
3)when super class variable name and sub class variable name are same,
to refer to the super class variable from the sub class method.
For eg.
class A{
int a;
A(){
a=10;
}
}
class B extends A{
int a;
B(){
a=50;
}
void display(){
System.out.println("super a="+super.a);
System.out.println("a="+a);
}
}
class Test{
public static void main(String[] args){
B b=new B();
b.display();
}
}
final modifier
--------------
=>"final" is a keyword in Java.
=>"final" keyword can be applied to
1)variables
2)methods
3)classes
final variables
----------------
=>When final modifier is applied to a variable, it becomes a constant.I.e. constants
are created in Java using "final" keyword.
For eg.
final int MAX=10;
=>Once a final variable(constant) is created, programmatically its value can’t be
changed in the program.
For eg.
final int MAX=10;
MAX=40;//error
=>final variable must be defined and should not be declared.
=>super keyword is used in a Java program in 3 scenarios.
1)to call a parameterised super class constructor from within the sub class
constructor.
2)to call the overridden method from within the overriding method.
3)when super class variable name and sub class variable name are same,
to refer to the super class variable from the sub class method.
For eg.
class A{
int a;
A(){
a=10;
}
}
class B extends A{
int a;
B(){
a=50;
}
void display(){
System.out.println("super a="+super.a);
System.out.println("a="+a);
}
}
class Test{
public static void main(String[] args){
B b=new B();
b.display();
}
}
final modifier
--------------
=>"final" is a keyword in Java.
=>"final" keyword can be applied to
1)variables
2)methods
3)classes
final variables
----------------
=>When final modifier is applied to a variable, it becomes a constant.I.e. constants
are created in Java using "final" keyword.
For eg.
final int MAX=10;
=>Once a final variable(constant) is created, programmatically its value can’t be
changed in the program.
For eg.
final int MAX=10;
MAX=40;//error
=>final variable must be defined and should not be declared.