1

this is a 2 part question.

I have two classes: Animal and Vehicle which I'll be instantiating as objects. Animal contains two instance variables: numLegs and animalName, and Vehicle contains one instance variable: numTires, both classes contain getters and setters.

I have a generic class Box that holds either an Animal or a Vehicle.

Suppose I want to create an Arraylist of boxes. Every Box in said Arraylist will hold exclusively type Animal or exclusively type Vehicle.

e.g: In the case of exclusively animals, this will look something to the effect of:

List<Box<Animal>> list = new ArrayList<Box<Animal>>(); 
list.add( new Box<Animal>( new Animal( "fluffy", 4 ) ) ); 

(1). What should the box class look like?

also, suppose I want to perform a getter on an animal from the list. Such as: System.out.println(list.get(0).getName());

(2). how would I go about this properly?

1
  • 2
    Having a class that contains both Animal and Vehicle is just confusing. This is why you are having a hard time coming up with a good solution. If the two classes having a common class then just use that. If they have nothing in common then I suggest using a different list for each. Commented May 5, 2012 at 22:18

2 Answers 2

1

I'm not exactly sure what you're after, but if you're wanting a generically typed Box class, it probably needs to look something like this:

public class Box<T extends SuperclassOfAnimalAndVehicle> {
  T myModeOfTransport;

  Box(T myModeOfTransport) {
    this.myModeOfTransport = myModeOfTransport;
  }

  public T getModeOfTransport() {
    return myModeOfTransport;
  }
}

This line: list.get(0).getName() won't do however. You'll have to get the object that Box contains. So using my example, it would be list.get(0).getModeOfTransport().getName(), assuming Vehicle and Animal both have getName() methods.

All that being said, I'm really confused about why exactly this is necessary. Perhaps if you explained the bigger picture I could give you a more complete answer.

Sign up to request clarification or add additional context in comments.

3 Comments

okay, tyvm for the response. But in this case, what would SuperclassOfAnimalAndVehnicle look like? And wouldn't it be considered unprofessional to implement a method getName() in vehicle if vehicle had no name?
@StephenSiegel Like I said, I don't really even know what Box should look like. Why does it exist? What does it do? No it's not good to add methods to objects just so they can share a superclass, but if you want a single generically typed Box class that can contain Animal and Vehicle that's what you have to do. Like I said, if you give me some more info, I can give you a better idea about what you need to do.
@StephenSiegel: It's OK to have empty abstract parent class.
0

If you want to have a list containing two or more entities with nothing in common then you will always need to specialize your code to address each of them. This Box class is completlly useless.

Object o = list.get(0);
if(o instanceof Animal) {
   System.out.println(((Animal)o).getName());
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.