#60 Python Tutorial for Beginners | Method Overloading and Method Overriding
Introduction to Polymorphism in Python
In this video, the instructor introduces the concept of polymorphism in Python and explains its three types: operator overloading, method overloading, and method overriding.
Operator Overloading
- Operator overloading means that the same operator can be used for different types of operands.
- The add method is an example of operator overloading. It takes different types of parameters or arguments.
- Polymorphism allows you to use the same method name but with different arguments.
Method Overloading
- Method overloading is not available in Python but is present in other OOP languages like Java and C#.
- Method overloading means having two methods with the same name but different parameters or arguments.
- In Python, you cannot create two methods with the same name.
Method Overriding
- Method overriding means having two methods with the same name and number of parameters or arguments.
- This can be achieved through inheritance where Class A and Class B both have a method with the same name and parameter.
Achieving Method Overloading in Python
- To achieve method overloading in Python, you can use a third variable or default arguments using
None.
- If you want to pass three values instead of two, you can use
a + b + cas a solution.
- To solve issues when not passing all required arguments, set them as
Noneby default.
Conclusion
Polymorphism is an important concept in object-oriented programming that allows for flexibility and reusability of code. While method overloading is not available in Python, it can still be achieved through other means such as using default arguments.
Python OOP Tutorial - Working with Classes and Objects
In this section, we learn about power loading and method overriding in Python.
Power Loading
- Pass three values (5, 9, 6) to variables B and C.
- If passing two arguments, check if B and C are not equal to none before adding them.
- If passing three arguments, calculate S as A + B + C.
- Declare S outside so that it can be used from anywhere.
- If only one argument is passed, set S equal to A.
Method Overriding
- Create a simple example with Class A having a function named show.
- Create an object of A and call the show method.
- Create a new class named Class B which inherits from Class A.
- When calling the show method on an object of Class B, it will first search for the method inside B. Since it doesn't exist there, it will go to A to search for it.
Overall, this section covers important concepts in Object-Oriented Programming such as power loading and method overriding. The examples provided help illustrate how these concepts work in Python.
The Evolution of Phone Ownership
In this section, the speaker talks about how he used to not have a phone and would borrow his father's Nokia double 1 double zero. He later got a Motorola phone and would identify it as his own instead of his father's Nokia.
Phone Ownership Evolution
- The speaker did not have a phone and would borrow his father's Nokia double 1 double zero.
- The speaker later got a Motorola phone and identified it as his own instead of his father's Nokia.
Method Overriding in Java
In this section, the speaker explains method overriding in Java using an example with classes A and B. When class B has its own show method, calling show on an instance of B will call its own show method instead of inheriting from class A.
Method Overriding Example
- When class B does not have its own show method, calling show on an instance of B will inherit from class A.
- When class B has its own show method, calling show on an instance of B will call its own show method instead of inheriting from class A.
- This concept will be further explored in future tutorials on abstract classes or interfaces.