Alok Kumar Alok Kumar - 11 months ago 30
Java Question

Can we initialize an object with the help of constructor of another class?

Can we initialize an object with the help of constructor of another class?

class1 object = new class2();

Answer

As long as class2 extends (or implements in case of an interface) class1 that is fine. For example, List<String> list = new ArrayList<>();

To be clear, you are creating an instance of class2 (or ArrayList from my example). It just so happens that you have declared your variable to be of type class1 (or List).