aspose file tools*
The moose likes Java in General and the fly likes Using polymorphism with arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Using polymorphism with arraylist" Watch "Using polymorphism with arraylist" New topic
Author

Using polymorphism with arraylist

Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Dear all, this topic is related to the earlier topic(which can be found here)which I started last Friday but it does not seem to have any replies..

Currently, I have total of three classes which are Item, Video and Game. I have learn from here how polymorphism works with Arraylist.. but the problem that i am facing now is that I am unable to print it out to an table even though i can print it out to the console with the example Zoo with animals but I have spent alot of time researching it since last Friday and still I am unable to solve it with my friends' help..
This is part of the Java assignment that I have done



There are the codes for the two classes that I used for polymorphism.


There are the part of the codes that I have used for array list
private static ArrayList<Item> items;
items = new ArrayList<Item>(); // declared in an method


Currently, I can find examples on Arraylist and Polymorphism but it is able to print on console..
I would sincerely hope that someone would help me on getting the " g.getManufacturer()" to work which is the attribute from a child class.

Any comments or help on this topice would be great!!

Regards,
Jon
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

String manu = g.getManufacturer(); // this will appear the error of "cannot find symbol" and "incompatible type"

That is because g is an Item and not a Game, and the method getManufacturer() is in class Game. Change line 7 in your first piece of code to this:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Hi Jesper,

Thank you for replying this topic.. In fact I did it earlier but it gave me incompatible problem.. to be more precise, I have upload the screenshot for viewing..

By right, i should put Item instead of Game and in theory it should work.. but it does not! This is the part I am very puzzled it seems what i have learned in class and online is wrong!!

Regards,
Jon
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Dear Mods,

Is this Topic suitable in intermediate level? or it should be transferred to "Java in General (advanced)" section instead?

This is the first time that I tried to resolve such question using up to 7 days and there is no progress.

Regards,
Jon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Jon Kho wrote:Dear Mods,

Is this Topic suitable in intermediate level? or it should be transferred to "Java in General (advanced)" section instead?

Definitely not. It's a simple case of trying to call a method that's declared in class Game on a reference variable of type Item. Since items is an ArrayList<Item>, you'll need to cast:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

Jon Kho wrote:Dear Mods,

Is this Topic suitable in intermediate level? or it should be transferred to "Java in General (advanced)" section instead?

This is the first time that I tried to resolve such question using up to 7 days and there is no progress.

Regards,
Jon


Jon,

No offense. But I don't think this even belongs in intermediate. Intermediate and advanced are used to solve harder Java programs.... failure to compile a program is hardly anything but a beginner's problem.

[EDIT: was going to follow up with answer, but Rob already beat me to it]

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raf Szczypiorski
Ranch Hand

Joined: Aug 21, 2008
Posts: 383
That's because you assign elements to an Item, and Item is more general than Game, and doesn't define the getManufacturer() method. You can use a cast and an instanceof check, but here, as you have an ArrayList<Game> simple assign to Game in the loop and it will work.

Also, if it should be transferred, it should go not to advanced, but to beginners section.

Cheers.
Manjunatha Murthy
Greenhorn

Joined: Jul 10, 2008
Posts: 10
Hi,
As Jesper earlier pointed out g is declared as Item. Even though items contain Games' because of the declaration for g as Item, getManufacturer is not identified as part of g.

Please change your for loop as


Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Dear all,

Thank you very much for your comments especially Rob.

I would like to touch on more about casting and instanceof.. As I have attended the lecture for Java after the day which this topic was created, the lecturer has taught more about these two uses.

using Rob's example..


Regards,
Jon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Jon Kho wrote:Casting is useful if you want to use one of the attrubites from the child class to parent (In theory, the child should inherit all the parent's attributes and method but in actual programming you have to define it //using casting)

Actually, you need a cast when your object is referenced by a parent type reference but is actually an instance of the child class. If the latter is not the case you will get a ClassCastException:
You are right about the inheritence of all attributes and methods; you cast to be able to use the attributes and methods that are not defined in the parent but only in the class. In your example, the cast is only necessary for getManufacturer() - getItemId() and getTitle() can be called without the cast.

You may want to read this: http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html
Jon Kho
Ranch Hand

Joined: Jun 03, 2009
Posts: 54
Rob Prime wrote:
Jon Kho wrote:Casting is useful if you want to use one of the attrubites from the child class to parent (In theory, the child should inherit all the parent's attributes and method but in actual programming you have to define it //using casting)

Actually, you need a cast when your object is referenced by a parent type reference but is actually an instance of the child class. If the latter is not the case you will get a ClassCastException:
You are right about the inheritence of all attributes and methods; you cast to be able to use the attributes and methods that are not defined in the parent but only in the class. In your example, the cast is only necessary for getManufacturer() - getItemId() and getTitle() can be called without the cast.

You may want to read this: http://java.sun.com/docs/books/tutorial/java/IandI/subclasses.html


Thanks for the comment Rob!

I will read that document once I(or should I say the people in JavaRanch to give me some tips, hopefully) am able to clear last final "bug" regarding transferring arraylist's .size..
 
wood burning stoves
 
subject: Using polymorphism with arraylist