1

I have 3 classes (Car (superclass), CarToRent, and CarToSell (extend Car).

I'm making an interface (CarCompany) and want to store an ArrayList of type car in it. How do I do this? Please help?

2
  • 1
    An interface can't store anything. It doesn't have any state. It only defines behavior (methods). Commented Apr 16, 2012 at 16:26
  • 1
    Next time, please don't respond with the same comment to each poster. Commented Apr 16, 2012 at 17:01

4 Answers 4

3

In an interface, you can't set attributes because all of them have the final modifier by default, so just declare the get and set method for your interface. Let that the implementors have the attribute and overrides those methods.

public interface CarCompany {
    List<? extends Car> getCarList();
    void setCarList(List<? extends Car> carList);
}

UPDATE:

First, you shouldn't use ArrayList even if you can, instead use List interface, ArrayList implements that interface (more info about this: What does it mean to "program to an interface"?):

List<Car> carList = new ArrayList<Car>();

Second, if you have a superclass, the list attribute shouldn't be for the superclass, instead for a template that supports all that class generation. Let's see a sample of this: you have a DAO class that returns a List<CarToRent> and a List<CarToSell> in different methods. Later on, in a business class, you need to read all the elements for a List<Car> and use the price of the car for some formula. Will you create a method to convert from List<CarToRent> to List<Car> and one for List<CarToSell> to List<Car>, what if later on you need to create a new child of Car like WreckedCar for a CarWorkshop, will you create a third converter? So, instead of all that pain, you can use the List<? extends Car> that do that "dirty work" for you:

public void executeSomeFormula(List<? extends Car> carList) {
    //getting the actual VAT value (just for a sample);
    double actualVAT = getActualVATValue();
    //every object in the list extends Car, so it can be threatened as a Car instance
    for(Car car : carList) {
        car.setTotalPrice(car.getPrice() * (1 + actualVAT));
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It just says "the class CarCompany should store an ArrayList of type Car". What does this mean? Do I use your above code? Thanks so much for helping.
1

You should create an ArrayList<CarCompany> to be able to store every kind of object which comes from a class that implements your CarCompany interface.

2 Comments

It just says "the class CarCompany should store an ArrayList of type Car". What does this mean? Do I use your above code? Thanks so much for helping
@MissBee You can't store any information in an interface. An interface only defines contracts which you have to implement. Why don't you create/store/manage your cars using Factory design pattern?
0
import java.util.ArrayList;

ArrayList<TYPE> al = new ArrayList<TYPE>();
so if you need CarCompany
eg.
ArrayList<CarCompany> al = new ArrayList<CarCompany>();

Also have a look at this document about Class ArrayList

1 Comment

It just says "the class CarCompany should store an ArrayList of type Car". What does this mean? Do I use your above code? Thanks so much for helping
0

Your interface should not have a List of cars. It may define getter and setting methods for the List but should not define it. Your concrete classes that implement your interface should define the List.

public class CarCompany{
    private List<Car> myList = new ArrayList<Car>();
}

2 Comments

It just says "the class CarCompany should store an ArrayList of type Car". What does this mean? Do I use your above code? Thanks so much for helping
Yes. The above CarCompany class contains a List of Car instances.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.