Constructor chaining is a process of calling a constructor from another constructor. We can use this() to call same class constructor and super() to call super class constructor.
class SuperClass{ public SuperClass(int i){ System.out.println("Super class constructor"); } } class SubClass extends SuperClass { public SubClass(){ //Calling same class constructor this(50); } public SubClass(int i){ //Calling super class constructor super(i); } } |
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?