What is Polymorphism:
- Polymorphisms is a generic term that means 'many shapes'.
- Polymorphism is the ability (in programming) to present the same interface for differing underlying forms (data types).
- Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.
- More precisely Polymorphisms means the ability to request the same operations be Performed on a wide range of different types of things. (operator overloading)
- Polymorphism is the ability of one object to be treated, or used, like another.
- It is closely related to the inheritance hierarchy.
- Function polymorphism is a kind of overloading and overriding.
- Class polymorphism is like inheriting parent class/interface and instantiating the subclass with parent class/interface.
- (class doesn't support polymorphism by default.) (we achieve class level polymorphism by interface/abstract class)
- (object doesn't support polymorphism by default; we cannot add 1 to any object) (we achieve polymorphism in object by 'operator function')
By polymorphism we can develop
new ability. (child jab born hota hai.
to vo polymorphic nature nahi rakhata, use ability di jati hai polymorphic hone
ki.)
Polymorphhism in Plain English:
Noun: 1. The occurrence of something in different forms, in
particular.
2. The occurrence of different forms
among the members of a population or colony.
Synonym: Multiformity
Why polymorphism:
- It makes our code simple (by giving polymorphic nature)
- U don't have remember which methode to call. (eg. multi-language SW) (function/operator overloading)
- To extend the behaviour of base class. (function overriding)
The
beauty of polymorphism is that the code
working with the different classes does not
need to know which class it is using since they’re all used the same way. A
real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply
apply pressure to it. What a button “does,” however, depends on what it is
connected to and the context in which it is used — but the result does not
affect how it is used. If your boss tells you to press a button, you already
have all the information needed to perform the task.
In the
programming world, polymorphism is used to make applications
more modular and extensible. Instead of messy conditional statements
describing different courses of action, you create interchangeable objects that
you select based on your needs. That is the basic goal of polymorphism.
There are various forms
of Polymorphism:
- Function overloading: Defining multiple functions with the same name and different parameter types, such as sqrt(float), sqrt(double) and sqrt(complex). In most languages that allow this, the compiler will automatically select the correct one for the type of argument being passed into it, thus this is compile-time polymorphism.
- Virtual methods in OOP: A method of a class can have various implementations tailored to the specifics of its subclasses; each of these is said to override the implementation given in the base class. Given an object that may be of the base class or any of its subclasses, the correct implementation is selected on the fly, thus this is run-time polymorphism.
- Templates: A feature of some OO languages whereby a function, class, etc. can be parameterised by a type. For example, you can define a generic "list" template class, and then instantiate it as "list of integers", "list of strings", maybe even "list of lists of strings" or the like. Generally, you write the code once for a data structure of arbitrary element type, and the compiler generates versions of it for the various element types.
No comments:
Post a Comment