-
a linked list of different objects
I'm having trouble putting different kinds of Objects in a Linked list,.
I have this code:
class Entry
{
Key key;
Object next; // can point to internal nodes, leaf nodes, or data objects
public Entry(Key key, Object next)
{
this.key = key;
this.next = next;
}
public void setNext(Object obj)
{
this.next = obj;
}
public void setKey(Key key)
{
this.key = key;
}
public String toString()
{
return ("key: " + key.toString());
}
// accessors and mutators
}
in the setNext method of this class, I'm trying to send a Object of type LeafNode, and Java isn't letting me, I thought that you could send any class and as long as you accept an Object in the parameters, it would work.
This is the method used to send the LeafNode:
public static Node createRootNode()
{
Node root = new InternalNode();
root.entries[0].setNext( new LeafNode());
return root;
}
And this is the error message I get:
H:\74.215 - Object Orientation\Assignment 1\a1q2.java:171: setNext(Object) in Entry cannot be applied to (LeafNode)
root.entries[0].setNext( new LeafNode());
-
I think but i'm not sure that you have to use downcasting in somewhere
try to change this line
PHP Code:
root.entries[0].setNext( new LeafNode());
to
PHP Code:
root.entries[0].setNext( (object) new LeafNode());
try it and tell me the result>>>
good luck
-
Well, I have no problems stuffing whatever into
the Entry class. I changed the key variable to a
String, cause I don't know 'Key' is, but that has no
impact on the compile check:
Code:
public EntryTest() {
Entry en=new Entry("lala",new Vector());
en.setNext(new Integer(0));
}
So if your LeafNode is not from some obscure and unknown java basement where Object is no longer the
base class, then the problem remains as to what the
public array entries contained in Node is, and what
the relation between Node and LeafNode is.
But:
A parameter of type Object accepts any class, and
'downCasting' is a new concept in java (at least it is for me)
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks