Pages

Monday 1 April 2013

POLYMORPHISM IN JAVA

Polymorphism  is an important feature of object oriented programming. Polymorphism refers to the principle in which an object can exhibit different behaviors. Polymorphism provide improved code organization and readability as well as the creation of extensible programs. A subclass can show its own unique behavior and yet exhibit some of the functionality from its parent class.


For Example:-

public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}


Now the considered to be polymorphic since it has multiple inheritance.
we can say that.

A Deer IS-A Animal.
A Deer IS-A Vegetarian.
A Deer IS-A Deer.
A Deer IS-A Object.

Following declarations are available :

Deer d= new Deer();
Animal a=d;
Vegetarian v=d;
Object O=d;


all the reference variables d,a,v,o refer to the same Deer object in the heap.

This is how we can define polymorphism in java.

No comments:

Post a Comment