Note that there are some explanatory texts on larger screens.

plurals
  1. POBeginning Java Polymorphism Subclass Superclass
    primarykey
    data
    text
    <p>I am trying to practice with Polymorphism and using classes. I wrote a superclass called <code>Card</code>. I then wrote 3 subclasses called: <code>IDCard</code>, <code>CallingCard</code>, and <code>DriverLicense</code>. I then wrote another class called <code>Billfold</code> which is supposed to contain slots for two of the cards.<br> I am supposed to write a <code>BillfoldTester</code> program which adds two objects of different subclasses to a <code>Billfold</code> object. </p> <p>In <code>BillfoldTester</code>, a <code>DriverLicense</code> object and a <code>CallingCard</code> object are instantiated and added to a <code>Billfold</code>, which refers to these objects with <code>Card</code> references. </p> <p>I don't really understand how to do this. I created two <code>Card</code> objects but I am trying to add it to my <code>Billfold</code> and it wont work. I tried <code>Billfold a = new Card (x);</code> but it's not right... Any help is much appreciated.</p> <pre><code>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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload