Why doesn't this work again??

Okay, it’s been a while, but I’m thinking about getting back into programming.

Anyway, This code takes the variable data from it’s own class instead of it’s child classes even when called from the child class. How do i get it so it uses the child’s variable data instead of it’s own when called by the child?



public class TestRPG1 {

	static Player hero;
	static Enemy dragon;
	
	public static void main(String[] args) {
		hero = new Player();
		dragon = new Enemy();
		
		while(dragon.hp > 0){
			int choice = (int) (Math.random() * 2);
			
			if(choice == 0)
				hero.attack(dragon);
			
			else
				hero.magic(dragon);	
		}
		
		System.exit(0);

	}

}



public class Combatant {
	int hp = 100;
	int mp = 100;
	int attack = 15;
	int magic = 25;
	int defence = 15;
	int damage = 0;
	String name = "null";
	
	public void attack(Combatant target){
		damage = (int) (Math.random() * attack);
		System.out.println(name + " attacked the " + target.name + " for " + damage + " damage!");
		target.hp -= damage;
		System.out.println(target.name + " has " + target.hp + " HP left!");
	}
	
	public void magic(Combatant target){
		damage = (int) (Math.random() * magic);
		System.out.println(name + " shot a fireball at " + target.name + " for " + damage + " damage!");
		target.hp -= damage;
		System.out.println(target.name + " has " + target.hp + " HP left!");
	}
}



public class Enemy extends Combatant{
	String name = "Dragon";
}



public class Player extends Combatant{
	String name = "Hero";
}


I think that in the way you’re doing it, you are hiding the parent data member with a child data member, of which the parent has no knowledge, so parent’s data member is called instead in parent’s method.

Try this instead, using parent’s data member to hold the information:

public class Enemy extends Combatant{
	public Enemy() { name = "Dragon"; }
}
public class Player extends Combatant{
	public Player () { name = "Hero"; }
}

EDIT: oups sorry same answer as above :slight_smile: … I should have read better

the goal ov overloading is to not redefine existing properties method when not neceessary, so you should not redefin the name string but rather initialise it in the constructor.


public class Combatant 
{
 protected String name;
}

public class Enemy extends Combatant
{
 public Enemy()
 {
  super();
  this.name="Dragon";
 }
}

IMHO you should use constructors for this and just pass the name up.


public class Combatant {
	private int hp;
	private int mp;
	private int attack;
	private int magic;
	private int defence;
	private int damage;
	private String name;
	
	public Combatant()
	{
		this( "null" );
	}
	
	public Combatant(String name)
	{
		tihs.name = name;
		hp = 100;
		mp = 100;
		attack = 15;
		magic = 25;
		defence = 15;
		damage = 0;
	}
	
	public void attack(Combatant target){
		damage = (int) (Math.random() * attack);
		System.out.println(name + " attacked the " + target.name + " for " + damage + " damage!");
		target.hp -= damage;
		System.out.println(target.name + " has " + target.hp + " HP left!");
	}
	
	public void magic(Combatant target){
		damage = (int) (Math.random() * magic);
		System.out.println(name + " shot a fireball at " + target.name + " for " + damage + " damage!");
		target.hp -= damage;
		System.out.println(target.name + " has " + target.hp + " HP left!");
	}
}

public class Enemy extends Combatant{
	public Enemy()
	{
		super( "Dragon" );
	}
}
public class Player extends Combatant{
	public Player()
	{
		super( "Hero" );
	}
}