Process by which code, method or object behaves differently under different Circumstances.
REAL
TIME EXAMPLE:
A person can play many roles in his\her life.
Lets
take women as an example,
She plays the role of
★Daughter for her parents,
★Student in her school,
★Mother for her children etc..
Types of Polymorphisms:
Ø Compile Time Polymorphism
Ø Run Time Polymorphism
ü Also called Static Polymorphism.
ü As the name indicates it happens at compile time.
ü The compiler decides what value or shape, object should take.
ü Compile time polymorphism achieved through Method Overloading or Operator Overloading.
ü It is the Compile Time Polymorphism feature in which entity has same name with multiple implementations.
Types of Overloading:
Ø Method Overloading
Ø Operator Overloading
Method Overloading:
ü In method overloading, different method have the same method name but different number of parameters.
ü If the method have the same number of parameters then the data types of the parameter will be different.
Real Time Example:
Let us consider painter who paints different objects,
class painter {
Paint(brush1, brush2, watercolor)
{
Perform oil painting;
}
Paint(round brush, flat brush, acrylic paint)
{
Perform acrylic painting;
}
Paint(pencils, paper)
{
Perform Sketching;
}
class Main{
public static void main(String[] args)
{
System.out.println(painter.paint(angular brush, tiny brush, watercolor));
System.out.println(painter.paint(round brush, flat brush, acrylic paint));
System.out.println(painter.paint(HP,A4));
}
}
ü Languages that support method overloading : C++, Java.
ü Languages that do not support method overloading : C, Python.
ü To use the class variables or class objects by redefining the meaning of operator without changing their original meaning.
ü It gives additional meaning along with their existing ones.
Example:
Additional operator "+" can be taken as an example for operator overloading.
i.e., in the following statement "+" is used to sum the two integers,
a=5+5;
In this statement,
str=str1+str2;
"+" operator used to concatenate the two strings.
Thus, "+" operator serves in two ways it is called operator overloading.
OPERATORS THAT CAN BE OVERLOADED:
Ø Unary Operator
Ø Logical Operator
Ø Dereferencing operator
Ø Binary Operator
Ø Relational Operator
Ø Bit-wise Operator
OPERATORS THAT CANNOT BE OVERLOADED:
Ø Scope Resolution Operator
Ø Conditional Operator
Ø Size of Operator
Languages that support operator overloading : C++, Python, C#
Languages that do not support operator overloading :C, Java except for "+" operator which can be overloaded in Java.
0 Comments