Monday, 19 March 2012

Polymorphism


Polymorphism:

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
Any java object that can pass more than on IS-A test is considered to be polymorphic. In Java, all java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.
It is important to know that the only possible way to access an object is through a reference variable. A reference variable can be of only one type. Once declared the type of a reference variable cannot be changed.

Example:
interface Vegetarian{}
class Animal{}
class Deer extends Animal implements Vegetarian{}
 
There are two types of polymorphism
1. Static polymorphism
2. Dynamic polymorphism 
 
 Examples for these two types can be explained below i.e. method overloading and method overriding.
 
Abstraction:
Abstraction refers to the ability to make a class abstract in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, and its fields, methods, and constructors are all accessed in the same manner. You just cannot create an instance of the abstract class.
If a class is abstract and cannot be instantiated, the class does not have much use unless it is sub classed. This is typically how abstract classes come about during the design phase. A parent class contains the common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own.

No comments:

Post a Comment