Polymorphism is a greek word meaning "many forms". The concept of polymorphism can be expressed by the phrase "one interface, multiple methods". Polymorphism is the concept that supports the capability of an object of a class to behave differently in response to a message or action.
For example. If you give 2+3, it results into 5 (the sum of 2 and 3). And if you give 'A'+'BC', it results into 'ABC', the contacenated strings. Hence the same operator ' + ' is doing two different operations (operator overloading) . This is polymorphism.
Hence polymorphism is a property by which the same message can be sent to objects of several different classes, and each object can respond in a different way depending on its class.
Method overloading and method overriding are examples of polymorphism.
Function overloading :
When several function declarations are specified for a single function name in the same scope, the function is said to be overloaded. Functions with same name but different number or types of arguments are called overloaded functions.
For example:
float divide( int a, int b);
float divide( float x, float y);
Here divide() taking two int arguments is different from divide() taking two float arguments.
This is known as function overloading.
Another example:
void calc( int i);
void calc( char c);
void calc( float f);
void calc( double d);
these are overloaded functions and each of them can be treated as separate function with different declarations.
However,
1. if the signature of 2nd function is same as of first then the 2nd is treated as redeclaration of 1st.
for ex. void sqr( int a, float b); //function 1
void sqr( int x, float y); //same signature as that of function 1
2. if the signature of the two functions match but the return types differ, the compiler will give an error.
For ex.
float square( float f);
double square( float x); //error
You can have different return types only if signatures are also different:
float square( float f); //diff signatures, hence
double square ( double d); //allowed
*Method overloading is compile time polymorphism
Method overriding :
When a method in a subclass has the same name and type signature as a method in its superclass, then method in subclass is said to override the method in superclass. When an overridden method is called from within a subclass, it will always refer to the version of that method defined by the subclass. The version of the
method defined by the superclass will be hidden.
For example:
class A
{ int i,j;
void show( )
{ System.out.println ("show in base class");
}
}
class B extends A
{ int k;
void show( )
{ System.out.println("show in sub class");
}
}
class override
{
public static void main( String args[] )
{
B subobj = new B( );
subobj.show( );
}
}
When show( ) is invoked on an object of type B, the version of show( ) define within B is used, i.e the version of show( ) inside B overrides the version declared in A.
hence here output will be:
show in sub class
*Overridden methods allow java to support run time polymorphism
However if we want to access the superclass version of an overridden function we can do this by using keyword super.
if in show( ) of sub class we use:
:
:
void show( ) //in class B
{
super.show( ); //calling the function show of its super class A
System.out.println("show in sub class");
}
:
:
:
this will call the function of superclass and hence output will be:
show in super class
show in sub class
To know more about keyword super go to the article
use of keyword super in java
If you found the article helpful please like my page................
Hi, I like that our extensive useful post. I would quickly understand our article blogs. I periodically read our blog post all content is really useful.Superb article. I read really experienced article here.
ReplyDeleteJava Training Institute in Chennai
Java Training in Velachery
you have brainstormed my mind with your excellent blog. Thanks for that !
ReplyDeleteSelenium Training in Chennai
software testing selenium training
ios developer course in chennai
Digital Marketing Course in Chennai
Selenium Interview Questions and Answers
Future of testing professional
Cloud Courses in Chennai
Cloud Computing Classes in Chennai
Excellent Blog!!! Such an interesting blog with clear vision, this will definitely help for beginner to make them update.
ReplyDeletedata analytics training in bangalore
Data Science Courses in Bangalore
Best Data Science Courses in Bangalore
data analytics courses in bangalore
big data analytics training in bangalore
Very great blog for sharing informations.
ReplyDeletemobile service center in velachery
mobile service center in vadapalani
mobile service center in porur
best mobile service center
Shweta gaur is one of the famous makeup artist in all over India. We are providing the best makeup artist courses and more other courses in over branches in chennai
ReplyDeleteMicrosoft Windows Azure Training | Online Course | Certification in chennai | Microsoft Windows Azure Training | Online Course | Certification in bangalore | Microsoft Windows Azure Training | Online Course | Certification in hyderabad | Microsoft Windows Azure Training | Online Course | Certification in pune
"I simply wanted to thank you. I do not know what would have I done, as reading this information made things so easy to understand.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery
"
good to see people talking about the addiction.and its some new point.
ReplyDeleteDigital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery