super()
In Java, super() is used in inheritance. It lets a child class call the constructor of its parent class.
Inheritance happens when one class extends another class.
class Dog extends Animal
In this example:
Animal is the parent class.Dog is the child class.Dog inherits from Animal.super() Mean?
The keyword super refers to the parent class.
When we write:
super();
We are calling the constructor of the parent class.
A constructor is a special method that runs when an object is created.
super()?
We use super() when the child class needs to run the parent class constructor first.
This helps Java set up the parent part of the object before setting up the child part.
class Animal {
public Animal() {
System.out.println("Animal constructor runs.");
}
}
class Dog extends Animal {
public Dog() {
super();
System.out.println("Dog constructor runs.");
}
}
public class Main {
public static void main(String[] args) {
Dog myDog = new Dog();
}
}
Animal constructor runs.
Dog constructor runs.
The parent constructor runs first.
super()
Important: super() must be the first statement inside the child class constructor.
public Dog() {
super();
System.out.println("Dog created.");
}
public Dog() {
System.out.println("Dog created.");
super(); // Error
}
This causes an error because super() must come first.
Java does this because the parent class must be initialized before the child class.
super()
If you do not write super(), Java automatically adds it for you, but only if the parent class has a no-argument constructor.
class Animal {
public Animal() {
System.out.println("Animal constructor runs.");
}
}
class Dog extends Animal {
public Dog() {
System.out.println("Dog constructor runs.");
}
}
Java treats the Dog constructor like this:
public Dog() {
super();
System.out.println("Dog constructor runs.");
}
super() With ParametersSometimes the parent class constructor needs information.
class Animal {
private String name;
public Animal(String animalName) {
name = animalName;
}
public String getName() {
return name;
}
}
The parent class does not have a no-argument constructor.
The child class must call the parent constructor using super(value).
class Dog extends Animal {
private String breed;
public Dog(String animalName, String dogBreed) {
super(animalName);
breed = dogBreed;
}
public String getBreed() {
return breed;
}
}
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog("Buddy", "Golden Retriever");
System.out.println(dog1.getName());
System.out.println(dog1.getBreed());
}
}
Buddy
Golden Retriever
When this line runs:
Dog dog1 = new Dog("Buddy", "Golden Retriever");
Java does the following:
Dog object.Dog constructor.Dog constructor calls super(animalName).Animal constructor stores the name.Dog constructor stores the breed.super()If the parent class only has a constructor with parameters, the child class must call it.
class Animal {
public Animal(String name) {
System.out.println(name);
}
}
class Dog extends Animal {
public Dog() {
System.out.println("Dog created.");
}
}
This causes an error because Java tries to automatically call:
super();
But the Animal class does not have a no-argument constructor.
class Dog extends Animal {
public Dog() {
super("Buddy");
System.out.println("Dog created.");
}
}
super() vs. super
There is a difference between super() and super.
| Code | Purpose |
|---|---|
super() |
Calls the parent class constructor. |
super.methodName() |
Calls a method from the parent class. |
super.variableName |
Can access a visible variable from the parent class. |
class Animal {
public void speak() {
System.out.println("Animal makes a sound.");
}
}
class Dog extends Animal {
public void speak() {
super.speak();
System.out.println("Dog barks.");
}
}
Animal makes a sound.
Dog barks.
public class Vehicle {
private String brand;
private int year;
public Vehicle(String vehicleBrand, int vehicleYear) {
brand = vehicleBrand;
year = vehicleYear;
}
public String getBrand() {
return brand;
}
public int getYear() {
return year;
}
public void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Year: " + year);
}
}
public class Car extends Vehicle {
private int numberOfDoors;
public Car(String carBrand, int carYear, int doors) {
super(carBrand, carYear);
numberOfDoors = doors;
}
public int getNumberOfDoors() {
return numberOfDoors;
}
public void displayCarInfo() {
displayInfo();
System.out.println("Number of doors: " + numberOfDoors);
}
}
public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2022, 4);
myCar.displayCarInfo();
}
}
Brand: Toyota
Year: 2022
Number of doors: 4
| Term | Meaning |
|---|---|
| Parent Class | The class being inherited from. |
| Child Class | The class that inherits from another class. |
| Inheritance | A way for one class to reuse another class’s code. |
| Constructor | A special method that runs when an object is created. |
super() |
Calls the parent class constructor. |
super |
Refers to the parent class. |
super() calls the parent class constructor.super() must be the first line in the child constructor.super() if you do not write it.super(arguments).super() is used with constructors.super.methodName() can call a parent method.What does super() do in Java?
Answer: It calls the constructor of the parent class.
Where must super() appear inside a constructor?
Answer: It must be the first statement.
What happens if the parent class has no no-argument constructor?
Answer: The child class must call the parent constructor using super() with the correct arguments.
What is the difference between super() and super.methodName()?
Answer: super() calls a parent constructor.
super.methodName() calls a method from the parent class.
The super() statement is used when a child class needs to call the constructor of its parent class.
It helps Java build the parent part of the object before building the child part.
It must always be the first line in the child constructor.
Using super() is an important part of inheritance in Java.