简体   繁体   中英

Override method error

I am getting the following error when I'm compiling my code...

BSLinkedList.java:10: error: BSLinkedList is not abstract and does not override abstract method push(T) in BoundedStack
public class BSLinkedList <T> implements BoundedStack<T>{
       ^
  where T is a type-variable:
    T extends Object declared in class BSLinkedList

I have tried to make sure that everything I am using is generic type T. All of the questions I've found on this topic say that people have not implemented a method from the interface, however I have implemented the method push as well as all other methods from my interface! I appreciate any help or guidance anyone can supply! Thank you!

import java.util.EmptyStackException;
import java.util.*;
/**
 * The class BSLinkedList implements the Stack ADT as described
 * in cpsc331Stack using a linked list
 *
 * @version 1.0
 */ 
public class BSLinkedList <T> implements BoundedStack<T>{
        public class StackNode<T> {
        private T value;
        public StackNode<T> next;
        private int capacity;
        public StackNode<T> top;
        private int size;

        public StackNode(T x, StackNode<T> n){
            value = x;
            next = n;
        }


        public void BSLinkedList(int capacity) {
        assert capacity >= 0;

        LinkedList<T> stack = new LinkedList<T>();
        size = 0;
        top = (StackNode<T>)null;
    }

        public boolean isEmpty(){
        assert size >= 0;
            if (size == 0){
                return true;
            }
            else{
                return false;
            }
        }

        public boolean isFull(){
            if (size == capacity){
                return true;
            }
            else{
                return false;
            }
        }

        public int capacity(){
            return capacity;
        }

        public int size(){
            return size;
        }

        public void push(T x) {
            if (isFull()){
                throw new FullStackException();
            }       
            else{
                ++size;
                top = new StackNode<T>(x, top);
            }
        }

        public T top(){
            if(isEmpty()){
                throw new EmptyStackException();
            }
            else{
                return top.value;
            }
        }

        public T pop(){
            if (isEmpty()){
                throw new EmptyStackException();
            }
            else{
                T e = top.value;
                top = top.next;
                --size;
                return e;
            }
        }

    }
}

and the interface...

/**
* The BoundedStack interface represents the Bounded Stack ADT as described in CPSC 331.
* This interface extends the interface cpsc331Stack.
*
* @version 1.0
*/
public interface BoundedStack<T> extends cpsc331Stack<T>{

    /**
     * Returns the number of elements currently on the stack.
     *
     * @return the number of elements on the stack
     */
    public int size();

    /**
     * Returns the maximum number of elements the stack can store.
     * 
     * @return the maximum number of elements the stack can store
     */
    public int capacity();

    /**
     * Tests whether the stack is full.
     *
     * @return true if number of elements in the stack is equal to 
     * the stack's capacity, false otherwise
     */
     public boolean isFull();

    /**
     * Pushes the object x onto the top of the stack.
     *
     * @param x object to be pushed onto the stack.
     * @throws FullStackException if the stack is full
     */
    public void push (T x);

}

Other implementation that is working correctly...

import java.util.EmptyStackException;

/**
 * The class BSArray implements the Stack ADT as described
 * in cpsc331Stack using an array
 *
 * @version 1.0
 */ 
public class BSArray <T> implements BoundedStack<T>{
        private T[] stack;
        private int size;
        private int top = -1;;
        private int capacity;

    /** 
     * Creates a new BSArray of size capacity
     *
     * @param capacity integer value of the maximum number of elements the array can store  
     */ 

        public BSArray(int capacity) {
            assert capacity >= 0;
            stack = (T[]) new Object[capacity];
        }
    /**
     * Tests whether or not the stack is empty.
     *
     * @return true if the stack is empty, false otherwise
     */
        public boolean isEmpty(){
            if (size >= 0){
                return true;
            }
            else{
                return false;
                }
        }
    /**
     * Tests whether or not the stack is full.
     *
     * @return true if number of elements in the stack is equal to 
     * the stack's capacity, false otherwise
     */
        public boolean isFull(){
            if (size == capacity){
                return true;
            }
            else{
                return false;
            }
        }

    /**
     * Returns the maximum number of elements the stack can store.
     * 
     * @return the maximum number of elements the stack can store
     */
        public int capacity(){
            return capacity;
        }

    /**
     * Returns the number of elements currently on the stack.
     *
     * @return the number of elements on the stack
     */
        public int size(){
            return size;
        }

    /**
     * Pushes the object x onto the top of the stack.
     *
     * @param x object to be pushed onto the stack.
     * @throws FullStackException if the stack is full
     */
        public void push(T x){
            if (isFull()){
                throw new FullStackException();
            }
            else{
                ++top;
                ++size;
                stack[top] = x;
            }
        }
    /**
     * Returns the object at the top of the stack.
     *
     * @return reference to the item at the top of the stack
     * @throws EmptyStackException if the stack is empty
     */
        public T top(){
            if(isEmpty()){
                throw new EmptyStackException();
            }
            else{
                return (T) stack[top];
            }
        }
    /**
     * Removes and returns the object at the top of the stack.
     *
     * @return reference to the item at the top of the stack
     * @throws EmptyStackException if the stack is empty
     */
        public T pop(){
            if (isEmpty()){
                throw new EmptyStackException();
            }
            else{
                assert top >= -1;
                T e = stack[top];
                stack[top] = null;
                --top;
                --size;
                return e;
            }
        }
}

cpsc331Stack interface

/**
 * The cpsc331Stack interface represents the Stack ADT as described
 * in CPSC 331.
 *
 * @version 1.0
 */
public interface cpsc331Stack<T> {

    /**
     * Tests whether the stack is empty.
     *
     * @return true if the stack is empty, false otherwise
     */
    public boolean isEmpty();

    /**
     * Pushes the object x onto the top of the stack.
     *
     * @param x object to be pushed onto the stack.
     */
    public void push(T x);

    /**
     * Returns the object at the top of the stack.
     *
     * @return reference to the item at the top of the stack
     * @throws EmptyStackException if the stack is empty
     */
    public T top();

    /**
     * Removes and returns the object at the top of the stack.
     *
     * @return reference to the item at the top of the stack
     * @throws EmptyStackException if the stack is empty
     */
    public T pop();
}   

This is a bit speculative, but I believe the problem you are seeing is because your push() method does not actually override the exact signature you specified in your interface. You have this:

public void push(T x) throws FullStackException { }

but the interface has this:

public void push (T x);

For a quick fix, you can update the interface to throw the FullStackException .

By the way, the error you are seeing is a standard Java 101 error which occurs when you extend an abstract class without overriding all the abstract methods contained within that class. So your compiler thinks that there is an abstract method push() out there which you never overrode (even though you did, at least functionally speaking).

You have implemented the methods in the StackNode inner class but it is the outer class BSLinkedList which implements the interface.

In addition, as mentioned in the previous answer the push method declares an exception that is not in the interface method signature. This will result in an different error when you fix other issue.

**Edit in your second example you are have correctly implemented the methods in the class that implements the interface (you do not have the inner class).

Is this what you are trying to achieve?

/**
 * The class BSLinkedList implements the Stack ADT as described
 * in cpsc331Stack using a linked list
 *
 * @version 1.0
 */
public class BSLinkedList<T> implements BoundedStack<T> {
    private int capacity;
    public StackNode<T> top;
    private int size;

    public void BSLinkedList(int capacity) {
        assert capacity >= 0;

        LinkedList<T> stack = new LinkedList<T>();
        size = 0;
        top = null;
    }

    public boolean isEmpty() {
        assert size >= 0;
        if (size == 0) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isFull() {
        if (size == capacity) {
            return true;
        } else {
            return false;
        }
    }

    public int capacity() {
        return capacity;
    }

    public int size() {
        return size;
    }

    @Override
    public void push(T x) {
        if (isFull()) {
            throw new FullStackException();
        } else {
            ++size;
            top = new StackNode<T>(x, top);
        }
    }

    public T top() {
        if (isEmpty()) {
            throw new EmptyStackException();
        } else {
            return top.value;
        }
    }

    public T pop() {
        if (isEmpty()) {
            throw new EmptyStackException();
        } else {
            T e = top.value;
            top = top.next;
            --size;
            return e;
        }
    }

    public class StackNode<T> {
        private T value;
        public StackNode<T> next;

        public StackNode(T x, StackNode<T> n) {
            value = x;
            next = n;
        }

    }

}

Your method signature should match exactly with the method it is overriding. So, either remove the throws Exception from push method of BSLinkedList class or add the throws exception in the push method of BoundedStack interface. Moreover, your class overrides all the methods of the BoundedStack interface. However, there may be certain methods which are defined in cpsc331Stack which you have to implement in your class. Kindly post the source code of cpsc331Stack interface here so that we can help you better

You have actually defined the methods size(),capacity(),isFull(),push() inside the innerclass "StackNode" which is defined inside "BSLinkedList".If that is what you were actually intending,then "StackNode" class should implement "BoundedStack" instead of "BSLinkedList" implementing "BoundStack",else the method should be defined inside "BSLinkedList".

`class BSLinkedList <T>{
        public class StackNode<T>  implements BoundedStack<T>{
        private T value;
        public StackNode<T> next;
        private int capacity;
        public StackNode<T> top;
        private int size;

        public StackNode(T x, StackNode<T> n){
            value = x;
            next = n;
        }


        public void BSLinkedList(int capacity) {
        assert capacity >= 0;

        LinkedList<T> stack = new LinkedList<T>();
        size = 0;
        top = (StackNode<T>)null;
    }

        public boolean isEmpty(){
        assert size >= 0;
            if (size == 0){
                return true;
            }
            else{
                return false;
            }
        }
        @Override
        public boolean isFull(){
            if (size == capacity){
                return true;
            }
            else{
                return false;
            }
        }
        @Override
        public int capacity(){
            return capacity;
        }
        @Override
        public int size(){
            return size;
        }
        @Override
        public void push(T x) {
            if (isFull()){
                throw new FullStackException();
            }       
            else{
                ++size;
                top = new StackNode<T>(x, top);
            }
        }

        public T top(){
            if(isEmpty()){
                throw new EmptyStackException();
            }
            else{
                return top.value;
            }
        }

        public T pop(){
            if (isEmpty()){
                throw new EmptyStackException();
            }
            else{
                T e = top.value;
                top = top.next;
                --size;
                return e;
            }
        }

    }
}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM