Javatpoint Logo
Javatpoint Logo

Difference between super and super() in Java with Examples

In Java, the "super" keyword serves as a reference variable that points to the objects of the parent class. It is primarily used in the context of inheritance, where a subclass inherits its superclass's fields, methods, and constructors.

When a subclass is created, the first thing that happens is that the superclass's Constructor is called. If the superclass has a parameterized constructor, the subclass constructor must call it explicitly using the super keyword to initialize the superclass variables. To invoke the superclass's default constructor, the subclass constructor must use the "super" keyword if the superclass does not have its own constructor.

The "super" keyword can be utilized to access the particular variable of a superclass. If a subclass defines a field with the same name as a field in its superclass, it can use "super" to access the field in the superclass. It is useful when we want to reference the superclass field instead of the subclass field with the same name.

The "super" keyword is an essential concept in Java programming used extensively in inheritance and object-oriented programming.

Filename: Dog.java

Output:

My name is Buddy
I am a Golden Retriever dog

super()

The super() method is employed to invoke the constructor of the parent class. Depending on the situation, it can be used to call both parameterized and non-parameterized constructors of the parent class. When a child class object is created, the parent class's Constructor is automatically called using the super(). When the parent class has a parameterized constructor, the child class constructor must use super() to call the parent class constructor and provide the necessary parameters.

Filename: School.java

Output:

Person class Constructor
Student class Constructor

Difference between super and super()

super super()
In Java, the "super" keyword is a reference variable that allows us to access and refer to objects of the parent class. In the constructor of a subclass, the "super()" method is used to invoke the constructor of its parent class.
The "super" keyword in Java can be used to access and invoke variables and methods of the parent class. The "super()" method in Java is specifically used to invoke constructors of the parent class. It allows the subclass to initialize the inherited members from the parent class before initializing its own members.
If one does not explicitly invoke a superclass variables or methods, by using super keyword, then nothing happens If a constructor does not explicitly invoke a superclass constructor by using super(), the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
It is a keyword in Java. It is not a keyword but rather a special method call.

Next Topicfor loop enum Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA