Java Class Notes: Using super()

Student Note

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:

1. What Does 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.

2. Why Do We Use 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.");
    }
}

Driver Code

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
    }
}

Output

Animal constructor runs.
Dog constructor runs.

The parent constructor runs first.

3. Important Rule About super()

Important: super() must be the first statement inside the child class constructor.

Correct

public Dog() {
    super();
    System.out.println("Dog created.");
}

Incorrect

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.

4. Java Automatically Calls 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.");
}

5. Using super() With Parameters

Sometimes 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;
    }
}

Driver Code

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());
    }
}

Output

Buddy
Golden Retriever

6. Why This Works

When this line runs:

Dog dog1 = new Dog("Buddy", "Golden Retriever");

Java does the following:

  1. Creates a Dog object.
  2. Calls the Dog constructor.
  3. The Dog constructor calls super(animalName).
  4. The Animal constructor stores the name.
  5. The Dog constructor stores the breed.

7. Common Error With super()

If the parent class only has a constructor with parameters, the child class must call it.

Incorrect

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.

Correct

class Dog extends Animal {
    public Dog() {
        super("Buddy");
        System.out.println("Dog created.");
    }
}

8. 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.

Example: Calling a Parent Method

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.");
    }
}

Output

Animal makes a sound.
Dog barks.

9. Full Example Program

Parent Class: Vehicle.java

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);
    }
}

Child Class: Car.java

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);
    }
}

Driver Class: Main.java

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Toyota", 2022, 4);

        myCar.displayCarInfo();
    }
}

Output

Brand: Toyota
Year: 2022
Number of doors: 4

10. Key Vocabulary

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.

11. Key Rules to Remember

  1. super() calls the parent class constructor.
  2. super() must be the first line in the child constructor.
  3. Java automatically calls super() if you do not write it.
  4. If the parent constructor has parameters, the child constructor must pass values using super(arguments).
  5. super() is used with constructors.
  6. super.methodName() can call a parent method.

12. Practice Questions

Question 1

What does super() do in Java?

Answer: It calls the constructor of the parent class.

Question 2

Where must super() appear inside a constructor?

Answer: It must be the first statement.

Question 3

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.

Question 4

What is the difference between super() and super.methodName()?

Answer: super() calls a parent constructor. super.methodName() calls a method from the parent class.

13. Student Summary

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.