- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to call the constructor of a superclass from a constructor in java?
Whenever you inherit/extend a class, a copy of superclass’s members is created in the subclass object and thus, using the subclass object you can access the members of both classes.
Example
In the following example we have a class named SuperClass with a method with name demo(). We are extending this class with another class (SubClass).
Now, you create an object of the subclass and call the method demo().
class SuperClass{ public void demo() { System.out.println("demo method"); } } public class SubClass extends SuperClass { public static void main(String args[]) { SubClass obj = new SubClass(); obj.demo(); } }
Output
demo method
Super class’s Constructor in inheritance
In inheritance constructors are not inherited. You need to call them explicitly using the super keyword.
If a Super class have parameterized constructor. You need to accept these parameters in the sub class’s constructor and within it, you need to invoke the super class’s constructor using “super()” as −
public Student(String name, int age, String branch, int Student_id){ super(name, age); this.branch = branch; this.Student_id = Student_id; }
Example
Following java program demonstrates how to call a super class’s constructor from the constructor of the sub class using the super keyword.
class Person{ public String name; public int age; public Person(String name, int age){ this.name = name; this.age = age; } public void displayPerson() { System.out.println("Data of the Person class: "); System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); } } public class Student extends Person { public String branch; public int Student_id; public Student(String name, int age, String branch, int Student_id){ super(name, age); this.branch = branch; this.Student_id = Student_id; } public void displayStudent() { System.out.println("Data of the Student class: "); System.out.println("Name: "+this.name); System.out.println("Age: "+this.age); System.out.println("Branch: "+this.branch); System.out.println("Student ID: "+this.Student_id); } public static void main(String[] args) throws CloneNotSupportedException { Person person = new Student("Krishna", 20, "IT", 1256); person.displayPerson(); } }
Output
Data of the Person class: Name: Krishna Age: 20
- Related Articles
- How to call one constructor from another in Java?
- How to call Private Constructor in Java
- Java Program to Call One Constructor from another
- Can we call a constructor directly from a method in java?
- How to call a static constructor or when static constructor is called in C#?
- How to call the constructor of a parent class in JavaScript?
- Can we call a method on "this" keyword from a constructor in java?
- Golang program to call one constructor from another
- Haskell Program to Call One Constructor from Another
- How to declare a constructor in Java?
- How to explicitly call base class constructor from child class in C#?
- Can we call methods of the superclass from a static method in java?
- What are the rules for calling the superclass constructor C++?
- Order of Constructor/ Destructor Call in C++
- How to create a parameterized constructor in Java?
