Results 1 to 4 of 4
  1. #1
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default 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.
    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;
    	}
    }
    Now here is the part I don't understand.
    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
    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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    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 this
    Java Code:
    //Prints the Armor toString instead of Item toString
    Search the net for polymorphism.

    db

  3. #3
    ImWithStupid is offline Member
    Join Date
    Nov 2010
    Posts
    10
    Rep Power
    0

    Default

    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    No need to be sorry. OOPS principles can take time to absorb and digest.

    db

Similar Threads

  1. invoking parent method even though child overrides it...
    By vinod_javaranch in forum New To Java
    Replies: 2
    Last Post: 08-18-2011, 04:33 PM
  2. Access Parent and child node
    By emyk in forum Advanced Java
    Replies: 0
    Last Post: 05-02-2011, 11:16 PM
  3. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  4. Casting a child class into a parent class.
    By Unsub in forum New To Java
    Replies: 7
    Last Post: 01-30-2010, 01:39 AM
  5. Parent & Child window issues......
    By jithan in forum New To Java
    Replies: 2
    Last Post: 09-20-2008, 09:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •