Super:
Super is a reference variable that is used to refer immediate parent class object.
Syntax:
super.<method-name>();
Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.
Usage of super Keyword:
1) super is used to refer immediate parent class instance variable.
Problem without super keyword
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
Output:100
In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance bydefault, but if
parent class instance variable is to be referred then we use super keyword to distinguish between parent class instance variable and current class instance variable.
Solution by super keyword
//example of super keyword
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
Output:50
2) super is used to invoke parent class constructor.The super keyword can also be used to invoke the parent class constructor as given below:
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
}
class Bike extends Vehicle{
Bike(){
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
Output:Vehicle is created
Bike is created
super() is added in each class constructor automatically by compiler.
Another example of super keyword where super() is provided by the compiler implicitly.
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
}
class Bike extends Vehicle{
int speed;
Bike(int speed){
this.speed=speed;
System.out.println(speed);
}
public static void main(String args[]){
Bike b=new Bike(10);
}
}
Output:Vehicle is created
10
3) super can be used to invoke parent class method.
The super keyword can also be used to invoke parent class method. It should be used in case subclass contains the same method as parent class as in the example given below:
class Person{
void message(){System.out.println("welcome");}
}
class Student extends Person{
void message(){System.out.println("welcome to java");}
void display(){
message();//will invoke current class message() method
super.message();//will invoke parent class message() method
}
public static void main(String args[]){
Student s=new Student();
s.display();
}
}
Output:welcome to java
welcome
In the above example Student and Person both classes have message() method if we call message() method from Student class, it will call the message() method of Student class not of Person class because priority is given to local.
Final:
Final is used for
Super is a reference variable that is used to refer immediate parent class object.
Syntax:
super.<method-name>();
Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.
Usage of super Keyword:
- Super is used to refer immediate parent class instance variable. It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword.
- Super() is used to invoke immediate parent class constructor. It is also used by class constructors to invoke constructors of its parent class.
- Super is used to invoke immediate parent class method.
1) super is used to refer immediate parent class instance variable.
Problem without super keyword
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(speed);//will print speed of Bike
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
Output:100
In the above example Vehicle and Bike both class have a common property speed. Instance variable of current class is refered by instance bydefault, but if
parent class instance variable is to be referred then we use super keyword to distinguish between parent class instance variable and current class instance variable.
Solution by super keyword
//example of super keyword
class Vehicle{
int speed=50;
}
class Bike extends Vehicle{
int speed=100;
void display(){
System.out.println(super.speed);//will print speed of Vehicle now
}
public static void main(String args[]){
Bike b=new Bike();
b.display();
}
}
Output:50
2) super is used to invoke parent class constructor.The super keyword can also be used to invoke the parent class constructor as given below:
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
}
class Bike extends Vehicle{
Bike(){
super();//will invoke parent class constructor
System.out.println("Bike is created");
}
public static void main(String args[]){
Bike b=new Bike();
}
}
Output:Vehicle is created
Bike is created
super() is added in each class constructor automatically by compiler.
Another example of super keyword where super() is provided by the compiler implicitly.
class Vehicle{
Vehicle(){System.out.println("Vehicle is created");}
}
class Bike extends Vehicle{
int speed;
Bike(int speed){
this.speed=speed;
System.out.println(speed);
}
public static void main(String args[]){
Bike b=new Bike(10);
}
}
Output:Vehicle is created
10
3) super can be used to invoke parent class method.
The super keyword can also be used to invoke parent class method. It should be used in case subclass contains the same method as parent class as in the example given below:
class Person{
void message(){System.out.println("welcome");}
}
class Student extends Person{
void message(){System.out.println("welcome to java");}
void display(){
message();//will invoke current class message() method
super.message();//will invoke parent class message() method
}
public static void main(String args[]){
Student s=new Student();
s.display();
}
}
Output:welcome to java
welcome
In the above example Student and Person both classes have message() method if we call message() method from Student class, it will call the message() method of Student class not of Person class because priority is given to local.
Final:
Final is used for
- Variable
- Method
- Class
For a variable
The final keyword only allows a single assignment for the variable. That is to say, once the variable has been assigned, its value is in read-only. If the variable is a primitive type, its value will no longer change. If it is an object, only its reference will no longer change. Keep in mind that its value can still be changed.
For a class
The final keyword forbids to create a subclass(stops inheritance). It is the case of the Integer or String class.
A final class cannot be abstract. The final keyword is similar to sealed keyword in C#.
For a method
The final keyword forbids to override the method in a subclass. It is useless if the class is already final and a private method is implicitly final. A final method cannot be abstract.
The final keyword only allows a single assignment for the variable. That is to say, once the variable has been assigned, its value is in read-only. If the variable is a primitive type, its value will no longer change. If it is an object, only its reference will no longer change. Keep in mind that its value can still be changed.
For a class
The final keyword forbids to create a subclass(stops inheritance). It is the case of the Integer or String class.
A final class cannot be abstract. The final keyword is similar to sealed keyword in C#.
For a method
The final keyword forbids to override the method in a subclass. It is useless if the class is already final and a private method is implicitly final. A final method cannot be abstract.
No comments:
Post a Comment