I have one problem because I wanna put different classes in one array.
I have 5 classes: Dog, Kitchen, Cat, Cow, Bird
How can I put all this classes to one array?
I have one problem because I wanna put different classes in one array.
I have 5 classes: Dog, Kitchen, Cat, Cow, Bird
How can I put all this classes to one array?
Because every data type in Java extends Object
, you can just use an object array:
Object[] array = {new Dog(), new Kitchen(), new Cat(), new Cow(), new Bird()};
Primitives do not extend object, although autoboxing allows for primitives to be converted to objects when needed.
Use object array as following:-
Object[] objects = {new Dog(), new Kitchen(), new Cat(), new Bird()};