Java Program to Call a Superclass Constructor From Sub/Child Class

You are currently viewing Java Program to Call a Superclass Constructor From Sub/Child Class

Java program to call a superclass constructor from sub/child class

// Java program to call a superclass constructor 
// from sub/child class

class Super {
  Super() {
    System.out.println("Super class constructor called.");
  }
}

class Sub extends Super {
  Sub() {
    super();
    System.out.println("Sub class constructor called.");
  }
}

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

Output

Super class constructor called.
Sub class constructor called.

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply