Super and Final in Java
Super and Final in Java
PRESENTED BY:
ANSHU SHARMA
SUPER KEYWORD IN JAVA
The super keyword in Java is a reference variable which is
used to refer immediate parent class object.
Whenever you create the instance of subclass, an instance
of parent class is created implicitly which is referred by
super reference variable.
It is used to call superclass methods, and to access the
superclass constructor.
The most common use of the super keyword is to
eliminate the confusion between superclasses and
subclasses that have methods with the same name.
USE OF SUPER KEYWORD
super can be used to refer immediate parent class instance
variable.
super can be used to invoke immediate parent class method.
super() can be used to invoke immediate parent class constructor.
super () calls the parent class constructor with no argument.
super.methodname calls method from parents class.
It is used to call the parents class variable.
USE OF SUPER KEYWORD
EXAMPLES: 1) SUPER IS USED TO REFER
IMMEDIATE PARENT CLASS INSTANCE VARIABLE.
We can use super keyword to access the data member or field of parent class. It is used if
parent class and child class have same fields.
class vehicle{
String color="white";
}
class car extends vehicle{
String color="black";
void printColor(){
System.out.println(color);//prints color of car class
System.out.println(super.color);//prints color of vehicle class
}
}
EXAMPLE CONTD.
class TestSuper1{
public static void main(String args[]){
car c=new car();
c.printColor();
class person{
void eat(){System.out.println("eating...");}
}
class student extends person{
void eat(){System.out.println("eating ...");}
void read(){System.out.println(“Reading...");}
void work(){
super.eat();
read();
}
}
EXAMPLE CONTD.
class TestSuper2{
public static void main(String args[]){
student d=new student();
d.work();
}}
The super keyword can also be used to invoke the parent class constructor. Let's see a
simple example:
class person{
person()
{
System.out.println(“person class");}
}
class student extends person{
student()
{
super();
System.out.println(“student class");
}
}
EXAMPLE CONTD.
class TestSuper3{
public static void main(String args[]){
student d=new student();
}}
Output:
person class
Student class
As we know well that default constructor is provided by compiler automatically if
there is no constructor. But, it also adds super() as the first statement.
FINAL KEYWORD IN JAVA
The final keyword in java is used to restrict the user. The java
final keyword can be used in many context. Final can be:
• A variable
• A method
• A class
The final keyword can be applied with the variables, a final
variable that have no value it is called blank final variable or
uninitialized final variable. It can be initialized in the constructor
only. The blank final variable can be static also which will be
initialized in the static block only.
FINAL KEYWORD IN JAVA
1) JAVA FINAL VARIABLE
Output:
class Bike{
final void run()
{
System.out.println("running");}
}
class Honda extends Bike{
void run()
{
System.out.println("running safely with 100kmph");
}
EXAMPLE CONTD.
public static void main(String args[])
{
Honda honda= new Honda();
honda.run();
}
}
Output:
Output:
Compile Time Error
Thankyou