11## Polymorphism
22
3- - Like other programming language python also support polymorphism .
4- - Sometimes an object comes in many types or forms create a method that handle such different type of data we can call it as polymorphism
3+ - Like other programming language python also support polymorphism.
4+ - Sometimes an object comes in many types or forms create a method that handles such different type of data we can call it as polymorphism
55- Python you can define a method in such a way that there are multiple ways to call it.
6- polymorphism can be achieved using following two terms
7- 1. Method Overloading.
8- 2. Method Overriding.
9-
6+ polymorphism can be achieved using the following two terms
7+ ```
8+ 1. Method Overloading.
9+ 2. Method Overriding.
10+ ```
1011
1112## 1. Method Overloading:
1213
13- - Method overloading is the ability to create multiple methods with same name and different parameter (Or Different Type)
14- within same class.
15- - In the simple word, creates multiple methods that can handle diferrent data types in same class
16-
14+ - Method overloading is the ability to create multiple methods with the same name and different parameter (Or Different Type)
15+ within the same class.
16+ - In the simple word, creates multiple methods that can handle different data types in the same class
17+
1718![ Method Overloading] ( https://github.com/chavarera/PythonScript/blob/master/Class/methodOverloading.png )
1819
19- Python built in polymorphism Example
20+ Python built- in polymorphism Example
2021** len()** Function can handle different datatypes
2122
2223### Example:
@@ -41,13 +42,19 @@ length=len(mytuple)
4142print (length)
4243# Result: 7
4344```
45+ Output
46+ ```
47+ 21
48+ 9
49+ 7
50+ ```
4451
4552in above example ** len()** is an example of polymorphism which handelling various data types(string,list,tuple)
4653
4754
4855### Custom Method Overloading
49- - Python directly not support an method overloading but we can trick it to access method overloading.
50- - Python doesn't allow to create same name method in same class but using defualt argument we can achieve method overloading in class
56+ - Python directly not support a method overloading but we can trick it to access method overloading.
57+ - Python doesn't allow to create same name method in the same class but using default argument we can achieve method overloading in class
5158
5259### Example:
5360``` python
@@ -89,17 +96,18 @@ s1.Display("Jhon",24)
8996print (" \n ----Display('Jhon',24,20000) with three parameter---" )
9097s1.Display(" Jhon" ,24 ,20000 )
9198
92- '''
93- #Result
99+ ```
100+ Output:
101+ ```
94102----Display() without parameter---
95- Name:None
96- Age:None
97- Salary:None
103+ Name: None
104+ Age: None
105+ Salary: None
98106
99- ----Display('Jhon') with single parameter---
100- Name:Jhon
101- Age:None
102- Salary:None
107+ ----Display('Jhon') with a single parameter---
108+ Name: Jhon
109+ Age: None
110+ Salary: None
103111
104112----Display('Jhon',24) with two parameter---
105113Name:Jhon
@@ -110,13 +118,11 @@ Salary:None
110118Name:Jhon
111119Age:24
112120Salary:20000
113-
114- '''
115121```
116122
117123## 2.Method Overriding
118- - Simply defining in the child class a method with the same name of a method in the parent class.
119- - Defining multiple methods with same name in child class with same or diffrent parameter called as method overriding
124+ - Simply defined in the child class a method with the same name as a method in the parent class.
125+ - Defining multiple methods with the same name in the child class with a same or different parameter called as method overriding
120126
121127![ method Overriding] ( https://github.com/chavarera/PythonScript/blob/master/Class/Methodoverriding.png )
122128## Example of simple Inheritance
@@ -136,12 +142,17 @@ s1=Son()
136142
137143# child class Doesn't have any method
138144s1.Display()
139- # still we can accesss parent class methods
145+ # still we can access parent class methods
140146# becuase son is derived class from Father
141147
142- # Result:from parentClass:I am Father
148+ # Result:
149+ ```
150+ Output:
151+ ```
152+ from a parent class: I am Father
143153```
144154
155+
145156### Example Of Method Overriding*
146157``` python
147158# parent class
@@ -152,7 +163,7 @@ class Father:
152163# Derived class from Father (Empty class)
153164class Son (Father ):
154165 def Display (self ):
155- print (" from childClass: I am Son" )
166+ print (" from child class: I am Son" )
156167
157168
158169# Create object of child class
@@ -161,8 +172,10 @@ s1=Son()
161172# child class Display() method overrids the parent class Method
162173s1.Display()
163174
164- # overriding method is called instead of Parent method
165- # if if method is overriden then only child class method is called
166-
167- # Result:from childClass:I am Son
175+ # overriding method is called instead of the Parent method
176+ # if if the method is overridden then only child class method is called
177+ ```
178+ Output:
179+ ```
180+ from child class: I am Son
168181```
0 commit comments