Saturday 26 December 2015

What is polymorphism in java with example

Polymorphism is a feature that allows one interface to be used for a general class of actions.When one task is performed by different ways i.e. known as polymorphism.  

The behavior depends on the types of data used in the operation. It plays an important role in allowing objects having different internal structures to share the same external interface.


In java, we use method overloading and method overriding to achieve polymorphism. 




For Example 

cat speaks meaw, dog barks woof etc


Types of Polymorphism:

  1. Static Polymorphism and 
  2. Dynamic Polymorphism


1. Static Polymorphism (Function Overloading) – Function overloading if we have more than one function having same name with different prototypes (parameters) then it is also called function overloading.


2. Dynamic Polymorphism (Function Overriding) – Function overriding is just easy to say if sub class or child class has the same method or function declared in the parent class that types of method is called function or method overriding.


Declaring a method in subclass (child class) which function is already  declared in  parent class is known as method overriding.




No comments:

Post a Comment