First you need to setup methods in each class containing the information you want to return. For example, add this to car1, car2, car3:
public String model(){
String model = "BMW";
return model;
}
public String colour(){
String colour = "Red";
return colour;
}
To be able to read between classes you need to create objects of each class.
For example add this to your Driver class:
public static void main(String[] args) {
car1 c1 = new car1();
car2 c2 = new car2();
car3 c3 = new car3();
}
Then you can add System.out.println() to print out the information contained in each classes method:
class Driver {
public static void main(String[] args) {
car1 c1 = new car1();
car2 c2 = new car2();
car3 c3 = new car3();
System.out.println("Car1 model: " + c1.model);
System.out.println("Car1 colour: " + c1.colour);
System.out.println("Car2 model: " + c2.model);
System.out.println("Car2 colour: " + c2.colour);
System.out.println("Car3 model: " + c3.model);
System.out.println("Car3 colour: " + c3.colour);
}
}
Output:
Car1 model: BMW
Car1 colour: Red
etc etc..
This seems to be a very common exercise when learning Java. I remember doing it myself so im sure this is exactly what you need.
