Results 1 to 3 of 3
  1. #1
    box2box's Avatar
    box2box is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Your problem is why generics were made. The basic idea is this:
    Java Code:
    List l = new LinkedList();
    This list stores Objects, and as such, can store anything, for example:
    Java Code:
    l.add(3);
    l.add(3.14);
    l.add("somestring");
    The problem is when you use the get method to retrieve an element of the list:
    Java Code:
    Object o = l.get(0);
    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:
    List<Integer> gen = new LinkedList<Integer>();
    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<MySuperClass> list = new LinkedList<MySuperClass>();
    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:
    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
      }
    }
    So, to extend this to your case, using generic data structs would look like this:
    Java Code:
    List<SuperClass> list = new LinkedList<SuperClass>();
    list.add(new SubClass(1,2));
    SuperClass = list.get(0);
    These are the basics, I would recommend you read up on generics in Sun's Java tutorials.

  3. #3
    box2box's Avatar
    box2box is offline Member
    Join Date
    Apr 2010
    Posts
    3
    Rep Power
    0

    Default

    Thanks a lot, m00nchile.

    I think I get it. I'll say something when I finish writing the program.

Similar Threads

  1. Replies: 3
    Last Post: 09-16-2009, 10:27 PM
  2. Implicit object in jsp declared where?
    By mahendra.athneria in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 08-11-2009, 03:23 PM
  3. Replies: 6
    Last Post: 02-11-2009, 11:51 AM
  4. Replies: 2
    Last Post: 11-21-2008, 01:20 AM
  5. Replies: 6
    Last Post: 10-27-2008, 01:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •