Ofir Sasson
Ofir Sasson

Reputation: 671

Java: Implementation of interfaces and functions that returns interfaces

I'm having a problem with implement interface that have a function which returns a value of a class that implement itself an interface. I have an assignment which said I need to implement those specific interfaces in this way. These are my interfaces:

public interface Something {
    int getValue();  // unique
}

public interface SomethingCollection {
    void add(Something something);
    Something removeMaxValue();
}

This is the class that implement Something interface:

public class Node implements Something {
    private int value;
    private Node next;

    public Node(int value) { 
        this.value = value;
        this.next = null;
    }

    public Node(int value, Node next) { 
        this.value = value;
        this.next = next;
    }

    public int getValue() {
        return this.value;
    }

    public Node getNext() {
        return this.next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

And this is the class that implements SomethingCollection:

public class List implements SomethingCollection {
    private Node head;
    private int maxValue;

    public List() {
        this.head = null;
    }

    public List(Node p) {
        Node n = new Node(p.getValue(), p.getNext());
        this.head = n;
        this.maxValue = this.head.getValue();
    }

    public void add(Node node) {
        if (node.getValue() > this.maxValue) {
            this.maxValue = node.getValue();
        }

        node.setNext(this.head);
        this.head = node;
    }

    public Node removeMaxValue() {
        Node current = this.head;
        Node prev = this.head;

        if (this.head == null) {
            return this.head; 
        }

        while (current != null) {
            if (current.getValue() == this.maxValue) {
                prev.getNext() = current.getNext();
                return current;
            }

            prev = current;
            current = current.getNext();
        }
    }
}

I have in List class this error: "List is not abstract and does not override abstract method add(Something) in SomethingCollection". I don't know how to fix this problem and what I'm doing wrong. How can I fix this problem?

Upvotes: 0

Views: 73

Answers (2)

Andy Thomas
Andy Thomas

Reputation: 86409

Your list does not implement void add(Something something).

Sure, it does implement public void add(Node node), and a Node is a Something. But your class implements an interface. It has to satisfy that interface. And that interface includes a method that accepts any type of Something, not just Nodes.

Here's something else to consider: do you want a Node to be a Something, or to contain a Something?

Upvotes: 4

Yuriy P
Yuriy P

Reputation: 1540

You can use generics in your collection, something like this should work:

interface SomethingCollection <T>
{
   public void add(T something);
   public T removeMaxValue();
}

class List implements SomethingCollection<Node> {
    ...
}

Upvotes: 2

Related Questions