0

I have a java app which has the following classes: Car and Phone. Each class has the method connectToBluetooth(), which prints a message regarding the class' connection to bluetooth.

In another class, I want to create an array of objects in which I add the instances of each one of the objects already created. Then, I want to access the connectToBluetooth method coresponding to each instance. I have created one instance for each class:

I want to create an array of these two instances and to access the connectToBluetooth method coresponding to each class. (The constructors ask for the owner and the device's color )

    Car car = new Car("John", "red");
    car.connectToBluetooth();

    Phone phone = new Phone("Susan","black");
    phone.connectToBluetooth();
6
  • Please post what you tried so far Commented Dec 22, 2018 at 15:19
  • i guess that the thing you need is to define a common interface for both kind of classes an itetare against an array of those interfaces invoking the connect to BlueTooth. Teel me if that gives you an idea Commented Dec 22, 2018 at 15:19
  • @Victor, I have already created an interface, and in both classes I have implemented that interface. Commented Dec 22, 2018 at 15:22
  • that's ok luli, that would be a second step (first is defing the interface, second is to implement it). Now in a thirdh step you have to use it. Can you share more of your code? Commented Dec 22, 2018 at 15:25
  • @Victor, I have posted the solution, thank you! Commented Dec 22, 2018 at 15:28

3 Answers 3

2

Object[] arr = new Object[]

You could use array of Object, but in this case you have to case concrete instance before invoke connectToBluetooth():

Object[] arr = { new Car("John", "red"), new Phone("Susan","black") };

for (Object obj : arr) {
    if (obj instance of Car)
        ((Car)obj).connectToBluetooth();
    else if (obj instance of Phone)
        ((Phone)obj).connectToBluetooth();
}

Bluetooth[] arr = new Bluetooth[]

More correct way is to declare an interace with connectToBluetooth() method and use it as array type:

interface Bluetooth {
    void connectToBluetooth();
}

class Car implements Bluetooth {} 
class Phone implements Bluetooth {}

Bluetooth[] arr = { new Car("John", "red"), new Phone("Susan","black") };

for (Bluetooth bluetooth : arr)
    bluetooth.connectToBluetooth();
Sign up to request clarification or add additional context in comments.

Comments

1

You can create an array of Objects for where you can add both Object types to it which is not a good way. The better way is to create a super type for Phone and Car and create a array of that Type (super type can be an interface or a class).

For an example create a class called BlueToothDevice and extend that class to both Phone and Car. Then create an array of BlueToothDevice type and add both of them.

3 Comments

Yes, You got it :)
If BlueToothDevice will never be instantiated then I would rather create an Interface
True. An Interface would be better. Just took it as an example to explain him and I have mentioned super type can be an interface or a class.
1

This is the actual solution, where Bluetooth is the interface implemented:

Bluetooth[] bluetooth= new Bluetooth[2];
bluetooth[0] = new Car("John", "blue"); 
bluetooth[0].connectToBluetooth(); //-> prints message coresponding to Car class 
bluetooth[1] = new Phone("Susan", "black");
bluetooth[1].connectToBluetooth(); //-> prints message coresponding to Phone class

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.