super
KeywordIn Java, the super
keyword is used to refer to the superclass (parent class) of the current object. It serves as a reference to the superclass and is commonly used to access superclass members (fields, methods, and constructors) from the subclass. The super
keyword is vital in scenarios involving method overriding, constructor chaining, and when you need to access hidden members in the subclass.
This guide will walk you through the uses and concepts surrounding the super
keyword in Java with examples.
super
Keyword?super
to Call Superclass Methodssuper
to Access Superclass Fieldssuper()
to Call Superclass Constructorsuper
and this
super
super
super
Keyword?In Java, the super
keyword refers to the superclass of the current object. It can be used in several contexts, such as:
The super
keyword helps distinguish between the members of the superclass and the subclass, especially in cases where they have the same names.
super
to Call Superclass MethodsWhen a subclass overrides a method of its superclass, you can use the super
keyword to call the original method from the superclass. This is useful when you want to add new functionality in the subclass while retaining the functionality from the superclass.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
super.sound(); // Calls the superclass method
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Animal makes a sound
Dog barks
In this example, the Dog
class overrides the sound()
method from the Animal
class. The super.sound()
call in the Dog
class invokes the sound()
method from the superclass before executing its own implementation.
super
to Access Superclass FieldsIf a subclass has a field with the same name as a field in its superclass, the subclass field hides the superclass field. To access the superclass field, you can use the super
keyword.
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void displayNames() {
System.out.println("Superclass name: " + super.name); // Accesses superclass field
System.out.println("Subclass name: " + name); // Accesses subclass field
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
}
}
Superclass name: Animal
Subclass name: Dog
Here, the super.name
is used to access the name
field of the Animal
class (the superclass), while name
directly refers to the name
field of the Dog
class (the subclass).
super()
to Call Superclass ConstructorThe super()
keyword is used to call the constructor of the superclass. It is used in the subclass constructor to initialize the superclass part of the object. If no constructor is explicitly defined in the superclass, the default constructor is called automatically.
class Animal {
Animal() {
System.out.println("Animal constructor");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the superclass constructor
System.out.println("Dog constructor");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
}
}
Animal constructor
Dog constructor
In this example, the Dog
class constructor calls super()
to invoke the constructor of the Animal
class (the superclass) before executing its own code.
super
and this
While both super
and this
are used to refer to the object, they serve different purposes:
super
: Refers to the superclass (parent class).
this
: Refers to the current object (instance of the class).
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog";
void displayNames() {
System.out.println("Superclass name: " + super.name); // Refers to superclass field
System.out.println("Subclass name: " + this.name); // Refers to subclass field
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
}
}
Superclass name: Animal
Subclass name: Dog
In this case, super.name
refers to the name
field in the superclass Animal
, while this.name
refers to the name
field in the Dog
class.
super
The super
keyword is used in several scenarios:
super()
is used to ensure the proper initialization of both the superclass and subclass.super
super
makes it clear that you are intentionally accessing a superclass member or constructor.super
helps resolve ambiguity when a subclass hides a superclass member (field or method).super()
, you can ensure proper initialization of the superclass part of an object in a constructor.