Polymorphism In OOP: What Is Polymorphism [Detailed Explanation] The Most Trending Thing Now?
_11zon%20(2).png)
What is Polymorphism:
_11zon%20(2).png)
- Polymorphism is the cap potential of an item to take many forms.
- The maximum not unusual place use of polymorphism in OOP takes place whilst a determined elegance reference is used to consult an infant elegance item.
- An idea via way of means by which we will carry out an unmarried motion via the form of means of one-of-a-kind ways.
- Polymorphism is derived from 2 greek words: poly and morphs. The word "poly" manner many and "morphs" manner forms. So polymorphism manner caps the potential to exist in lots of forms.
- Polymorphism offers programmers the benefit of writing bendy code.

Types of Polymorphism:
In java polymorphism commonly takes place in sorts
- Compile time polymorphism(technique overloading or Static binding)
- Run-time polymorphism(technique overriding or dynamic binding)
.png)
What is Method Overloading:
Method Overloading is a characteristic that lets a category have extra techniques having an identical name if their argument lists are one of a kind.
Arguments lists may be one of a kind as:
- A number of parameters.
- Data form of parameters.
- The sequence of Data form of parameters.
Method Overloading (Contd.)
- Method overloading is likewise referred to as Static Polymorphism.
- Static Polymorphism is likewise referred to as collect time binding or early binding
- Static binding occurs at a collect time.
- Method overloading is an instance of static binding in which binding of technique name to its definition occurs at Compile time.
- Overloading – Different Number of parameters in argument listing
Eg: void disp(char c)
void disp(char c, int num)
- Overloading – Difference in records form of arguments
Eg: void disp(char c)
void disp(int c)
- Overloading – Sequence of records form of arguments
Eg: void disp(char c, int num)
void disp(int num, char c)
Valid/invalid cases of method overloading
- Case 1:
int mymethod(int a, int b, float c)
int mymethod(int var1, int var2, float var3)
Result:/invalid Compile time error. Argument lists are exactly same. Both methods are having same number, data types and same sequence of data types in arguments.
- Case 2:
int mymethod(int a, int b)
int mymethod(float var1, float var2)
Result:/invalidPerfectly fine. Valid case for overloading. Here data types of arguments are different.
- Case 3:
int mymethod(int a, int b)
int mymethod(int num)
Result:/invalid Perfectly fine. Valid case for overloading. Here number of arguments are different.
- Case 4:
float mymethod(int a, float b)
float mymethod(float var1, int var2)
Result:/invalid Perfectly fine. Valid case for overloading. Sequence of the data types are different,
"first method is having (int, float) and second is having (float, int)".
- Case 5:
int mymethod(int a, int b)
float mymethod(int var1, int var2)
Result:/invalid Compile time error. Argument lists are exactly same. Even though return type of methods are different, it is not a valid case. Since return type of method doesn’t matter while overloading a method.
What is Method Overriding:
- Overriding is a characteristic that lets in a subclass or infant elegance to offer a selected implementation of a way this is already supplied via way of means of certainly considered one among its first-rate instructions or determined instructions.
- When away in a subclass has an identical name, identical parameters or signature, and identical go-back kind(or sub-kind) as a way in its first-rate-elegance, then the technique withinside the subclass is stated to override the technique withinside the superclass.
- In such instances, infant elegance overrides the determined elegance technique without even touching the supply code of the bottom elegance. This characteristic is referred to as technique overriding.
- Method overriding is used to offer precise implementation of a way this is already supplied via way of means of its first-rate elegance.
- The model of a way this is finished could be decided via way of means of the item this is used to invoke it.
- If an object of a determined elegance is used to invoke the technique, then the model withinside the determined elegance could be finished, however, if an item of the subclass is used to invoke the technique, then the model withinside the infant elegance could be finished.
- In different words, it's far the form of the item being referred to (now no longer the form of the reference variable) that determines which model of an override
Method Overriding (Contd.)
- When the reference variable of Parent elegance refers back to the item of Child elegance, it's far referred to as upcasting.
Eg: class A{}
class B extends A{}
A a=new B();//upcasting
RULES FOR METHOD OVERRIDING (Contd.)
- The argument listing should be precisely similar to the overridden technique.
- The go-back / return type ought to be the identical or a subtype of the go-back kind declared withinside the unique overridden technique withinside the superclass.
- The get right of entry to a degree can't be extra restrictive than the overridden technique's get right of entry to a degree.
- Instance techniques may be overridden simplest if they're inherited via way of means of the subclass.
- A technique declared very last can't be overridden
- A technique declared static can't be overridden however may be re-declared.
- If a way can't be inherited, then it can't be overridden.
- A subclass withinside the identical package deal because the instance's superclass can override any superclass technique that isn't always declared non-public or very last.
- A subclass in a one-of-a-kind package deal can simplest override the non-very last techniques declared public or protected.
- An overriding technique can throw any uncheck exceptions, no matter whether or not the overridden technique throws exceptions or now no longer. However, the overriding technique ought to now no longer throw checked exceptions that can be new or broader than those declared via way of means of the overridden technique. The overriding method can throw more limited or rarer exceptions than the overridden method.
- Constructors can't be overridden.

Intro of Virtual Functions:
- A digital/virtual characteristic is a member characteristic this is declared inside a base elegance and redefined (referred to as overriding) via way of means of a derived elegance.
- It implements the “one interface, more than one technique” philosophy which is polymorphism.
- The keyword digital is used to create a member characteristic as digital.
- Supports run-time polymorphism with the assistance of base elegance recommendations.
- When a digital/virtual characteristic is redefined via way of means of a derived elegance, the keyword digital/virtual isn't always needed (however may be particular if the programmer wants).
- The “virtual”-ity of the member characteristic keeps alongside the inheritance chain.
- An elegance that carries a digital characteristic is called polymorphic elegance.
- Virtual features permit us to create a listing of base elegance recommendations and contact techniques of any of the derived instructions without even understanding the type of derived elegance item.
For instance,/For Example, recall a worker control software program for an organization, allow the code to have an easy base elegance Employee, the elegance carries digital features likeraiseSalary(), transfer(), promote(),.. and so on. Different sorts of personnel like Managers, Engineers, ..and so on can also additionally have their personal implementations of the digital features found in base elegance Employees. In our whole software program, we simply want to skip a listing of personnel anywhere and contact suitable features without even understanding the form of the worker. For instance, we will without problems enhance the earnings of all personnel via way of means of iterating via listing of personnel. Every form of worker can also additionally have its personal common sense in its elegance, we don’t want to fear due to the fact if raiseSalary() is a gift for a selected worker kind, simplest that characteristic might be referred to as.
What are Pure Virtual Functions:
- A type of virtual function that has no body is known as a pure virtual function. A function can be declared as a pure virtual function by adding two things
- The keyword digital is used at the beginning of the characteristic
- The =zero(0) on the end of function declarator
Pure digital features are used withinside the instructions which aren't used directly.
_11zon.png)
If You Have any Queries Regarding our Topic Then Contact Us Here! or via the Comment icon .....
🎉 If you want to buy this document's PDF OR Any-File Click Here! (Service Currently Unavailable)
Post a Comment