I'm not sure whether my title is clear enough, a bit hard for me to explain. Basically, I have this card abstract class. I also have a CreditCard subclass. In my CardSystem, I have different methods. One of them is chargeCard. What it does is simple putting some cash on my card. The thing is that I'm not sure how to do it. How can I get the chosen CreditCard using my Card class?

public abstract class Card {
    private int ID;
    public double price;
    public int next;

    public Card(double price) {
        ID = next++;
        this.price = price;
    }

    // Get method for price and ID
}

public class CredidCard extends Card {
    public static final int price_per_card = 20; 
    private int balance;
    private static int amountSold = 0;

    public CreditCard(int balance) {
        super(price_per_card);
        this.balance = balance;
        amountSold++;
    }   

    // Get method for balance and amount sold

    public void fill(int sum) {
        balance += sum;
    }
}

public class CardSystem {
    public Card[] cards = new Card[10];

    public Card findCard(int id) {
        for(int i = 0....) {
            if(card[i] != null && card.getID() == id) return card[i];
        }
    }

    public void newCard(Card c) {
        for(int i = 0; i < card.length; i++) {
            if(card[i] == null) {
                card[i] = c;
                break;
            }
        }
    }

    public CreditCard chargeCard(int id, int sum) {
        // Not sure how to do this.. 
        Card findCard = findCard(id);
        if(card != null) .... // what to do? how do I choose my CreditCard? I want to be able to use the fill method for selected CreditCard..
    }
}

you can't/shouldn't.
as far as your Card class is concerned, the CreditCard class doesn't exist.

How does it not exist?

Shari ... I'm not saying that it doesn't exist. I'm merely saying your Card class doesn't know it does.
do you think the Object class has a list in it, containing the name and members of each and every class you have written in your life?
it's the same concept.
the subclass knows that the superclass exists, and you can code it in a way that the superclass knows the sub class exists, but then you haven't really understood the point of inheritance, not to mention, you would make it very difficult on yourself.

for instance:

you put this method:

public CreditCard getCard(){
  return new CreditCard();
}

in your Card class ...
it would be impossible to compile the class, before the CreditCard class is compiled.

the subclass is supposed to inherit from the superclass, not force it to change.

Line 7 - looks like you are using next to create unique IDs? Problem is that next is an instance variable, every new instance will get its own next, equal to zero. This will work if you make next static, so there is only one of it.

Your CardSystem class is where the find happens, not in Card or CreditCard. It has an array of Cards - which can be any mixture of Cards or CreditCards because CreditCard is a subclass of Card. If you create a mixture of Cards and CreditCards and store them in your array then the find method will find the right card, regardless of its type.

Finally - you have a problem with your constructors. When you create a new CreditCard you need to be sure that its superclass's constructor is run as well so as to set up the ID and Price properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.