Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: ArrayList (Inheritance)

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList (Inheritance)

    I am new to Java, I am woring on learning inheritance.

    I am creating an abstract class called Thing.

    I have to put this requirement in, Please could somebody help me with Syntax

    Thing will have the following private class field:
    • Things, an ArrayList defined with a Thing generic. This ArrayList must be maintained as Thing objects are constructed. This ArrayList must contain all unique objects for which (object instanceof Thing) is true. Each time an object for which (object instanceof Thing) is true is constructed, a reference to that object must be put in the ArrayList, if that object does not already exist in the ArrayList. The Thing equals method will be used to test if the object is not unique, i.e., already exists in the ArrayList. Your code must not useany object casts and must not use any instance of operator.



    Java Code:
     
     import java.util.*;   
    public abstract class Thing { 
    public String iName = new String();
     private ArrayList<Thing> iThings = new ArrayList<Thing>();   
     
    public static void main(String[] args) { Thing vehicle1 = new Vehicle();   }   
    public Thing(String name) {   iName = name;   }   public String getName() {   return iName; }     public abstract void show();   
    public abstract boolean equals(Thing b);   
    public void showThings() { System.out.println("Things");  
    show();  
     } 
     }

    Thank you,
    urpalshu


  2. #2

    Default Re: ArrayList (Inheritance)

    Maybe I'm a stickler for OOP, but I'll ask anyway.

    Why is there a collection of Things inside of class Thing? It's like putting a collection of Computers inside of class Computer. It's possible I could be wrong, but I don't think there are tiny computers inside my laptop...

    I think what you need is a ThingList class that has a collection of Things inside of it.

    In that case you can add a class member using the standard ArrayList definition and make sure you parameterize ArrayList with Thing (also called generics).
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList (Inheritance)

    With regards to syntax, I recommend reading the following links:
    Code Conventions for the Java Programming Language
    Lesson: Generics (The Java™ Tutorials > Learning the Java Language)

    Should you have a more specific question as to what you mean by syntax, whether you are getting compile or runtime errors, or program misbehavior, I recommend your question providing a bit more information

    Quote Originally Posted by kenster421
    Why is there a collection of Things inside of class Thing? It's like putting a collection of Computers inside of class Computer. It's possible I could be wrong, but I don't think there are tiny computers inside my laptop..
    While I can't speak for why the original poster is doing this, I do it all the time for all sorts of data structures (Trees, Graphs, etc...), although I typically wouldn't name the class 'Thing'

  4. #4

    Default Re: ArrayList (Inheritance)

    While I can't speak for why the original poster is doing this, I do it all the time for all sorts of data structures (Trees, Graphs, etc...), although I typically wouldn't name the class 'Thing'
    While I will concede that the case of a Tree is a good reason to employ this, I cannot think of any reason that a Graph should contain a collection of Graphs. If this is the functionality needed, use the Composite Design Pattern. Generally speaking (from a design perspective) an ideologically singular object like Car, should not contain a bunch of Cars within it.

    Composite pattern - Wikipedia, the free encyclopedia
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: ArrayList (Inheritance)

    Quote Originally Posted by kenster421 View Post
    While I will concede that the case of a Tree is a good reason to employ this, I cannot think of any reason that a Graph should contain a collection of Graphs. If this is the functionality needed, use the Composite Design Pattern. Generally speaking (from a design perspective) an ideologically singular object like Car, should not contain a bunch of Cars within it.

    Composite pattern - Wikipedia, the free encyclopedia
    Perhaps I'm not writing clearly enough - not a Graph containing a collection of Graphs or a Tree containing a collection of Trees - A Node containing a Collection of Nodes, the Set of all Nodes being the Tree or Graph. In the case of a Graph, often the Nodes contain collections of Edges rather than Nodes, but not always.

Similar Threads

  1. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  2. inheritance help
    By justin3492 in forum Object Oriented Programming
    Replies: 3
    Last Post: September 30th, 2010, 07:45 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. [SOLVED] Extracting an How to ArrayList from an ArrayList and convert to int??
    By igniteflow in forum Collections and Generics
    Replies: 2
    Last Post: August 16th, 2009, 01:11 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM