Can you have an array of different objects?

I know that you can only have an array of a certain type (e.g. String, int, Student, etc.). I was wondering if this is true in the case of inheritance - that is, whether the Bicycle object extending the vehicle can be placed in the Vehicle array along with something else, such as a bus object.

Here is my code:

public class Test{ public static void main(String[] args) { Bus bus1 = new Bus(); Bicycle bike1 = new Bicycle(); bike1.changeGear(true); Bus bus2 = new Bus(); Vehicle[] myFleet = {bus1, bike1, bus2}; // Note that Vehicle is an abstract class for (Bus v: myFleet){ // Trying to access every bus in the Vehicle array. I already v.issueTicket(1.50); // tried to get the computer to treat myFleet as a Bus array - doesn't System.out.println(v); // work, sadly. } } } 

Please note that this is in Java

+5
source share
6 answers

You have a super-type array.

for iterates through ALL elements in the iterable object (here an array).

Since there may be instances of a Bicycle or Bus in the array (or even other types that are currently unknown), you cannot treat it like a Bus array.

What you might want is this:

 for (Vehicle v : myFleet) { if (v instanceof Bus) { Bus b = (Bus)v; b.doSomeSpecialThingsForABus(); } } 

There is no other way around this, except for the visitor template .

+4
source

If ClassTwo extends ClassOne and you create an array of ClassOnes, then you can add objects of the ClassTwo class to the array, however, you can access members of the ClassOne class only if you do not execute them.

+1
source

The time has come to understand your thought.

If you want to do something like this, you need to drop Vehicle on the Bus . However, usually you should have an interface common to all cars.

Since issueTicket() does not apply to bicycles, you could probably consider not using the same interface for buses and bicycles in general.

Another idea is to implement the issueTicket() method for bead, which simply signals an error as soon as it calls:

 Vehicle[] myFleet = {bus1, bike1, bus2}; // Note that Vehicle is an abstract class for (Vehicle v: myFleet){ v.issueTicket(1.50); System.out.println(v); } 

However, in my opinion, it still looks like a design that could be better.

So that I can offer a few more suggestions, it would be nice to know the reason why these objects should be stored in one container.

Hope this helps.

Regards, JΓΆrg

+1
source

you can use

java.lang.Object[] myFleet;

But this may be too general.

If your objects have a common interface (say Foo ), you can use

Foo[] myFleet;

Also consider some Java containers, like ArrayList .

0
source

Yes ... but

Yes , you can have an array of vehicles like Bus1, Bus2 and Bike1.

... But , that does not prevent your bike from being a bike or turning it into a bus. An array is a list of references to objects; it does not accept a new copy of an object and turns it into an array type: objects will still be instances of their original class

The array can contain vehicles regardless of their subclass; it does not change them to Buses, bicycles or vehicles: the type remains the same.

However, you can try using the bike as a bus: if you agree that you will lose any attributes other than the bus. You can also use instanceof to determine if the car is already a bus or a bicycle, and convert material if necessary

ie

 for (Vehicle v: myFleet){ // For each vehicle in the fleet Bus b = (Bus) v; // Turn the vehicle into a bus b.issueTicket(1.50); System.out.println(b); } 
0
source

Provided that the bus and bike either extend the same superclass or implement the same interface, they can be placed in an array of the type of interface or superclass.

To access those, you can check if they are instances of any of the subclasses

 for (Vehicle v: myFleet){ if(v instanceof Bus){ ((Bus) v).issueTicket(1.50); } } 

However, using instanceof is not recommended, and in most cases it is better to find either an interface or a superclass that describes the general functions needed for all objects in the array.

0
source

Source: https://habr.com/ru/post/1208594/


All Articles