No, we cannot call subclass constructor from superclass constructor.
Example
class SuperClass{ SuperClass(){ System.out.println("SuperClass constructor"); SubClass(); } } public class SubClass extends SuperClass { SubClass (){ System.out.println("Subclass constructor"); } public static void main(String[] args) { System.out.println("Constructor test"); } } |
Output
SubClass.java:11: error: cannot find symbol SubClass(); ^ symbol: method Main() location: class SuperClass 1 error |
Java interview questions on constructor
- Default constructor
- Does constructor return any value in java?
- Is constructor inherited in java?
- Can you make a constructor final in java?
- Difference between constructor and method in java?
- How to copy values from one object to another java?
- How to overload constructor in java?
- can you create an object without using new operator in java?
- Constructor chaining in java
- Parameterized constructor in java
- Can we call subclass constructor from superclass constructor?
- What happens if you keep return type for a constructor?
- What is the use of private constructor in java?
- Can a constructor call another constructor java?