Super keyword | Super keyword in Java | Use of super in java

In this article super keyword we give the information about Super Keyword is a reference variable used to refer to parent class objects. Whenever we create an instance of a sub class, the instance of the parent class is created automatically.

In Java, Super Keyword is a reference variable used to refer to parent class objects. Whenever we create an instance of a sub class, the instance of the parent class is created automatically. Which is called super reference variable.

Super Keyword in Java

In Java, Super Keyword is a reference variable used to refer to parent class objects.

In other words, “Super keyword is used in sub class to access super class members”.

Whenever we create an instance of a sub class, the instance of the parent class is created automatically. Which is called super reference variable.

Use of Super Keyword-

  • It is used to access the data members of the parent class. When parent and child class have members of the same name.
  • It is used to call the method of the parent class.
  • Used to call the default and parameterized constructor of the parent class.

Use of Super with Variable:-

When there is same variable in parent class and child class then we use super to access the variable of parent class.

Example:-

class BaseClass

{

int num = 1000;

}

class SubClass extends BaseClass

{

int num = 2000;

void displayNumber()

{

System.out.println(“Num. Value: ” + super.num);

}

}

class Test

{

public static void main(String[] args)

{

SubClass value1 = new SubClass();

value1.displayNumber();

}

}

O/P:-
Num. Value: 1000

In the above example, both the base class and the sub class have a variable named num. We have accessed the num of the base class using the super keyword.

Using Super with methods:-

It is used when we want to call the method of parent class. It is used when base class and sub class have same method.

That is, it is used when the method is overriden by the sub class.

example:-

class BaseClass

{

//Overridden method

void display()

{

System.out.println(“This is Base class method”);

}

}

class Subclass extends BaseClass

{

//Overriding method

void display()

{

System.out.println(“This is Child class method”);

}

void printMsg()

{

//This would call Overriding method

display();

//This would call Overridden method

super.display();

}

public static void main(String args[])

{

Subclass obj= new Subclass();

obj.printMsg();

}

}

O/P:-
This is Base class method
This is Child class method.

In the above example you saw that when we are calling only display() then display() of subclass is getting invoked. But when we are using super with display() then display() of base class is also invoked.

Using Super with Constructors:-

The super keyword can also be used to access the parent class’s constructor. Most importantly, it can call both default and parameterized

Example:-

class SuperClass

{

SuperClass ()

{

System.out.println(“Super class Constructor”);

}

}

class SubClass extends SuperClass

{

SubClass ()

{

super();

System.out.println(“Sub class Constructor”);

}

}

class Test

{

public static void main(String[] args)

{

SubClass s = new SubClass ();

}

}

O/P:-
Super class Constructor
Sub class Construct!

Related Link:

Some More: DBMS/ WT/ DMDW

Santosh Nalawade

Work as Assistant Professor and Web Developer.

Leave a Reply

Your email address will not be published. Required fields are marked *