2 Replies Latest reply: Oct 8, 2006 2:59 AM by 807607 RSS

    inheritance problem

    807607
      i have the following class that creates a deck of cards using linked list and it works fine

      import java.util.LinkedList;
      import java.util.Random;

      public class CardList implements CardListInterface {
           
           private LinkedList<Card> cardlist = new LinkedList<Card>();
           private int size = 0;
           
           public CardList() {
                
           }
           
           public void add(Card c) {
                cardlist.add(c);
                size++;
           }

           public void add(Card c, int position) {
                if (position > size) {
                     System.out.println("Out of Range of List");
                }
                else {
                     cardlist.add(position - 1, c);
                     size++;
                }                         
                
           }

           public void display() {
                
                for (int i = 0; i < size; i++) {
                     System.out.print(cardlist.get(i) + " ");
                }
                System.out.println();
           }

           public Card draw() {
                if (size == 0)
                     return null;
                else {
                     size--;
                     return cardlist.removeFirst();
                }
           }
           
           public Card draw(int position) {
                if (size == 0)
                     return null;
                else {
                     size--;
                     return cardlist.remove(position - 1);
                }
           }

           public int length() {
                return size;          
           }

           public Card look(int n) {
                if (size == 0)
                     return null;
                else
                     return cardlist.get(n - 1);
           }

           public void shuffle() {
                Card temp;
                Random rand = new Random();
                for (int i = 0; i < size; i++) {
                     temp = cardlist.getFirst();
                     cardlist.removeFirst();
                     cardlist.add(rand.nextInt(size - 1), temp);
                }
                
           }

      now i want to create another class which implements this class and overrides the add() method, so i wrote this class

      public class SortedCardList extends CardList {     
           
           public SortedCardList() {
                
           }
           
           
           public void add(Card c) {          
                
                if (this.length() == 0)
                     **this.add(c);
                else
                     System.out.println("blah blah");
                
                     
           }
           
           public static void main(String[] args) {
                
                SortedCardList a = new SortedCardList();
                System.out.println(a.length());
                a.add(new Card(Card.Rank.Ace, Card.Suit.Hearts));
                System.out.println(a.length());
                a.display();
                a.add(new Card(Card.Rank.Ace, Card.Suit.Hearts));
                a.display();          
           }
      }

      but it returns a "java.lang.StackOverflowError" when i try to run the add method(i put a couple of asteriks to mark the exact line above")
      pls help.