Java super Keyword Examples with Demo

hello everyone !!! welcome to Pakainfo.com. today I am going to learn – Super Keyword In Java.

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

Simple Definition: Super() is used to invoke parent class constructor, method or variable.

super keyword in java Examples

The topics that I am going to learn in today’s session are :

super keyword in java
super keyword in java

  • what is super keyword in java?
  • Use of super with variables,
  • Use of super with methods,
  • and Use of super with constructors

So without wasting time lets start with today’s session.

Also Read: Java super Keyword Syntax With Example

what is super keyword in java?

First of all the question arises what is super keyword in java?

The super keyword refers to superclass that is parent class objects. It is used to call superclass methods, and to access the superclass constructor. The most common use of the super keyword is to eliminate the confusion between superclasses and subclasses that have methods with the same name. To understand the super keyword, one should have a basic understanding of Inheritance and Polymorphism.

/* Base class Employee */
class Employee
{
    int empMaxSalary = 120;
}
 
/* sub class Car extending Employee */
class Car extends Employee
{
    int empMaxSalary = 180;
 
    void display()
    {
        /* print empMaxSalary of base class (Employee) */
        System.out.println("Maximum Salary: " + super.empMaxSalary);
    }
}
 
/* Driver program to Welcome */
class Welcome
{
    public static void main(String[] args)
    {
        Car small = new Car();
        small.display();
    }
}

super keyword with variables

Super is majorly used in the contexts such as: super keyword with variables, super keyword with methods and super keyword with constructors. Lets see each one of them. first is – Use of super with variables: – This scenario occurs when a derived class and base class has same data members. In that case there is a possibility of ambiguity for the JVM.

I can understand it more clearly using this source code Example: In the above example, both base class and subclass have a member empMaxSalary. I could access empMaxSalary of base class in subclass using super keyword.

Use of super with methods

Second is – Use of super with methods: This is used when i want to call parent class method. So whenever a parent and child class have same named methods then to resolve ambiguity i use super keyword. This source code Example helps to understand the said usage of super keyword. In the above example, i have seen that if i only call method message() then, the current class message() is invoked but with the use of super keyword, method message() of superclass could also be invoked.

/* Base class Category */
class Category
{
    void message()
    {
        System.out.println("This is Category class");
    }
}
 
/* Subclass Product */
class Product extends Category
{
    void message()
    {
        System.out.println("This is Product class");
    }
 
    // Note that display() is only in Product class
    void display()
    {
        // will invoke or call current class message() method
        message();
 
        // will invoke or call parent class message() method
        super.message();
    }
}
 
/* Driver program to Welcome */
class Welcome
{
    public static void main(String args[])
    {
        Product s = new Product();
 
        // calling display() of Product
        s.display();
    }
}

Use of super with constructors

Third is – Use of super with constructors: super keyword can also be used to access the parent class constructor. One more important thing is that, ’super’ can call both parametric as well as non parametric constructors depending upon the situation. Following is the source code Example to explain the above concept: In the above example i have called the superclassconstructor using keyword ‘super’ via subclass constructor. Now i will see some important points to remember: Call to super() must be first statement in Derived(Product) Class constructor.

/* superclass Category */
class Category
{
    Category()
    {
        System.out.println("Category class Constructor");
    }
}
 
/* subclass Product extending the Category class */
class Product extends Category
{
    Product()
    {
        // invoke or call parent class constructor
        super();
 
        System.out.println("Product class Constructor");
    }
}
 
/* Driver program to Welcome*/
class Welcome
{
    public static void main(String[] args)
    {
        Product s = new Product();
    }
}

If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the superclass does not have a no-argument constructor, you will get a compile-time error. Object does have such a constructor, so if Object is the only superclass, there is no problem. If a subclass constructor invokes a constructor of its superclass, either explicitly or implicitly, you might think that a whole chain of constructors called, all the way back to the constructor of Object. This, in fact, is the case. It is called constructor chaining. Thank you for watching today’s session.

If you are new to this Tutorilas then share to pakainfo.com. You can also follow us on pakainfo.com facebook page. Social media links are given in the description below. if you like today’s article then like it, share with your friends. If you have any question or issues then please comment below. Thats it for today. Stay tuned for next article. Thank you.

Leave a Comment