Java – can we initialize an object with the help of the constructor of another class?

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

class1 object = new class2();

Solution

As long as class2 extends (or implements in the case of interface) Class1 For example, list < string > List = new ArrayList < > ();

To be clear, you are creating an instance of class2 (or ArrayList in my example) You have just declared that your variable belongs to Class1 (or list)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>