• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using polymorphism with arraylist

 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15459
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Jon Kho
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Henry Wong
author
Marshal
Pie
Posts: 21446
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Raf Szczypiorski
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20613
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic