Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday 8 March 2017

Super Keyword In Java

 

super Keyword

The 'super' keyword in java is a reference variable which is used to refer immediate parent class object.

'super' keyword is used to access the members(data members or methods) of the parent class objects.

Whenever you create the instance of subclass, an instance of a parent class is created implicitly which is referred by super keyword.


Usage of super keyword in java

  • super keyword can be used to refer immediate parent class instance variables.
  • super keyword can be used to invoke immediate parent class method.
  • super() keyword cane be used to invoke immediate parent class constructor.


Need of super keyword

Whenever the child class is inherits the parent class features, there is possibility that parent class features are similar to child class features and JVM gets an ambiguity. In order to differentiate between parent class features and child class features, you will use super keyword.

Syntax:

super.parent members(data members or methods);


Problem without using super keyword


For example:

class Father
{
int salary = 5000;
}
class Son extends Father
{
int salary = 7000;
void show()
{
System.out.println(salary);//print salary of Son class
}
public static void main(String args[])
{
Son s = new Son();
s.show();
}
}

output: 7000

In the above example, parent and child have same data member i.e salary and when we will print salary in child class it will print salary of child class(Son) because priority goes to child class, if you wanted to print salary of parent class also than you have to use super keyword in child class.


(1) super can be used to refer immediate parent class instance  variable

We can accesss the data member of parent class using super keyword . super keyword can be used when parent class and child class have same field(data members).

For example:

class Father
{
int salary = 5000;
}
class Son extends Father
{
int salary = 7000;
void show()
{
System.out.println(salary);//print salary of Son class
System.out.println(super.salary);//print salary of Father class
}
public static void main(String args[])
{
Son s = new Son();
s.show();
}
}

output: 7000
             5000




(2) super can be used to invoke parent class method

super keyword can be used to invoke or call parent class method. It happens when parent class method and child class method have same name i.e in case of method overriding.

For example:

class Father
{
void run()
{
System.out.println("run slow");
}
}
class Son extends Father
{
void run()
{
System.out.println("run fast");
super.run();
}
}
class Test
{
public static void main(String args[])
{
Son s = new Son();
s.run();
}
}

output: run fast
             run slow

In the above example , here parent and child have same name method called run() method. if we don't use super in run method of child class, it will only print run method of child class by default, if you want call run() method of parent class , you have to use super in child class.


👉 : super and this keyword cannot be used in static methods.

Program where super keyword is not required

In case there is no method in subclass as parent class, there is no need to use super keyword. In the example given below massage() method will invoke from Student class but student class does't have massage() method so you can call massage() method directly.

For example:

class Student
{
void massage()
{
System.out.println("Good Morning Sir");
}
}
class Faculty extends Student
{
void show()
{
massage();//call parent class massage() method
}
public static void main(String args[])
{
Faculty f = new Faculty();
f.show();
}
}

output: Good Morning Sir


(3) super() can be used to invoke parent class constructor

For example:

class Vehicle
{
Vehicle()
{
System.out.println("Vehicle");
}
}
class Bike extends Vehicle
{
Bike()
{
super();//call parent class constructor
System.out.println("Bike is running");
}
public static void main(String args[])
{
Bike b =new Bike();
}
}

output: Vehicle
            Bike is running

👉 : super() is added in each class constructor automatically by compiler if there is no super() or this().





As we know that default constructor is provided by the compiler automatically ,if there is no constructor in the program but compiler also add super() in the constructor as the firs line or statement.

Example where super() keyword provided by the compiler implicitly

For example:

class Vehicle
{
Vehicle()
{
System.out.println("Vehicle");
}
}
class Bike extends Vehicle
{
Bike()
{
System.out.println("Bike is running");
}
public static void main(String args[])
{
Bike b =new Bike();
}
}

output: Vehicle
             Bike is running
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate