Search code examples
javastackabstract-classreturn-valueabstract

why can I not use methods of my extended class correctly?


I am working on a project that uses abstract classes. I have mad the class Item, and then I made a "Snickers" and a "Mars" class that both extend the Item class.

I am using it in stacks, and after I fill these stacks, I am trying to print out the name of the top, in this case, Snickers. I tried calling the getName() method directly, but it tells me that it is undeclared. When I try to use it using the super keyword like System.out.println(snickersStack.top().super().getName()) there is an error that says "void cannot be derefferenced", which I can't really understand because the method I am trying to use, is a method that returns a String.

This is the Item class:

public abstract class Item {
    private float preis;
    private String name;
    private boolean haltbar;
    
    public Item(float pPreis, String pName, boolean pHaltbar)
    {
        preis = pPreis;
        name = pName;
        haltbar = pHaltbar;
    }
    
    public float getPreis() {
        return preis;
    }
    
    public String getName() {
        return name;
    }
    
    public boolean getHaltbar() {
        return haltbar;
    }
    
    public void setPreis(float pPreis) {
        preis = pPreis;
    }
    
    public void setName(String pName) {
        name = pName;
    }
    
    public void setHaltbar(boolean pHaltbar) {
        haltbar = pHaltbar;
    }
}

which clearly has the getName() method, this is the Snickers method, which just refferences the Item class:

public class Snickers extends Item {
    public Snickers(boolean pHaltbar) { 
        super(1.2f, "Snickers", pHaltbar);
    }
}

and this is the way I fill different amounts of Items into the stack, at the bottom there is the line with my problem.

public void fuelleStacks() {
    //random int 0 - 7
    randomInt = random.nextInt(8);
    
    //fuelle snickersStack
    while(randomInt != 0) {
        randomBool = random.nextBoolean();
        Snickers snickers = new Snickers(randomBool);
        
        snickersStack.push(snickers);
        randomInt--;
    }
    
    //fuelle marsStack
    randomInt = random.nextInt(8);
    while(randomInt != 0) {
        randomBool = random.nextBoolean();
        Mars mars = new Mars(randomBool);
        
        marsStack.push(mars);
        randomInt--;
    }
    System.out.println(snickersStack.top().super().getName());
}

I have declared and initialized the stack itself in the same class, like so:

public class Automat {
    public Stack snickersStack;
    
    public Automat() {
        snickersStack = new Stack<Snickers>();
        marsStack = new Stack<Mars>();
    }
}

I did not import a Stack class, instead I have another class called Stack that contains this code (that's why I used top(), not peek() like you do with the normal Stack class):

public class Stack<ContentType> {

  private class StackNode {

    private ContentType content = null;
    private StackNode nextNode = null;

    public StackNode(ContentType pContent) {
      content = pContent;
      nextNode = null;
    }

    public void setNext(StackNode pNext) {
      nextNode = pNext;
    }

    public StackNode getNext() {
      return nextNode;
    }

    /**
     * @return das Inhaltsobjekt vom Typ ContentType
     */
    public ContentType getContent() {
      return content;
    }
  }

  private StackNode head;
  private int anzahl;

  public Stack() {
    head = null;
    anzahl = 0;
  }

  public boolean isEmpty() {
    return (head == null);
  }

  public void push(ContentType pContent) {
    if (pContent != null) {
      StackNode node = new StackNode(pContent);
      node.setNext(head);
      head = node;
      anzahl++;
    }
  }

  public void pop() {
    if (!isEmpty()) {
      head = head.getNext();
      anzahl--;
    }
  }
  
  public ContentType top() {
    if (!this.isEmpty()) {
      return head.getContent();
    } else {
      return null;
    }
  }
  
  public int getAnzahl() {
      return anzahl;
    }
}

Solution

  • snickersStack.top().super().getName() is an incorrect use of the keyword super(). super() can only be called as the first line in a constructor. In fact, you use it correctly here:

    public class Snickers extends Item {
        public Snickers(boolean pHaltbar) { 
            super(1.2f, "Snickers", pHaltbar);
        }
    }
    

    For more details, check out this documentation about super.