• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem trying to get Inheritance/Polymorphism right  RSS feed

 
David Pacsuta
Greenhorn
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

first of all, thanks in advance to anyone who takes a look at this problem.
For my amusement and to practice Java (more specifically inheritance and polymorphism) I created an "Item" superclass that has a direct subclass "Gear" which is also a superclass to "Armor".
I'm not really trying to create an RPG game, I just used this idea to have something to practice on. My problem is that when I create an array of "Item" type and then I try to fill it up with "Armor" objects, the compiler throws back all kinds of errors (please see below the code and the command prompt output). On the other hand, creating and operating on a single "Armor" object works perfectly.

Sorry for the long post!

Code for the superclass "Item":
Code for the class "Gear":
Code for the class "Armor":
Code for the tester class "TestArmor":
Not sure if I should put the errors in code tags but I'll do so anyway:

Regards,
David
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You have the inheritance/polymorphism hierarchy lay out correct. In your code however, you are initializing an array of Items. And each index in the array is an Item rather than a Gear or Armor.

To make things simpler, I suggest you use an ArrayList rather than an array. Because you know an Armor is a Gear is an Item so you can add an Armor or Gear to a list of Items.



 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of items[0].setItemLevel(75);, you can use casts to make it look like: ((Gear) items[0]).setItemLevel(75);
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

One of the advantages is the ability to write more general code -- meaning that you can write code that doesn't know about gear or armor, and still use it for gear or armor.

In this case, you code only creates armors. It also assumes that it is armors, and calls methods of it. Why not just create an array of armors?

Henry
 
David Pacsuta
Greenhorn
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies!
I'll post the results as soon as I have a chance to apply the changes.

I wish you a nice evening!
 
David Pacsuta
Greenhorn
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
In this case, you code only creates armors. It also assumes that it is armors, and calls methods of it. Why not just create an array of armors?


You are absolutely correct Henry, and I intend to add more classes such as "Weapon" extending "Gear" and "Misc" (miscellaneous) extending "Item".
I merely wanted to see if what's later going to be real polymorphism would work if tested only with the Armor class. But it is true that I will have to work on some more generalised methods.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Pacsuta wrote:I merely wanted to see if what's later going to be real polymorphism would work if tested only with the Armor class. But it is true that I will have to work on some more generalised methods.

It's also worth remembering that polymorphism doesn't only apply to class hierarchies, but also to interfaces. Class hierarchies tend to be quite brittle, and you may find you're hamstringing yourself by relying purely on a structure based on extends. You may find this article worth reading.

Winston

 
David Pacsuta
Greenhorn
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
It's also worth remembering that polymorphism doesn't only apply to class hierarchies, but also to interfaces. Class hierarchies tend to be quite brittle, and you may find you're hamstringing yourself by relying purely on a structure based on extends. You may find this article worth reading.


I am currently reading the book "Head First Java Second Edition" (amazing book and also the reason I found this forum) and it's funny I laid down the book yesterday at the beginning of the chapter that covers interfaces I think it might be a good idea for me to read it first along with the above mentioned article and then get back to practicing polymorphism. I'll still see if the previous ideas can solve my current problem though.
 
David Pacsuta
Greenhorn
Posts: 8
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I checked all the previous answers but unfortunately none of them solved my problem. Something slipped my attention while I was reading the chapter on inheritance/polymorphism: when using a superclass reference to invoke a method on a subclass reference, during compilation the compiler will only look for the requested methods in the superclass so they must be present even though they are not technically used there. At runtime the correct methods will be used anyway (the lower level methods in the subclasses). So I collected all the methods from the subclasses (Gear and Armor) that weren't already in the superclass and added a "raw" version of them (keeping argument and return types) to "Item" as you can see below after which everything started to work as intended:


I will mark this thread as solved.

EDIT: I just picked up the book again and found out I could also use abstract methods instead of the above code (of course only after I've marked the class abstract as well):
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Pacsuta wrote:I checked all the previous answers but unfortunately none of them solved my problem. Something slipped my attention while I was reading the chapter on inheritance/polymorphism: when using a superclass reference to invoke a method on a subclass reference, during compilation the compiler will only look for the requested methods in the superclass so they must be present even though they are not technically used there. At runtime the correct methods will be used anyway (the lower level methods in the subclasses). So I collected all the methods from the subclasses (Gear and Armor) that weren't already in the superclass and added a "raw" version of them (keeping argument and return types) to "Item" as you can see below after which everything started to work as intended:


I will mark this thread as solved.

EDIT: I just picked up the book again and found out I could also use abstract methods instead of the above code (of course only after I've marked the class abstract as well):


Your solution makes no sense -- how does moving the subclasses method to the superclass better here? What if you add an new item subclass that is *not* armor? Should you be able to call the armor methods? Or does item needs to have more methods? And what happens to armor when you add those non-armor methods to item?

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!