Search code examples
javaclassobjectsubclasssuperclass

Beginning Java Polymorphism Subclass Superclass


I am trying to practice with Polymorphism and using classes. I wrote a superclass called Card. I then wrote 3 subclasses called: IDCard, CallingCard, and DriverLicense. I then wrote another class called Billfold which is supposed to contain slots for two of the cards.
I am supposed to write a BillfoldTester program which adds two objects of different subclasses to a Billfold object.

In BillfoldTester, a DriverLicense object and a CallingCard object are instantiated and added to a Billfold, which refers to these objects with Card references.

I don't really understand how to do this. I created two Card objects but I am trying to add it to my Billfold and it wont work. I tried Billfold a = new Card (x); but it's not right... Any help is much appreciated.

public class BillfoldTester
{
    public static void main (String[]args)
    {
        Card x= new IDCard("Julie", 1995);
        Card j= new DriverLicense("Jess", 1997);
  //Having trouble trying to put the objects into my Billfold and print it.
    }
}

public class Billfold extends Card
{
    private String card1;
    private String card2;

    void addCard(String Card)//Not sure if this should be String
    {
        card1=Card;
    }
}

public class Card
{

   private String name;

   public Card()
   //This is my superclass
   {
      name = "";
   }

   public Card(String n)
   {
      name = n;
   }

   public String getName()
   {
      return name;
   }

   public boolean isExpired()
   {
      return false;
   }

   public String format()
   {
      return "Card holder: " + name;
   }
}
  public class IDCard extends Card
{
    //This is one of my subclasses
    private int IDNumber;
    public IDCard (String n, int id)

    {
        super(n);
        this.IDNumber=id;
    }
    public String format()
    {
        return super.format() + IDNumber;
    }
}

Solution

  • The polymorphism example. Not sure if the functionally is exactly what you need, but you can see the whole idea (I hope). See the showAllFormat() method of Billfold class.

    The whole point is inside different format() methods of the DriverLicense and IDCard. Depending on the 'real' (or initially assigned) object the different method will be called even if you just only refer to 'Card' class.

    NOTE: You didn't provide your DriverLicense implementation, and my is just from head. I have a bit different constructor to show this sub-classes may be totally different.

    import java.util.ArrayList;
    import java.util.List;
    
    
    class Billfold {
        List<Card> list = new ArrayList<Card>(10);
    
        void addCard(Card card) // Q: Not sure if this should be String
                                // A: You would like to add a Card
        {
            list.add(card);
        }
    
        void showAllFormat() {
            // go polymorphism !...
            // when you call this general 'format()' you see the subclasses
            // 'format()' is executed, not from 'Card' class
            for(Card x: list) {
                System.out.println(x.format());            
            }
        }
    }
    
    class Card {
        private String name; /* owner */
    
        public Card() //This is my superclass
        {
            name = "";
        }
    
        public Card(String n) {
            name = n;
        }
    
        public String getName() {
            return name;
        }
    
        public boolean isExpired() {
            return false;
        }
    
        public String format() {
            return "Card holder: " + name;
        }
    }
    
    class IDCard extends Card {
        //This is one of my subclasses
        private int IDNumber;
    
        public IDCard(String n, int id) {
            super(n);
            this.IDNumber = id;
        }
    
        public String format() {
            return "(ID)" + super.format() + " " + IDNumber;
        }
    }
    
    class DriverLicense extends Card {
        private String type;
    
        public DriverLicense(String n, String type) {
            super(n);
            this.type = type;
        }
    
        public String format() {
            return "(DL)" + super.format() + " TYPE: " + type;
        }
    }
    
    public class BillfoldTester {
        public static void main(String[] args) {
    
            Card x = new IDCard("Julie", 1995);
            Card j = new DriverLicense("Jess", "AB");
    
            Billfold bf = new Billfold();
            bf.addCard(x);
            bf.addCard(j);
    
            bf.showAllFormat();
        }
    }