I'm working on a project for my College and I'm stuck on this problem in my design. Right now I have something like:
public abstract class AbstractClass {
protected SomeClass attributeOne;
protected SomeClass2 attributeTwo;
public SomeClass getAttributeOne() { // code }
public SomeClass2 getAttributeTwo() { // code }
public void setAttributeOne(...) { // code }
public void setAttributeTwo(...) { // code }
}
public class ChildClassOne extends AbstractClass {
private SomeOtherClass childOneAttributeOne;
public SomeOtherClass getChildOneAttributeOne() { // code }
public void setChildOneAttributeOne(...) { // code }
}
public class ChildClassTwo extends AbstractClass {
private SomeOtherClass childTwoAttributeOne;
private SomeOtherClass2 childTwoAttributeTwo;
public SomeOtherClass getChildTwoAttributeOne() { // code }
public SomeOtherClass2 getChildTwoAttributeTwo() { // code }
public void setChildTwoAttributeOne(...) { // code }
public void setChildTwoAttributeTwo(...) { // code }
}
In another class in a higher level of abstraction, I have a Class with a
private List<AbstractClass> = new ArrayList<>();
And then when needed I add different instances of ChildClassOne and ChildClassTwo. However I need to get access to the different methods in every subclass in certain cases, but I may not know what kind of Child they are (so I can't always do a cast) and I don't want to do a switch of InstanceOf. It just feels bad. Another problem is that I have lots of subclasses, so I can't really declare an attribute for every single of them.
As possible solutions, I've seen the Adapter and the Composite pattern, but I really don't know if they are the correct approach, if there's a better solution or if my design is totally wrong.
EDIT: Yes I must use Java for this project
EDIT2: Actuale use case
I have a group of different cards, with some basic attributes. And I have two Classes containing a List of Card (in the example is only one (CardContainer) for sake of simplicity). Card is the abstract class and CardWithAction and CardWithBonus are my childs (they are actually 4 at the moment). In one of the "container" Class I only need access to the methods of the parent class (Card), so there's no problem right now. But in the other "container" I may need access to the methods of the childs, considering that most of the time I would like to treat them like a single object.