Results 1 to 4 of 4
- 08-25-2011, 06:49 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 10
- Rep Power
- 0
Need help understanding casting a child object to a parent reference
I am trying to make a simple text rpg after studying java for a bit to try and solidify what I have learned so far. I was under the impression that if you make a parent(base class) reference and then calling a child(derived) class constructor that it would cast the parent to a child. To some extent I understand the process and I am able to use the child methods but only if the parent class has that method. Here is an example.
Now here is the part I don't understand.Java Code:public class Item { private int itemLevel; private int itemType; public Item() { } public int getItemLevel() { return itemLevel; } public void setItemLevel(int itemLevel) { this.itemLevel = itemLevel; } public int getItemType() { return itemType; } public void setItemType(int itemType) { this.itemType = itemType; } public String toString() { return "I'm an Item"; } } public class Armor extends Item { private String name; private int defense; private int equipSlot; private int[] primaryStat; private int[] socket; private Enchantment enchant; public Armor() { this.name = ""; this.defense = 0; this.equipSlot = 0; this.primaryStat = new int[0]; this.socket = new int[0]; this.enchant = new Enchantment(); } public Armor(int level) { setItemType(1); this.name = ""; this.defense = 0; this.equipSlot = 0; this.primaryStat = new int[0]; this.socket = new int[0]; this.enchant = new Enchantment(); } public String toString() { String itemString = (name + "\n" + defense + "\n" + enchant); return itemString; } public void setName(String name) { this.name = name; } public void setDefense(int defense) { this.defense = defense; } }
So from what I understand then is that you can cast a derived class object onto a base object/ref but you can't use the derived class methods that the base class doesn't have. I wanted to make an array of Item references as an inventory that can hold the derived classes objects but it looks like I am going about the concept wrong. Can anyone help explain this concept?Java Code:Item item; item = new Armor(); item.setDefense(1); //Doesn't work System.out.println(item.toString()); //Prints the Armor toString instead of Item toString
- 08-25-2011, 06:59 AM #2
First, get the basic concept clear. Objects aren't cast, references (aka variables) are. The object remains unchanged by casting. All you get out of casting is a variable of an assignment-compatible type that refers to the same object.
Second, superclasses and subclasses have a is-a relationship, not a parent-child relationship. For example, a String is-a Object. It's not a son-of-Object nor a daughter-of-Object.
Now to come to your specific problem, if (and only if) all subclasses of Item must have a method setDefense(int) that maybe does different things in different subclasses, then (and only then) you could make the Item class abstract with an abstract method setDefense(int d); that concrete subclasses would have to implement. That would give you access to setDefense(int) on a Item reference.
If on the other hand all subclasses of Item don't have that method, then you're going about things in a wrong way.
Oh and about thisSearch the net for polymorphism.Java Code://Prints the Armor toString instead of Item toString
db
- 08-25-2011, 07:17 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 10
- Rep Power
- 0
Sorry about the terminology mix up, the book said that you can use super, parent, and base interchangeably. That could be leading to some of the misunderstanding. The way the examples are written in the book led me to believe what I was doing was correct. I appreciate the greater detail you gave that the book lacked. I'm going to the java website to read the tutorial on inheritance. Thank you
**Wow I'm an idiot, I re-read the passage and now it makes a lot more sense. I'm sorry that I bothered you with this.Last edited by ImWithStupid; 08-25-2011 at 07:22 AM.
- 08-25-2011, 08:57 AM #4
Similar Threads
-
invoking parent method even though child overrides it...
By vinod_javaranch in forum New To JavaReplies: 2Last Post: 08-18-2011, 04:33 PM -
Access Parent and child node
By emyk in forum Advanced JavaReplies: 0Last Post: 05-02-2011, 11:16 PM -
substract Parent class object from child class
By nikosv in forum New To JavaReplies: 0Last Post: 12-08-2010, 12:30 AM -
Casting a child class into a parent class.
By Unsub in forum New To JavaReplies: 7Last Post: 01-30-2010, 01:39 AM -
Parent & Child window issues......
By jithan in forum New To JavaReplies: 2Last Post: 09-20-2008, 09:21 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks