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.

Table of Contents

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

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.