I have a question about the solution to the following exercise; In the main class, when I call the force () or telekinesis () methods (which are in the derived classes), I have to do several castings on a vector that declares the type of the parent class so that I can call those methods correctly, and my question is: Is there any other way to carry out what the program asks for?
Exercise: Generate a Java program that models, multiple inheritance and polymorphism the Metahuman superclass. Hero and Villain will be derived from this class. Each of them, whether hero or villain, can have at least two of the following powers: flying, speed, strength, weakness, hearing, telekinesis, telepathy; more at least one own power (unique). Model at least 2 heroes and 2 villains, who will fight using their powers. The user will enter the name of the hero or villain who wants to know what their powers are either to save or destroy.
interface PoderParticular
{
String telequinesis();
String telepatia();
}
class Metahumano
{
String velocidad()
{
return "Tiene velocidad";
}
String debilidad()
{
return "Tiene debilidad";
}
String oir()
{
return "Tiene audicion";
}
}
class Heroe extends Metahumano implements PoderParticular
{
public String telepatia(){return "";}
String velocidad()
{
return "Tiene mucha velocidad";
}
String fuerza()
{
return "Tiene mucha fuerza";
}
public String telequinesis()
{
return ":-)";
}
}
class Villano extends Metahumano implements PoderParticular
{ public String telequinesis(){return "";}
public String telepatia()
{
return "Tiene nivel 1 en telepatia";
}
String volar()
{
return "Puede volar muy alto";
}
String debilidad()
{
return "Tiene poca debilidad";
}
}
public class EjercicioClase22ago {
public static void main(String[] args) {
Scanner leer=new Scanner(System.in);
Metahumano x[]=new Metahumano[4];
x[0]=new Heroe();
x[1]=new Villano();
System.out.println("Introduzca el nombre del heroe o del villano del
que desee saber cuales son sus superpoderes ya sea para salvar o
destruir");
String personaje=leer.next();
int n=personaje.compareTo("batman");
int k= personaje.compareTo("superman");
int l=personaje.compareTo("wason");
int m= personaje.compareTo("duende");
if (n==0 || k==0)
System.out.println(x[0].velocidad()+" "+((Heroe)x[0]).fuerza()+" "+((PoderParticular)x[0]).telequinesis());
else
if(l==0|| m==0)
System.out.println(((PoderParticular)x[1]).telepatia()+" "+((Villano)x[1]).volar()+" "+x[1].debilidad());
}
}