-4

I'm working on a task given off of a textbook . I can't call out the "poisonAttack" method from the same class. Would appreciate if anyone can give me feedback.

public class PoisonMatango   extends Matango {
    PoisonMatango  pm = new PoisonMatango ('A');


    public PoisonMatango ( char suffix) {
        super(suffix);


    }
    // The method I am trying call.
    public void  poisonAttack(Hero h) {
        super.attack(h);
        int poisonCount = 5;
        if ( poisonCount >=0 ) {
            System.out.println("The enemy had spread poisonous pollons");
            int pollenDamage = h.hp / 5;
             h.hp-= pollenDamage;
            System.out.println("Hero has received " + pollenDamage + "damage from " );
            poisonCount --;
        }else
        {
            System.out.println("No additional attack were made since poisonCount= 0");
        }}

    }
  • Possible duplicate of [Java Inheritance - calling superclass method](https://stackoverflow.com/questions/6896504/java-inheritance-calling-superclass-method) – Uzair A. May 04 '18 at 10:03
  • 3
    `instance.methodName([parameter1][, parameter2][, ... parameterN]);`... but this should be explained in your book or change it... – AxelH May 04 '18 at 10:04

1 Answers1

0

You have to use Class object 'pm' created to call method and pass required parameter as per method definition. Your code is having Object type param of class Hero

pm.poisonAttack(hr);

Below is solution for above code:-

// Class Object used as param in solution public class Hero {

int hp = 100;

}

// Super Class public class Matango {

Hero a;

public Matango(Hero suffix) {
    this.a =suffix;
}

// Super Class Method 
public void attack(Hero h){
    System.out.println("\n\nHero hp var value::"+h.hp);
}

}

// PoisonMatango

public class PoisonMatango   extends Matango {


    public PoisonMatango ( Hero suffix) {
        super(suffix);


    }
    // The method I am trying call.
    public void  poisonAttack(Hero h) {
        super.attack(h);
        int poisonCount = 5;
        if ( poisonCount >=0 ) {
            System.out.println("The enemy had spread poisonous pollons\n");
            int pollenDamage = h.hp / 5;
             h.hp-= pollenDamage;
            System.out.println("Hero has received " + pollenDamage + "damage from \n" );
            poisonCount --;
        }else
        {
            System.out.println("No additional attack were made since poisonCount= 0 \n");
        }}


    public static void main(String args[])
    {
        // Create Object of Param class, this example passes object but we can pass simple data type as per method definition.
        Hero hr = new Hero();
        PoisonMatango  pm = new PoisonMatango (hr);

        pm.poisonAttack(hr);

    }

    }