Monday 2 March 2015

Polymorphism in Java

     Polymorphism is an OOP concept that refers to the ability of a variable,function or object to take on multiple forms. Polymorphism means many forms .

Below are the 2 types which supports polymorphism in Java
1)Method Overloading
2)Method overriding

1) Method Overloading:
       Method overloading is one of the polymorphism concept which allows 2 or more methods within the same class that share the same name and the parameter for those methods are different. 

Depending on the parameter passed at the time of calling the method it will decide which method need to be called. 

Example :
To copy the code select+ctrl+C


Output:








2) Method Overriding
 A subclass has the same method as declared in the parent class is called method overriding. Here the method name and the number of parameters are same . The implementation logic i.e the method body may change than the parent method.
Method overriding is used for runtime polymorphism. If a child class overrides the parent class method then the method in the child class will be executed else the method in the parent class will be executed.

Example:
To copy the code select+ctrl+C


Output:








No comments: