0
0
JavaHow-ToBeginner · 3 min read

How to Call Parent Class Constructor in Java: Syntax and Examples

In Java, you call a parent class constructor using the super() keyword inside the child class constructor. This must be the first statement in the child constructor to properly initialize the parent part of the object.
📐

Syntax

The syntax to call a parent class constructor in Java is:

super();

or with parameters:

super(parameter1, parameter2, ...);

Explanation:

  • super() calls the parent class's no-argument constructor.
  • You can pass arguments to super() to call a parent constructor with parameters.
  • This call must be the first line in the child class constructor.
java
public class Parent {
    public Parent() {
        System.out.println("Parent constructor called");
    }

    public Parent(String message) {
        System.out.println("Parent says: " + message);
    }
}

public class Child extends Parent {
    public Child() {
        super(); // calls Parent's no-arg constructor
        System.out.println("Child constructor called");
    }

    public Child(String msg) {
        super(msg); // calls Parent's constructor with a String
        System.out.println("Child constructor with message called");
    }
}
💻

Example

This example shows how a child class calls different parent constructors using super(). It demonstrates both no-argument and parameterized parent constructors.

java
class Parent {
    public Parent() {
        System.out.println("Parent constructor called");
    }

    public Parent(String message) {
        System.out.println("Parent says: " + message);
    }
}

class Child extends Parent {
    public Child() {
        super();
        System.out.println("Child constructor called");
    }

    public Child(String msg) {
        super(msg);
        System.out.println("Child constructor with message called");
    }
}

public class Main {
    public static void main(String[] args) {
        Child child1 = new Child();
        System.out.println();
        Child child2 = new Child("Hello from child");
    }
}
Output
Parent constructor called Child constructor called Parent says: Hello from child Child constructor with message called
⚠️

Common Pitfalls

Common mistakes when calling parent constructors include:

  • Not calling super() explicitly when the parent has no default constructor, causing a compile error.
  • Calling super() after other statements in the child constructor, which is not allowed.
  • Forgetting to match the parent constructor parameters when using super(args).

Example of wrong and right usage:

java
class Parent {
    public Parent(String msg) {
        System.out.println("Parent: " + msg);
    }
}

class Child extends Parent {
    // Wrong: no call to super() and parent has no no-arg constructor
    /*
    public Child() {
        System.out.println("Child constructor"); // Error: super() must be called first
    }
    */

    // Right:
    public Child() {
        super("Called from child");
        System.out.println("Child constructor");
    }
}
📊

Quick Reference

Remember these tips when calling parent constructors:

  • super() must be the first statement in the child constructor.
  • If the parent has no default constructor, you must call a parent constructor explicitly.
  • You can pass arguments to super() to match the parent's constructor parameters.
  • If you don't call super(), Java inserts a no-arg call automatically only if the parent has a no-arg constructor.

Key Takeaways

Use super() to call the parent class constructor from a child class constructor.
super() must be the first statement in the child constructor.
Pass arguments to super(args) to call a parent constructor with parameters.
If the parent has no no-argument constructor, you must explicitly call a parent constructor.
Not calling super() properly causes compile-time errors.