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.
My name is Buddy I am a Golden Retriever dog
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.
Person class Constructor Student class Constructor
Difference between super and super()