class Polymorphism {
public static void main( String[ ] args ) {
if ( args[0].equals( "Animal" ) ) {
Animal myAnimal = new Animal( );
myAnimal.animalSound( );
}
else if ( args[0].equals( "Dog" ) ) {
Animal myDog = new Dog( );
myDog.animalSound( );
}
else if ( args[0].equals( "Pig" ) ) {
Animal myPig = new Pig( );
myPig.animalSound( );
}
}
}
class Animal {
public void animalSound( ) { System.out.print( "growl" ); }
}
class Dog extends Animal {
public void animalSound( ) { System.out.print( "bow" ); }
}
class Pig extends Animal {
public void animalSound( ) { System.out.print( "wee" ); }
}
|