0

How can I use an abstract method with Overriding on two classes.? But these two classes have the same method but different parameters. Is there any solution or should I make the method not abstract and type for each class differently?

public abstract int attack(Object object); // here is my abstract method from Character class



public int attack(Hero hero){ // this method from monster class
    int currentHeroHealth;
    int reducedDamage;
    System.out.println("You have been attacked by monster!");
    int attackDamage = getAttackDamage();
    int attackSpeed = getAttackSpeed();
    int attack = attackDamage * attackSpeed;

    if(attack<hero.getArmor()){
        reducedDamage=hero.getArmor()-attack;

    }
    else{
        reducedDamage=attack-hero.getArmor();

    }
    currentHeroHealth=hero.getHealthPoints()-reducedDamage;
    hero.setHealthPoints(currentHeroHealth);
    return currentHeroHealth;

}




public int attack(Monster monster) { // this method from hero class
    System.out.println("You attacked to monster!");


    int currentMonsterHealth=monster.getHealthPoints()-HitPoints;
    if(currentMonsterHealth==0){
        System.out.println("You defeated the monster"+monster.getName());
        monster.setHealthPoints(0);
    }
    else {
        monster.setHealthPoints(currentMonsterHealth);

    }
    return currentMonsterHealth;
}
  • I think that you are overthinking it. I would modify the Character class' attack to be ``attack(Character character)`` rather than object, and I'd override the methods with the same parameter. – NomadMaker Jun 18 '21 at 08:36

2 Answers2

0

You're looking for Java generics. This is an example:

Character class

public abstract class Charachter<T> {
    public abstract int attack(T object);
}

You declare the class Character to be parametrized with a parameter of type T.

Monster class

public class Monster extends Character<Hero> {
    @Override
    public int attack(Hero object) {
        //your implementation
    }
}

You declare that your Monster class extends the base class with a type Hero, so that the parameter will be Hero.

Hero class

public class Hero extends Character<Monster> {
    @Override
    public int attack(Monster object) {
        //your implementation
    }
}

You declare that your Hero class extends the base class with type Monster, so that the type of your overridden method will be Monster.

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
0

If the base method has an Object parameter, the overritten method also needs to have an Object parameter. While subclasses can override methods with more general parameters, more specialized parameters are not allowed.

However, you can use instanceof in order to decide what method to call:

public int attack(Object object){
    if(object instanceof Hero){
        return attack((Hero)object);
    }
    if(object instanceof Monster){
        return attack((Monster)object);
    }
    //Do whatever you want to do if the object is neither a hero nor a monster
}
dan1st
  • 12,568
  • 8
  • 34
  • 67