user16655
user16655

Reputation: 1941

Generic linked list - how to add different objects

I have created a linked list, but I can't seem to understand how to insert different objects into the same list. Let's say I have a list which I want to contain both objects of type cat and dog, is this possible? I know how to insert only cats, or only dogs, but I can't seem to understand how to insert both into the same list.

Upvotes: 1

Views: 2000

Answers (5)

Reut Sharabani
Reut Sharabani

Reputation: 31339

You want to use their "lowest" common class, or a common interface, so you can tell as much as possible (and what you need) about list's elements.

If both classes Dog and Cat extend the class Animal, and you want to represent a list of animals, you can do:

List<Animal> animals = new YourList<Animal>();
for (Animal animal: animals){
    // use some Animal method
    animal.eat();
}

If both of them implement the interface TailOwner and you want to use operations from that interface on members of the list, you can do:

List<TailOwner> tailOwners = new YourList<TailOwner>();
for (TailOwner tailOwner: tailOwners){
    // use some TailOwner method
    tailOwner.wiggle();
}

If you're not sure, you can always fall back to Object, as all Java classes extend it:

List<Object> objects = new YourList<Object>();
for (Object object: objects){
    // use some Object method
    System.out.println(tailOwner.toString());
}

Usually, when you have two objects in the same list, they do have something in common.

Either way, you're giving up any Cat-specific or Dog-specific methods (before casting back to either) when you put them in the list.

Having a good hierarchy can prevent casting, and promote polymorphism, when you use objects from the list. All you can tell about objects retrieved from the list at compile time, is that they at least comply to the list's generic type.

Upvotes: 2

Michel Keijzers
Michel Keijzers

Reputation: 15357

Since your list probably contains elements that are somehow related, it would be best to make an interface and use that as list object type.

Derive both Cat and Dog from IAnimal and make the list of type

List<IAnimal> animals = new YourList<IAnimal>();

Upvotes: 1

H&#233;ctor
H&#233;ctor

Reputation: 26054

You only can add elements that implements same interface or extends same class.

class Animal {}

class Cat extends Animal{}

class Dog extends Animal{}

List<Animal> animals = new LinkedList<Animal>;
animals.add(new Cat());
animals.add(new Dog());

OR

interface Animal {}

class Cat implements Animal{}

class Dog implements Animal{}

List<Animal> animals = new LinkedList<Animal>;
animals.add(new Cat());
animals.add(new Dog());

Upvotes: 1

Marcin D
Marcin D

Reputation: 916

I would be cautious when using the word generic, especially since cat and dog are not generic.

LinkedList<Object> linkedlist = new LinkedList<Object>();
linkedList.add(new Dog());
linkedList.add(new Cat());

Upvotes: 0

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

If Cat and Dog have nothing in common, you can parameterize the LinkedList with Object:

List<Object> list = new LinkedList<Object>();

This way you will be able to add both Cat and Dog, but please note that this may force you to check the types every time you retrieve the objects from the list.

Upvotes: 1

Related Questions