0

//main of programm i create a object of book and insert it into generic linked list but im unable to access all methods of book in linklist class

 public static void main(String[] args) {
 LinkedList<Book> list;
    list = new LinkedList<>();
   Book a=new Book();
   a.setAuthur("ahsan");
   a.setName("DSA");
   a.setNoOfPages(12);


   list.insertFirst(a);

}

//Linked List class

import java.util.NoSuchElementException;


public class LinkedList<AnyType> {
public class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}

/**
 * @return the data
 */
public AnyType getData() {
    return data;
}

/**
 * @param data the data to set
 */
public void setData(AnyType data) {
    this.data = data;
}

/**
 * @return the next
 */
public Node<AnyType> getNext() {
    return next;
}

/**
 * @param next the next to set
 */
public void setNext(Node<AnyType> next) {
    this.next = next;
}


}

 private Node<AnyType> head;

/*
*  Constructs an empty list
*/
public LinkedList()
{
head = null;
}




 public boolean isEmpty()
 {
   return head == null;
 }
 /*
 *  Inserts a new node at the beginning of this list.
 *  @param item to be inserted in First
 */
  public void insertFirst(AnyType item)
  {
  head = new Node<AnyType>(item, head);
  }
  /*
  *  Returns the first element in the list.
  *  @return First element in linked List
  */
  public AnyType getFirst()
  {
  if(head == null) throw new NoSuchElementException();
  return head.getData();
  }
/*
 *  Removes the first element in the list.
 *  @return Deleted First Item
 */
  public AnyType deleteFirst()
  {
    AnyType tmp = getFirst();
    head = head.getNext();
    return tmp;
  }
 /*
  *  Recursively inserts a new node to the end of this list.
  *  @param item to be inserted in last
  */
  public void insertLast(AnyType item)
  { 
       if( head == null)
        insertFirst(item);
       else
        insertLast(head, item);
  }
  private void insertLast(Node<AnyType> node, AnyType item)
  {
    if(node.getNext() != null) insertLast(node.getNext(), item);
    else
    node.setNext( new Node<AnyType>(item, null));
  }

//want to get the actual name and auther name of book here in display list method

  /*
  *   Display all the elements in linked list
  */
  public void DisplayList()
  {
    Node<AnyType> Current=head;
    while(Current!=null)
    {
        System.out.print("[ "+ Current.getData()+" ] -> ");//want to     print name of the book here
       //i do the following but not worked Current.getData().getName();
        Current=Current.getNext();
    }
    System.out.println("NULL");
  }

}

striker
  • 77
  • 8
  • Are you familiar with the [PECS](https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super) idiom? The generic nature of your class says "I can handle anything" but the DisplayList method says "I can only handle books". Which one must prevale? – Margaret Bloom Oct 08 '16 at 17:32
  • `AnyType` as its name suggest can represent any type, so it is not guaranteed that it will be Book. It can be type without `getData()` method. Simplest solution would be using method which all types have like `toString()`. Simply override this method in your `Book` class and return in it data which you want to print. – Pshemo Oct 08 '16 at 17:33
  • Thanks alot Pshemo but is there any way to call every method of book in linked list – striker Oct 08 '16 at 18:03

1 Answers1

0

Short answer: you can't.

Long answer: When you have a Node<AnyType>, all we know is that the value is some instance of AnyType. It might be a Book, or it might not. As this is something we cannot know at compile time, we cannot allow methods to be called that are in Book but not AnyType.

If your requirement is to pull books out of your list, you should make it a Node<Book>.

Joe C
  • 15,324
  • 8
  • 38
  • 50
  • Where is `toString()` coming into this? It's not mentioned either in your question or my answer. – Joe C Oct 08 '16 at 18:03
  • like there is no way of that i call the methode of class book from gen type node – striker Oct 08 '16 at 18:04
  • You can do it if it is a `Node`. You cannot if it is a `Node`. – Joe C Oct 08 '16 at 18:05
  • no no i mean to say toString() function override give me some libarty to print the book data in linked list but i still can not be able to access the methods of book as you said its not possible while im using gen type node – striker Oct 08 '16 at 18:05
  • The `toString()` method is available on every `Object` that exists in Java. You can override it in `Book` to do whatever you want. But on a more fundamental level... why does it have to be a `Node`? What do you want it to do if it is actually, say, a `Node`? You need to think about these things. – Joe C Oct 08 '16 at 18:07
  • actually i want to implement code generic for every type of object like the builtin linked list do the thing – striker Oct 08 '16 at 18:30
  • In that case, `toString()` is the way to go. – Joe C Oct 08 '16 at 18:46