Constructor is a block of code that allows you to create an object of class and has same name as class with no explicit return type.
Whenever a class (child class) extends another class (parent class), the sub class inherits state and behavior in the form of variables and methods from its super class but it does not inherit constructor of super class because of following reasons:
- Constructors are special and have same name as class name. So if constructors were inherited in child class then child class would contain a parent class constructor which is against the constraint that constructor should have same name as class name. For example see the below code:
Child c1 =
Child c2 =
// not allowed
If we define Parent class constructor inside Child class it will give compile time error for return type and consider it a method. But for print method it does not give any compile time error and consider it a overriding method.
- Now suppose if constructors can be inherited then it will be impossible to achieving encapsulation. Because by using a super class’s constructor we can access/initialize private members of a class.
- A constructor cannot be called as a method. It is called when object of the class is created so it does not make sense of creating child class object using parent class constructor notation. i.e. Child c = new Parent();
- A parent class constructor is not inherited in child class and this is why super() is added automatically in child class constructor if there is no explicit call to super or this.
This article is contributed by Sajid Ali Khan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Java Object Creation of Inherited Class
- Constructors in Java
- Java | Constructors | Question 6
- Java | Constructors | Question 2
- Java | Constructors | Question 3
- Java | Constructors | Question 4
- Java | Constructors | Question 5
- Java | Constructors | Question 1
- Inheritance and constructors in Java
- Java | Constructors | Question 8
- Java Interview Questions on Constructors
- Output of Java Programs | Set 14 (Constructors)
- StringTokenizer class in Java with example | Set 1 ( Constructors)
- Private Constructors and Singleton Classes in Java
- Order of execution of Initialization blocks and Constructors in Java
- Difference between the Constructors and Methods
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.function.LongPredicate interface in Java with Examples
- Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
- Java.util.concurrent.RecursiveAction class in Java with Examples