Superclass Arraylist, Subclass


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Superclass Arraylist, Subclass

  1. #1
    Join Date
    May 2005
    Posts
    2

    Superclass Arraylist, Subclass

    Hi

    I came across this forum and was wondering if i could be enlightened!

    This is what i am currently doing, is it bad programming? could you sudgest a better approach?

    I have a superclass which has a method to return an ArrayList "list1":

    public abstract class A
    {
    private ArrayList list1;

    public ArrayList method1()
    {
    return list1;
    }
    }

    my subclass uses method1 from superclass to access the arraylist from the superclass A and print the contents from it:

    public class B extends A
    {
    public void printlist1()
    {
    Iterator it = method1().iterator();
    while(it.hasNext())
    {
    system.out.println(it.next());
    }
    }
    }

    Any help on this subject would be great
    Thanks alot

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    No, this is a clear, clean way of doing it...well.. apart from the fact that your
    class A is not abstract, - you'll have to declare ite like:
    Code:
    public abstract class A {
    private ArrayList list1;
    
    public abstract ArrayList method1();
    
    }
    ... and...using an abstract class (or as in this case a
    an ordinary class "posing" as an abstract and nervously awaiting the compiler)
    for this is an overkill, even as an example of abstract class usage

    A good example of abstract class usage is, in my opinion, the problem of
    coding an animation that utilizes several classes for rendering. There may
    be polygons, images, animated icons, container objects ++. A useful
    abstract class for this could be like:

    Code:
    public class Playground extends JPanel implements MouseListener {
      public vObjects=new ArrayList();
      private void deadList=new ArrayList();
      // (constructors, initializers, setters, getters)
      // .
      // .
      // invoked by animation thread
      public void doTick() {
        deadList.clear();
        for (int i=0; i<vObjects.size(); i++) {
          VisibleObject vo=(VisibleObject)vObjects.get(i);
          if (!vo.doMove()) {
            deadList.add(vo);
          }
          repaint():
        }
      }
      public void update (Graphics g) {
        for (int i=0; i<vObjects.size(); i++) {
          VisibleObject vo=(VisibleObject)vObjects.get(i);
          vo.draw(g);
        }
      }
      public void paint(Graphics g) {
        update (g);
      }
      public void mousePressed (MouseEvent e) {
        for (int i=0; i<vObjects.size(); i++) {
          VisibleObject vo=(VisibleObject)vObjects.get(i);
          if (vo.isHit(e.getPoint()) {
            // do the hit stuff
          }
        }
      }
    }
    public abstract class VisibleObject extends Polygon {
        public abstract void draw(Graphics g);
        public abstract boolean doMove();  // false=can't move
        public boolean isHit (Point p) {  // the mouseclick 
          return this.contains(p);
        }
    }
    Last edited by sjalle; 05-15-2005 at 09:54 AM.
    eschew obfuscation

  3. #3
    Join Date
    May 2005
    Posts
    2
    cheers for the reply

    I understand the bit about abstract methods now! cheers

    However have you had any experience in using an arraylist from a parent class. Is the way i did it the normal way? How do you implement it.

    thanks alot for your help
    Cheers!

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    What the "normal" way of doing it is...? Hmm, I don't think there is a 100% normal
    way, and rigid coding standards are, in my opinion anti-creative. I prefer the
    pragmatic approach, - that combines ease of maintenance (modification/enhancement)
    and small code size. In other words; voluminous code that may appear kind of
    stupid and repetitive may be far easier to maintain than the "clever" approach that
    uses a deep-levelled and hardboiled algorithm.

    I've used araylists like you've done here with getter methods, and passed them around
    like public variables (classInstance.theList.get(i)). A good rule of thumb is to think
    like: "how would this appear to me/my collegues three years from now, when this
    entire problem complex is forgotten ?"
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center