The super keyword in Java is used to refer to the immediate parent class object. It is commonly used to access parent class methods and constructors, enabling a subclass to inherit and reuse the functionality of its superclass.
Usage
The super keyword can be used in three primary contexts:
To call the superclass constructor.
To access a method from the superclass that has been overridden in the subclass.
To access a field from the superclass when it is hidden by a field of the same name in the subclass.
classAnimal{Animal(){
System.out.println("Animal constructor called");}}classDogextendsAnimal{Dog(){super();// Calls the constructor of Animal class
System.out.println("Dog constructor called");}}publicclassTestSuper{publicstaticvoidmain(String[] args){
Dog d =newDog();// Output: Animal constructor called// Dog constructor called}}提供:
In this example, the super() call in the Dog constructor invokes the constructor of the Animal class.
Example 2: Accessing Superclass Method
classAnimal{voidsound(){
System.out.println("Animal makes a sound");}}classDogextendsAnimal{voidsound(){super.sound();// Calls the sound() method of Animal class
System.out.println("Dog barks");}}publicclassTestSuper{publicstaticvoidmain(String[] args){
Dog d =newDog();
d.sound();// Output: Animal makes a sound// Dog barks}}提供:
Here, the super.sound() call in the Dog class method sound() invokes the sound() method of the Animal class.
Example 3: Accessing Superclass Field
classAnimal{
String color ="white";}classDogextendsAnimal{
String color ="black";voiddisplayColor(){
System.out.println("Dog color: "+// Prints the color of Dog class
System.out.println("Animal color: "+super.// Prints the color of Animal class}}publicclassTestSuper{publicstaticvoidmain(String[] args){
Dog d =newDog();
d.displayColor();// Output: Dog color: black// Animal color: white}}提供:
In this example, super.color is used to access the color field of the Animal class, which is hidden by the color field in the Dog class.
Tips and Best Practices
Constructor Chaining: Use super() in the constructor of a subclass to ensure that the constructor of the superclass is called, enabling proper initialization of the object.
classParent{Parent(){
System.out.println("Parent constructor");}}classChildextendsParent{Child(){super();// Always the first statement in the constructor
System.out.println("Child constructor");}}提供:
Method Overriding: Use super.methodName() to call a method from the superclass when it is overridden in the subclass. This is useful for extending or modifying the behavior of the inherited method.
Field Access: Use super.fieldName to access a field from the superclass when it is hidden by a field of the same name in the subclass. This helps to avoid ambiguity and ensures that the correct field is accessed.
classParent{
int number =10;}classChildextendsParent{
int number =20;voidshowNumber(){
System.out.println(super.number);// Accesses Parent's number}}提供:
Readability: Use super judiciously to make your code more readable and maintainable. Overusing super can make the code harder to understand.
Placement in Constructors: Always place the super() call as the first statement in the subclass constructor to avoid compilation errors.
Static Contexts: Remember that super cannot be used in static methods or static contexts, as it refers to an instance of a class.
The
superkeyword in Java is used to refer to the immediate parent class object. It is commonly used to access parent class methods and constructors, enabling a subclass to inherit and reuse the functionality of its superclass.Usage
The
superkeyword can be used in three primary contexts:Syntax
Examples
Example 1: Calling Superclass Constructor
In this example, the
super()call in theDogconstructor invokes the constructor of theAnimalclass.Example 2: Accessing Superclass Method
Here, the
super.sound()call in theDogclass methodsound()invokes thesound()method of theAnimalclass.Example 3: Accessing Superclass Field
In this example,
super.coloris used to access thecolorfield of theAnimalclass, which is hidden by thecolorfield in theDogclass.Tips and Best Practices
super()in the constructor of a subclass to ensure that the constructor of the superclass is called, enabling proper initialization of the object.super.methodName()to call a method from the superclass when it is overridden in the subclass. This is useful for extending or modifying the behavior of the inherited method.super.fieldNameto access a field from the superclass when it is hidden by a field of the same name in the subclass. This helps to avoid ambiguity and ensures that the correct field is accessed.superjudiciously to make your code more readable and maintainable. Overusingsupercan make the code harder to understand.super()call as the first statement in the subclass constructor to avoid compilation errors.supercannot be used in static methods or static contexts, as it refers to an instance of a class.