Inner class and interface question (I posted some really long code)

Discussion in 'Java' started by Chad, Dec 28, 2011.

  1. Chad

    Chad Guest

    I'm want to put the getHead() method in the BagInterface. However, I
    can't do this because the compiler keeps saying it can't find 'class
    Node' in Location BagInterface<T>. I guess this is because Node is an
    inner class of my LinkedList class. Ideas how to fix this? Ideally I
    want to preserve the inner class. Below is the complete working code
    in question.

    public class Main {

    public static void main(String[] args) {
    BagInterface <Integer> list = new LinkedList <Integer>();
    list.add(new Integer(1));
    list.add(new Integer(2));
    list.add(new Integer(100));
    list.add(new Integer(100));
    list.add(new Integer(100));
    list.add(new Integer(100));
    list.add(new Integer(99));
    list.add(new Integer(11));
    list.printMe();
    //System.out.println("The sum is " +
    list.sumMe(list.getHead()));

    }//end main
    }

    interface BagInterface<T> {
    public void add(T newData);
    public void printMe();
    public Node getHead(); //<---Problem Line

    }

    class LinkedList<T> implements BagInterface<T>{

    private Node firstNode;
    private int numberOfEntries;

    public LinkedList() {
    firstNode = null;
    numberOfEntries = 0;
    }

    class Node {

    private T data;
    Node next;

    Node(T newData) {
    this(newData, null);
    }

    Node(T newData, Node nextNode) {
    data = newData;
    next = nextNode;
    }

    Node getHead() {
    return firstNode;
    }
    }//end class Node


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

    public Node getHead() {
    return firstNode;
    }

    public void add(T newData) {
    Node newNode = new Node(newData);
    newNode.next = firstNode;
    firstNode = newNode;
    numberOfEntries++;
    }//end add

    public void printMe() {
    Node current = firstNode;

    while (current != null) {
    System.out.println(current.data);
    current = current.next;
    }
    }

    }
     
    Chad, Dec 28, 2011
    #1
    1. Advertising

  2. Chad

    Daniel Pitts Guest

    Re: Inner class and interface question (I posted some really longcode)

    On 12/27/11 10:29 PM, Chad wrote:
    > I'm want to put the getHead() method in the BagInterface. However, I
    > can't do this because the compiler keeps saying it can't find 'class
    > Node' in Location BagInterface<T>. I guess this is because Node is an
    > inner class of my LinkedList class. Ideas how to fix this? Ideally I
    > want to preserve the inner class. Below is the complete working code
    > in question.
    >
    > public class Main {
    >
    > public static void main(String[] args) {
    > BagInterface<Integer> list = new LinkedList<Integer>();
    > list.add(new Integer(1));
    > list.add(new Integer(2));
    > list.add(new Integer(100));
    > list.add(new Integer(100));
    > list.add(new Integer(100));
    > list.add(new Integer(100));
    > list.add(new Integer(99));
    > list.add(new Integer(11));
    > list.printMe();
    > //System.out.println("The sum is " +
    > list.sumMe(list.getHead()));
    >
    > }//end main
    > }
    >
    > interface BagInterface<T> {
    > public void add(T newData);
    > public void printMe();
    > public Node getHead(); //<---Problem Line
    >
    > }
    >

    [snip]
    Node is specific to LinkedList, and doesn't belong in Bag. As a matter
    of fact, "getHead()" doesn't make sense in Bag at all, and on top of
    that, exposing "Node" out of a collection class seems to me to be a very
    bad leaking of encapsulation
     
    Daniel Pitts, Dec 28, 2011
    #2
    1. Advertising

  3. Chad

    GGolf Guest

    Re: Inner class and interface question (I posted some really longcode)

    On 12/28/2011 11:59 AM, Chad wrote:
    > I'm want to put the getHead() method in the BagInterface. However, I
    > can't do this because the compiler keeps saying it can't find 'class
    > Node' in Location BagInterface<T>. I guess this is because Node is an
    > inner class of my LinkedList class. Ideas how to fix this? Ideally I
    > want to preserve the inner class.


    Referencing an inner class in the way you have described doesn't seem
    quite right. An interface shouldn't have any knowledge of its
    implementors. You should probably extract the class so that it's no
    longer an inner class.
     
    GGolf, Dec 30, 2011
    #3
  4. Chad

    Roedy Green Guest

    On Tue, 27 Dec 2011 22:29:01 -0800 (PST), Chad <>
    wrote, quoted or indirectly quoted someone who said :

    > public Node getHead(); //<---Problem Line


    That means "I promise to implement a getHead method in any class that
    implements BagInterface<T>

    I don't see such a method in LinkedList<T>

    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    If you can't remember the name of some method,
    consider changing it to something you can remember.
     
    Roedy Green, Dec 31, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,889
    Thomas Weidenfeller
    Oct 12, 2005
  2. Pyenos
    Replies:
    2
    Views:
    406
    Pyenos
    Dec 27, 2006
  3. Mathieu Dutour

    long long and long

    Mathieu Dutour, Jul 17, 2007, in forum: C Programming
    Replies:
    4
    Views:
    505
    santosh
    Jul 24, 2007
  4. Bart C

    Use of Long and Long Long

    Bart C, Jan 9, 2008, in forum: C Programming
    Replies:
    27
    Views:
    838
    Peter Nilsson
    Jan 15, 2008
  5. Peter Michaux
    Replies:
    1
    Views:
    101
    Peter Michaux
    Jun 1, 2007
Loading...

Share This Page