[!INDEX]
- Definition
- Calling the Superclass Constructor (
super()), animal - dog - main- Approach
- Accessing Superclass Method (
super.methodName()) animal - dog - main- Approach
- Accessing Superclass Field (
super.fieldName) animal - dog - main- Approach
1. Definition
[!NOTE]
- The
superkeyword in Java is used to refer to the parent class (also called the superclass) of the current object. It provides a way to access members (fields, methods, and constructors) of the parent class from the child class.Uses of the
superkeyword in Java:
- To call the superclass constructor.
- To access a superclass method.
- To access a superclass field.
2. Calling the Superclass Constructor (super())
[!NOTE]
- The
super()keyword is used to call the constructor of the parent class. It must be the first statement in the subclass constructor. If you don’t explicitly callsuper(), Java automatically calls the default constructor of the parent class.
class Animal {
Animal() {
System.out.println("Animal constructor called");
}
}
class Dog extends Animal {
Dog() {
super(); // Calls the constructor of Animal class
System.out.println("Dog constructor called");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Animal();
Dog dog = new Dog(); // When Dog is created, Animal's ct is also called
System.out.println("main method called ");
}
}
Animal constructor called
Animal constructor called
Dog constructor called
main method called
3. Approach
[!approach]
- subclass ct contains super() keywords
4. Accessing Superclass Method (super.methodName())
[!NOTE]
- The
superkeyword is used to call a method from the parent class. This is useful when the child class overrides a method, but you still want to call the parent class's version of the method.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void makeSound() {
super.makeSound(); // Calls the makeSound() method of the Animal class
System.out.println("Dog barks");
}
}
public class Main1 {
public static void main(String[] args) {
Animal a1 = new Animal();
a1.makeSound();
Dog dog = new Dog();
dog.makeSound(); // Calls Dog's makeSound(), which also calls Animal's makeSound()
System.out.println("main ");
}
}
Animal makes a sound
Animal makes a sound
Dog barks
main
5. Approach
[!approach]
- same as above but, super.mt name
6. Accessing Superclass Field (super.fieldName)
[!NOTE]
- The
superkeyword is used to access a field from the parent class when it is hidden by a field in the subclass.
class Animal {
String name = "Animal";
}
class Dog extends Animal {
String name = "Dog"; // Hides the name field of Animal class
void displayNames() {
System.out.println("Parent class name: " + super.name); // Refers to the Animal class's name field
System.out.println("Child class name: " + this.name); // Refers to the Dog class's name field
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.displayNames();
}
}
Parent class name: Animal
Child class name: Dog
7. Approach
[!approach]
- same as above