class Animal{ void printMsg(){ System.out.println("I am Animal, I am the super class for all Animals"); } }
class Tiger extends Animal{ void printMsg(){ System.out.println("I am tiger"); } }
class Elephant extends Animal{ void printMsg(){ System.out.println("I am Elephant"); } }
class Lion extends Animal{ void printMsg(){ System.out.println("I am Lion"); } }
class PolyTest{ public static void main(String args[]){ Animal anim1; anim1 = new Animal(); anim1.printMsg(); anim1 = new Tiger(); anim1.printMsg(); anim1 = new Elephant(); anim1.printMsg(); anim1 = new Lion(); anim1.printMsg(); } }
Output
I am Animal, I am the super class for all Animals I am tiger I am Elephant I am Lion
As
you observe the above program, anim1 is a reference variable of class
Animal, by using this reference variable, we pointed to the objects
which are sub classes of the class Animal.
No comments:
Post a Comment