cannot find Symbol . in generic class LinkedList

1k views Asked by At

I am new to java and coming form c++ background. i have encountered a problem while making a generic class with comparable interface. In the SearchByID and SerchByName methods of LinkedList class it gives error on temp.value.getID() and temp.value.getName(), i am making an object of Linkedlist class in main so , according to my understanding temp.value should give me an employee object for which i am calling getID which is a function of Employee class . but still it gives me this error . is there anything i understood incorrectly ? or making a mistake in this code .

cannot find symbol symbol: method getID()

public class Employee implements Comparable<Employee>
{
    private int empID;
    private String name;
    private int salary;
    private boolean manager;
    private int subordinates;

    public Employee()
    {
        empID = 0;
        name = "";
        salary = 0;
        manager = false;
        subordinates = 0;
    }



    public Employee(int id , String name , int salary , boolean manager , int sub)
    {
        empID = id;
        this.name = name;
        this.salary = salary;
        this.manager = manager;
        subordinates = sub;
    }

    public int  getID()
    {
        return this.empID;
    }


    public String getName()
    {
        return this.name;
    }

    @Override
    public int compareTo(Employee other)
    {
        if (this.empID < other.empID)
        {
            return -1;
        }
        else if (this.empID > other.empID)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    }

and this is my Linkedlist class

public class LinkedList<T extends  Comparable<T>>
{
    private int count;
    private Node<T> head;


    //completely encapsulated Node class from outer world as they dont need it
    private class Node<T>
    {
        public T value;
        public Node<T> next;


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


    }


    LinkedList()
    {
        count = 0;
        head = null;
    }



    public Node<T> SearchByID(int id)
    {
        Node<T> temp ;
        for (temp = head; temp.value.getID() != id; temp = temp.next);
        return temp;
    }


    public Node<T> SearchByname(String name)
    {
        Node<T> temp ;
        for (temp = head; temp.value.getName() != name; temp = temp.next);
        return temp;
    }
1

There are 1 answers

4
Makoto On

Remember: Node is typed as Node<T>. By default, T erases to Object. Therefore, you're attempting an invocation of the method getID() on Object, which most certainly does not exist.

If you want to ensure that you're able to get objects that have that method in, then you need to use an interface for it...

public interface IdSearchable {
    int getID();
}

... then have your elements bound to that.

public class LinkedList<T extends IdSearchable & Comparable<T>> {

}

I leave the rest as an exercise to the reader, as your second method suffers from a similar flaw.

It's better to leave the data strucutre as open as you need it to be, because without that openness, the generics become worthless as you would replace every occurrence of T with Employee instead.