Program Definition 21 ::
The abstract class Animal has abstract subclasses named Bird and Reptile. Classes Dove, Eagle, Hawk, Penguin and Seagull extend Bird. Classes Rattlesnake and Turtle extend Reptile. The ColdBlooded interface defines no constants and declares no methods. It is implemented by Reptile. The OceanDwelling interface also defines no constants and declares no methods. It is implemented by the Penguin, Seagull and Turtle classes. Define all of these classes and implement the interface as specified. Create one instance of each class. Then display all ColdBooded animals and all OceanDwelling animals. |
abstract class Animal
{
}
interface ColdBlooded
{
}
interface OceanDwelling
{
}
abstract class Bird extends Animal
{
}
abstract class Reptile extends Animal implements ColdBlooded
{
}
class Dove extends Bird
{}
class Eagle extends Bird
{}
class Hawk extends Bird
{}
class Penguin extends Bird implements OceanDwelling
{}
class Seagull extends Bird implements OceanDwelling
{}
class RattleSnake extends Reptile
{}
class Turtle extends Reptile implements OceanDwelling
{}
class Prog21
{
public static void main( String args[] )
{
Animal[] animal = new Animal[7];
animal[ 0 ] = new Dove();
animal[ 1 ] = new Eagle();
animal[ 2 ] = new Hawk();
animal[ 3 ] = new Penguin();
animal[ 4 ] = new Seagull();
animal[ 5 ] = new RattleSnake();
animal[ 6 ] = new Turtle();
for ( Animal ai : animal )
{
if ( ai instanceof ColdBlooded )
System.out.println(ai.getClass() + " is ColdBlooded" );
if ( ai instanceof OceanDwelling )
System.out.println(ai.getClass() + " is OceanDwelling" );
}
}
}
/*
D:\MCA\JAVA >javac Prog21.java
D:\MCA\JAVA >java Prog21
class Penguin is OceanDwelling
class Seagull is OceanDwelling
class RattleSnake is ColdBlooded
class Turtle is ColdBlooded
class Turtle is OceanDwelling
*/
0 comments :