I have a question about the solution of 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. From this class will be derived Hero and Villain. Each of them, whether hero or villain, can have at least two of the following powers: flight, speed, strength, weakness, hearing, telekinesis, telepathy; plus at least one own (unique) power. 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 whose powers they want to know, 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());
}
}
The problem is that you are confusing super powers with methods of interfaces or classes. When it comes to actions, it's best if the super power is an interface and each power is an implementation of that interface. So then you can make the metahumans have two powers, which will be dynamic and depend on what you need.
Here an example.
Departure:
Castings are closely related to inheritance.
If a metahuman IS ALWAYS A human, then the metahuman class inherits from the human class.
In turn, if you need a metahuman to pass as a human to be able to do something, you can do a casting, and for that reason superman can be a journalist, doing an upcasting.
An object variable of a superclass can always store an object of that same superclass or any of its subclasses. By the law of inheritance "IT IS ALWAYS ONE".