Results 1 to 3 of 3
- 04-21-2010, 04:44 AM #1
LinkedList declared as an abstract superclass
Greetings.
I'm new to JAVA and even newer to Java Forums. I hope you don't find it annoying that my first post is a "need help" one.
Having studied the basics of the language for the last 2 months, I've been trying to solve some typical programming problems. Currently, I'm stuck in the following situation:
There's an abstract superclass with an X attribute (type double) and an abstract method, whose definition is done differently in 3 subsequent subclasses. So far, so good.
The idea is to have some structure (LinkedList ?) that can keep trace of several objects (unlimited number), whose type can be any of the 3 subclasses referred above. I would also like the structure to be sorted accordingly to the X attribute. Then, my idea is to run that structure and execute the method in each of its objects.
Since I want to have objects organised accordingly to a parameter, I think that LinkedList is the best structure to use. The problem is that I don't know if it is possible to declare such List as being of the abstract superclass type, and then add objects to it that are subclass types. I also don't have a clue of a way to sort it accordingly to a specified attribute...
I'm really not sure of anything...
Which is the best way to do such thing? I appreciate any kind of guidance.
Thanks in advance.
PS: sorry if my English is not natural. it is not my mother language.
- 04-21-2010, 01:34 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Your problem is why generics were made. The basic idea is this:
This list stores Objects, and as such, can store anything, for example:Java Code:List l = new LinkedList();
The problem is when you use the get method to retrieve an element of the list:Java Code:l.add(3); l.add(3.14); l.add("somestring");
What did l.get(0) return? You would have to do a lot of checking to find out if the returned object is an Integer, Double or String. This is where generics come in handy:Java Code:Object o = l.get(0);
With this list you know, that a returned element will always be an Integer, and the list can only store integers. In your case, you would do something like this:Java Code:List<Integer> gen = new LinkedList<Integer>();
This way, the list will only store objects of the type MySuperClass, and objects that extend your superclass, the return type will be MySuperClass. Even if the superclass is abstract, it poses no problem, as the following code is perfectly legal:Java Code:List<MySuperClass> list = new LinkedList<MySuperClass>();
So, to extend this to your case, using generic data structs would look like this:Java Code:public abstract class SuperClass { private int a; public SuperClass(int a) { this.a = a } } public class SubClass extends SuperClass { private int b; public SubClass(int a, int b) { super(a); this.b = b; } public static void main(String[] args) { SuperClass = new SubClass(1,2); //even though SuperClass is abstract, you can define a variable of this type, //and instantiate an extending subclass } }
These are the basics, I would recommend you read up on generics in Sun's Java tutorials.Java Code:List<SuperClass> list = new LinkedList<SuperClass>(); list.add(new SubClass(1,2)); SuperClass = list.get(0);
- 04-21-2010, 10:32 PM #3
Similar Threads
-
...is not abstract and does not override abstract method...
By Addez in forum New To JavaReplies: 3Last Post: 09-16-2009, 09:27 PM -
Implicit object in jsp declared where?
By mahendra.athneria in forum JavaServer Pages (JSP) and JSTLReplies: 7Last Post: 08-11-2009, 02:23 PM -
Difference between Abstract class having only abstract method and a Interface class
By Santoshbk in forum New To JavaReplies: 6Last Post: 02-11-2009, 10:51 AM -
GameImpl is not abstract and does not override abstract method
By Macca07 in forum New To JavaReplies: 2Last Post: 11-21-2008, 12:20 AM -
Error! "filename" is not abstract and does not override abstract method...
By hasani6leap in forum New To JavaReplies: 6Last Post: 10-27-2008, 12:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks