This is a beta version of Practice-It. Give us feedback

logo Practice-It logo

inheritanceMystery

Author: Marty Stepp (on 2018/05/01)

Assume that the following classes have been defined:

public class HeMan extends Skeletor {
    public void attack() {
        super.attack();
        System.out.print("heman-A   ");
    }
    
    public String toString() {
        return "heman!";
    }
}

public class SheRa {
    public void attack() {
        System.out.print("shera-A   ");
    }
    
    public void train() {
        System.out.print("shera-T   ");
    }
    
    public String toString() {
        return "shera!";   
    }
}
public class BattleCat extends HeMan {
    public void attack() {
        System.out.print("battlecat-A   ");
    }

    public void train() {
        attack();
        System.out.print("battlecat-T   ");
    }
}

public class Skeletor extends SheRa {
    public void train() {
        System.out.print("skeletor-T   ");
        attack();
    }
}

Given the classes above, what output is produced by the following code?

SheRa[] superheros = { new Skeletor(), new HeMan(), new SheRa(), new BattleCat() }; 
for (int i = 0; i < superheros.length; i++) {
    superheros[i].train();
    System.out.println();
    superheros[i].attack();
    System.out.println(); 
    System.out.println(superheros[i]); 
    System.out.println();   
}
elements[0]: Skeletor
elements[1]: HeMan
elements[2]: SheRa
elements[3]: BattleCat

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.