3

This is a bit of a general question. I am trying to understand the concept of polymorphism while creating an efficient program or at least a working program.

The program will add, remove, search and display the plants.

Let's say I have to create a super class plant and three different plants (flowers, fungus, weed) to extend from plant.

QUESTION: I want to be able to create a plant ArrayList or Array. Is that possible? or what would be the most logical thing to do?

The code above is merely to get my point across. Not by any means correct.

class Plant{
//atributes
//constructor
//setters and getters
}


class Flower extends Plant{
//with some different attributes
}


class Fungus extends Plant{
//with some different attributes
}

class Weed extends Plant{
// with some different attributes
}


public class PlantList{
public static void main(String[] args){

//HERE is where I'm confused

ArrayList<Plant> plantList= new ArrayList<Plant>(); 


// OR

Plant plantList= new Plant[25];

plantList[0] = new Flower();
plantList[1] = new weed();
plantList[2] = new fungus();

//or completely way off?

//add()
//remove ()
//search()
//display()

}

Can someone explain me how to add the tree different types of plants to an Array or ArrayList ?

3
  • Some small consistency typos in your code there; choose one spelling of plant Commented Jun 25, 2015 at 3:12
  • @BenKnoble as I mentioned above that was not really a code that would work. I was trying to type very fast. Did not think it would offend anyone. Thanks for your help either way! Commented Jun 25, 2015 at 3:17
  • Nope no offense; its a point of pride at SO that questions (and answers) look professional. If you get a chance, you might as well clean it up since you know. Commented Jun 25, 2015 at 3:18

2 Answers 2

4

By default you can put any Object into a List, but from Java 5, Java Generics makes it possible to limit the types of object you can insert into a List.

List<Plant> list = new ArrayList<Plant>();

This List can now only have Plant instances inserted into it. And this is polymorphism between arrays.

One annoying aspect regarding this topic is that although Flower is a subtype of Plant, that doesn't mean that ArrayList<Flower> is a subtype of ArrayList<Plant>.

What you can do is

List<Plant> list = new ArrayList<Plant>();

Then

Plant flower1 = new Flower();

Plant weed1 = new Weed();

list.add(flower1);
list.add(weed1);

The point of Generics is to add the compile time type safety, so that kind of answers why the second approach is not type safety.

Sign up to request clarification or add additional context in comments.

2 Comments

what if i write this some thing like this List list = new ArrayList(); list.add(flower1); ?????
That way you have an array list that can have any kind of objects I could also add something like a String list.add("flower?") as you can see this approach is not type safe.
3

You can store references to instances of any subclass of Plant in either a Plant[] array or an ArrayList<Plant>.

An array is one way to represent a fixed-size collection. Your example illustrates how to put a Plant reference into an array.

An ArrayList<Plant> supports a collection whose size can change as needed. You can put a Plant reference into it with several methods, including the add() method:

 plantList.add( new Flower() );

As an aside, it is customary in Java to name all classes with an initial uppercase character.

5 Comments

Thank you. I do know that. I was trying to type too fast and the code was just to get my point across.
@Andy Thomas what if i write this some thing like this List list = new ArrayList(); list.add(flower1); ?????
@BilalMaqsood - Then you'd be using a "raw type." Raw types are supported for backwards compatibility with code written prior to Java 1.5. They don't support compile-time type-checking of the parameterized types. A recipient of the list could put any reference into it, not just Plant references.
@AndyThomas if i add an object in Generic arraylist then i have to cast that object or not while getting it from arrarylist ???
With a raw type, the reference you get will be of Object type. You would have to cast it if you wanted it to be a different type. This is sometimes still necessary if you're receiving a collection from older third-party code. If you own the collection, the better course is to provide type parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.