Thursday , November 7 2024

Super keyword in java

Super keyword in java is a reference variable that is used to refer parent class object.Whenever you create the instance of subclass, an instance of parent class is created implicitly i.e. referred by super reference variable.

Usage of java super Keyword

  1. super is used to refer immediate parent class instance variable.
  2. super() is used to invoke immediate parent class constructor.
  3. super is used to invoke immediate parent class method.

1) super is used to refer immediate parent class instance variable.

//Parent class or Superclass
class Parentclass
{
int num=100;
}
//Child class or subclass
class Subclass extends Parentclass
{
/* I am declaring the same variable 
* num in child class too.
*/
int num=110;
void printNumber(){
System.out.println(num);
}
public static void main(String args[]){
Subclass obj= new Subclass();
obj.printNumber(); 
}
}
Output:
110

In the above program we have the same variable “num” declared in both parent class and sub class. There is no way we can access the num variable of parent class without using super keyword.

Accessing the num variable of parent class:

//Parent class or Superclass
class Parentclass
{
int num=100;
}
//Child class or subclass
class Subclass extends Parentclass
{
int num=110;
void printNumber(){
//Super.variable_name
System.out.println(super.num);
}
public static void main(String args[]){ 
Subclass obj= new Subclass();
obj.printNumber(); 
}
}
Output:
100

As you can see by using super.num we accessed the num variable of parent class.

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 Bike5 extends Vehicle{ 
Bike5(){ 
super();//will invoke parent class constructor 
System.out.println("Bike is created"); 
} 
public static void main(String args[]){ 
Bike5 b=new Bike5(); 

} 
}

Output:Vehicle is created
Bike is created

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 Student16 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[]){ 
Student16 s=new Student16(); 
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.

In case there is no method in subclass as parent, there is no need to use super. In the example given below message() method is invoked from Student class but Student class does not have message() method, so you can directly call message() method.
Program in case super is not required

class Person{ 
void message(){System.out.println("welcome");} 
} 

class Student17 extends Person{ 

void display(){ 
message();//will invoke parent class message() method 
} 

public static void main(String args[]){ 
Student17 s=new Student17(); 
s.display(); 
} 
}

Output:welcome

 

 

About admin

Check Also

Arrays in java

The array is a data structure which stores a fixed-size sequential collection of elements of …

Leave a Reply