Write the Java statement to create the object named as lion of Animal class.
Answers
Answer:
Explanation:
Animals.java (interface):
interface Animals {
void callSound();
int run();
}
Feline.java (abstract class):
abstract class Feline implements Animals {
@Override
public void callSound() {
System.out.println("roar");
}
}
Canine.java (abstract class):
abstract class Canine implements Animals {
@Override
public void callSound() {
System.out.println("howl");
}
}
Lion.java (class):
class Lion extends Feline {
@Override
public void callSound() {
super.callSound();
}
@Override
public int run() {
return 40;
}
}
Cat.java (class):
class Cat extends Feline {
@Override
public void callSound() {
System.out.println("meow");
}
@Override
public int run() {
return 30;
}
}
Wolf.java (class):
class Wolf extends Canine {
@Override
public void callSound() {
super.callSound();
}
@Override
public int run() {
return 20;
}
}
Dog.java (class):
class Dog extends Canine {
@Override
public void callSound() {
System.out.println("woof");
super.callSound();
}
@Override
public int run() {
return 10;
}
}
Main.java:
public class Main {
public static void main(String[] args) {
Animals[] animals = new Animals[4];
animals[0] = new Cat();
animals[1] = new Dog();
animals[2] = new Wolf();
animals[3] = new Lion();
for (int i = 0; i < animals.length; i++) {
animals[i].callSound();
}
}
}