Abstract Class Polymorphism?

I want to be able to add any of my sub classes to a collection (hash set) , without duplicating the code. The superclass for all these subclasses is an abstract class.

Is there a possible way of achieving this?

At the moment I have methods for each individual class; before making the parent class abstract, I could use the same method for all, but was advised as I did not want an instance of that parent class, the abstract class was best.

Hope this is not too vague or abstract... :)

[511 byte] By [Craigusa] at [2007-11-26 18:47:25]
# 1

I think if you use the wildcard symbol it'll allow you to add a covariant to the collection, for example

public static void addSub(List<? super NameofSuperClass> varName)

When you can to retrieve the data, if you want any methods not in the super class you would still be to cast it.

Is this what you meant? - Peter sorry if it wasn't helpful

cabbagesa at 2007-7-9 6:21:23 > top of Java-index,Java Essentials,Java Programming...
# 2

public abstract class Abs

{

public abstract void doStuff();

//...

}

public class Sub1 extends Abs {/*...*/}

public class Sub2 extends Abs {/*...*/}

//...

List<Abs> list = new ArrayList<Abs>();

list.add(new Sub1());

list.add(new Sub2());

for (Abs a : list)

a.doStuff();

#

duckbilla at 2007-7-9 6:21:23 > top of Java-index,Java Essentials,Java Programming...