Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #11019 > unrolled thread
| Started by | Chad <cdalten@gmail.com> |
|---|---|
| First post | 2011-12-27 22:29 -0800 |
| Last post | 2011-12-31 06:40 -0800 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.java.programmer
Inner class and interface question (I posted some really long code) Chad <cdalten@gmail.com> - 2011-12-27 22:29 -0800
Re: Inner class and interface question (I posted some really long code) Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2011-12-27 23:27 -0800
Re: Inner class and interface question (I posted some really long code) Patricia Shanahan <pats@acm.org> - 2011-12-28 09:53 -0800
Re: Inner class and interface question (I posted some really long code) GGolf <invalid@invalid.com> - 2011-12-30 12:32 +0530
Re: Inner class and interface question (I posted some really long code) Patricia Shanahan <pats@acm.org> - 2011-12-30 05:40 -0800
Re: Inner class and interface question (I posted some really long code) Roedy Green <see_website@mindprod.com.invalid> - 2011-12-31 06:40 -0800
| From | Chad <cdalten@gmail.com> |
|---|---|
| Date | 2011-12-27 22:29 -0800 |
| Subject | Inner class and interface question (I posted some really long code) |
| Message-ID | <e787d461-ab1a-417a-9c81-0caa4d1e7ac8@x34g2000prb.googlegroups.com> |
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;
}
}
}
[toc] | [next] | [standalone]
| From | Daniel Pitts <newsgroup.nospam@virtualinfinity.net> |
|---|---|
| Date | 2011-12-27 23:27 -0800 |
| Message-ID | <IxzKq.47710$cN1.2696@newsfe12.iad> |
| In reply to | #11019 |
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
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-12-28 09:53 -0800 |
| Message-ID | <q4KdnTm5ju8NymbTnZ2dnUVZ_uudnZ2d@earthlink.com> |
| In reply to | #11019 |
On 12/27/2011 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.
If you really, really want to do this, refer to "Node" as
"LinkedList<T>.Node" in the interface declaration. You may also need to
make the inner class Node public - not needed when it is all in one
source file or package. Also, to make it any use you would have to give
Node a public getData method or similar.
However, I agree with Daniel Pitts's comments on the undesirability of
this change. A bag does not have a head. Node should be private to the
LinkedList implementation, free to change if the implementation changes.
An interface should not depend on the implementation of any one of its
implementing classes - think about writing a different implementation of
BagInterface, based for example on java.util.IdentityHashMap<T,T>. Do
you really want it to have to construct a LinkedList<T> instance so that
it can create LinkedList<T>.Node instances?
Any methods in BagInterface<T> that return elements of the bag could
return type T.
If you want a way to visit the data in the bag, consider making
BagInterface<T> extend Iterable<T> so that its implementations would
have to provide a "public Iterator<T> iterator()" method. If you do
that, visiting code for BagInterface<String> someBag would be:
for(String s : someBag){
...
}
Your printMe becomes:
public void printMe() {
for(T data : this) {
System.out.println(data);
}
}
Patricia
[toc] | [prev] | [next] | [standalone]
| From | GGolf <invalid@invalid.com> |
|---|---|
| Date | 2011-12-30 12:32 +0530 |
| Message-ID | <jdjnm7$scm$1@speranza.aioe.org> |
| In reply to | #11019 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2011-12-30 05:40 -0800 |
| Message-ID | <wqednb-RUNfGImDTnZ2dnUVZ_tOdnZ2d@earthlink.com> |
| In reply to | #11029 |
On 12/29/2011 11:02 PM, GGolf wrote: > 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. I disagree. It is very reasonable for a linked list implementation to have an inner class representing the nodes that will be linked together. I think it should remain, and indeed be made private. I do not think the interface should say anything at all about Node. There are other ways of implementing a bag that do not have any such class. The interface should deal entirely in terms of the generic type T, the payload data type. Patricia
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-12-31 06:40 -0800 |
| Message-ID | <lj7uf757ijfb2dqv5lpjnc674q8gaufial@4ax.com> |
| In reply to | #11019 |
On Tue, 27 Dec 2011 22:29:01 -0800 (PST), Chad <cdalten@gmail.com> 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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web