Using the Same Interface for Classes with Common Attributes in Java

designdesign-patternsjava

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

Link to a simplified example:

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.

Best Answer

I suggest you keep things simple and use composition instead of inheritance, as I wrote in a comment. Instead of having classes CardWithAction and CardWithBonus, create a class Action and a class Bonus, and make your Card optionally containing a reference to a Bonus object and an optional reference to an Action object.

When iterating over a container of cards, you can just check for actions like

  if(card.getAction()!=null)
  {
       // do something with card.getAction().getReward();
  }

That will avoid the need for casts or "InstanceOf". As a "bonus" (pun intended), you can now have card with an action and a bonus, in case you need that.