Thursday, 25 June 2015

Polymorphism


Polymorphism

Polymorphism is a feature that allows one interface to be used for a general class of actions. It’s an operation may exhibit different behavior in different instances. 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. Polymorphism is extensively used in implementing inheritance.

Types of Polymorphism

1) Static Polymorphism
2) Dynamic Polymorphism

Static Polymorphism:
              Function Overloading – within same class more than one method having same name but differing in signature.
Resolved during compilation time.
Return type is not part of method signature.

Dynamic Polymorphism
                 Function Overriding – keeping the signature and return type same, method in the base class is redefined in the derived class.
Resolved during run time.
Which method to be invoked is decided by the object that the reference points to and not by the type of the reference.

Overriding:
      Redefining a super class method in a sub class is called method overriding.
The method signature ie. method name, parameter list and return type have to match exactly.
The overridden method can widen the accessibility but not narrow it, ie if it is private in the base class, the child class can make it public but not vice versa.

Overriding Examples:
          Consider a Super Class Doctor and Subclass Surgeon. Class Doctor has a method treatPatient(). Surgeon overrides treatPatient method ie gives a new definition to the method.

Doctor doctorObj = new Doctor();
// Call the treatPatient method of Doctor class
doctorObj.treatPatient()
Surgeon surgeonObj = new Surgeon();
// Call the treatPatient method of Surgeon class
surgeonObj.treatPatient()
Doctor obj = new Surgeon();
// calls Surgeon’s treatPatient method as the reference is pointing to Surgeon
obj.treatPatient();
Method/Function Overloading:
Method Overloading refers to the practice of using the same name to denote several different operations. Overloading can be done for both functions as well as operators. Here we look at only Method overloading.
Declaration:

void SomeMethod (int value);
void SomeMethod (float value);
void SomeMethod (char value);
void SomeMethod (String* str);
void SomeMethod (char* str);

           All the five methods are called ‘SomeMethod ’. All the methods have the same name, but different signatures.

The concept of the same function name with different types of parameters being passed is called Function Overloading.
1) In Overloading we can reuse the same method name by changing the arguments.
2) Overloaded methods- Must and Must Not Facts:

The Overloaded method must have different argument lists,
Can have different return types but in that case it is mandatory to have different argument list.
Can have different access modifiers and
Can throw different exceptions
3) Methods can be overloaded in the same as well as the sub classes.

Q: What determines which overridden method is used at runtime?
A: Object type
Q: What determines which overloaded method will be used at compile time?
A: Reference type determines. Operator overloading refers to the operators like ‘+’ being used for different purposes based on the data type on either side of the operator.

No comments:

Post a Comment