Super keyword in java

super Keyword in java is used to refer the object of the immediate superclass, and it is related to inheritance in java.

Let’s say you have an instance variable or method of same name in subclass and superclass.

How will JVM know which one are you referring to; superclass or subclass?
That’s where you can use super keyword to refer superclass’s variables, methods or constructor.

Let’s understand this with the help of examples.

super keyword in java can be used at three-levels.

Usage of super at variable level

We can refer to variable of super class using: super.variableName
Let’s see this with the help of example.

  1. Person class has name variable.
  2. Employee class also has same name variable.

You can use super.name to refer to Person’s class name variable, there is no other way to access name variable in subclass.

When you run above program, you will get below output:

Calling Person constructor
Calling Employee class constructor
Printing default name from person class : Default

As you can see, we have used super.name to print name variable from the Person class in Employee class’s printName() method.

Usage of super at Constructor level

super keyword can be used to call the constructor of immediate parent class.

💡 Important point

Please note that super should be first statement in child class constructor.

Let’s see with the help of example:

When you run above program, you will get below output:

Calling Person Parameterized constructor
Calling Employee class constructor
Employee’s name:John

SuperConstructorCall

As you can see we have used super keyword to call Person constructor from employee class.

💡Did you know?

When you explicitly call parameterized constructor with super(paramerters), then complier won’t call no argument constructor of Parent class.

Usage of super keyword at method level

super keyword can be used to call method of Parent class. It can be used to specifically call method of parent class in case of method overriding.
Let’s see with the help of example:
We have printName() method in Person and Employee class and we will super.printName() to call Person’s printName() method.

When you run above program, you will get below output:

Calling Person constructor
Calling Employee class constructor
Printing default name from person class : Default
Printing name from Employee class : Martin

SuperMethodCall

As you can see, we have called Person class method from Employee class using super keyword.

What if subclass doesn’t have same method name as superclass?
You don’t need to use super keyword in case subclass doesn’t have the same method name as superclass.
Let’s see with the help of example.

Output:

Calling Person constructor
Calling Employee class constructor
Printing default name from person class : Default
Printing name from Employee class : Martin

As you can see, we did not use super with printNameSuperClass(), and it executed successfully.

That’s all about super keyword in java

Related Posts

  • 25 September

    Reference Variable in Java

    In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class or access static member of the […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

  • 17 June

    How to learn Java Programming

    Java is one of the most used programming languages in the world for developing the desktop application and web application.It has completed 22 years in the programming world. In this post, we will see how to learn java programming in depth and in most efficient way. Java has a huge learning curve, it is a […]

  • 28 May

    Introduction to Java Programming

    Java is one of the most used programming languages in the world for developing desktop application and web application. It has completed 22 years in the programming world. History of Java: Java programming language was developed by James Ghosling, Patrick Naughton, Mike Sheridan at Sun Microsystems Inc. in 1991. The initial name was Oak but […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.