Program Definition 12 ::
The abstract Monster class has three concrete subclasses named Vampire, Werewolf and Zombie. Create six different monsters of various types and store them in a one-dimensional array. Create a loop that displays the type of each monster.
abstract class Monster
{
String name;
Monster(String n)
{
name = n;
}
abstract public void display();
}
class Vampire extends Monster
{
Vampire(String n)
{
super(n);
}
public void display()
{
System.out.println("This is Vampire :: " + name);
}
}
class Werewolf extends Monster
{
Werewolf(String n)
{
super(n);
}
public void display()
{
System.out.println("This is Werewolf :: " + name);
}
}
class Zombie extends Monster
{
Zombie(String n)
{
super(n);
}
public void display()
{
System.out.println("This is Zombie :: " + name);
}
}
class TestMonster
{
public static void main( String[] args)
{
Monster[] m = new Monster[6];
m[0] = new Vampire("vam1");
m[1] = new Vampire("vam1");
m[2] = new Zombie("zom1");
m[3] = new Zombie("zom1");
m[4] = new Werewolf("wolf1");
m[5] = new Werewolf("wolf1");
for ( Monster m1 : m)
{
m1.display();
}
}
}
/*
D:\JAVA >javac TestMonster.java
D:\JAVA >java TestMonster
This is Vampire :: vam1
This is Vampire :: vam1
This is Zombie :: zom1
This is Zombie :: zom1
This is Werewolf :: wolf1
This is Werewolf :: wolf1
*/
0 comments :