Inheritance and Polymorphism in Python
Inheritance and Polymorphism are two important pillars of Object Oriented Programming (OOP) in Python. They help in writing reusable, flexible, and scalable code.
Inheritance in Python
Inheritance allows one class (child class) to acquire the properties and methods of another class (parent class).
It promotes code reusability and reduces duplication.
Syntax of Inheritance
class ParentClass:
pass
class ChildClass(ParentClass):
pass
Example of Inheritance
class Animal:
def speak(self):
print("Animal makes a sound")
class Dog(Animal):
def bark(self):
print("Dog barks")
dog = Dog()
dog.speak()
dog.bark()
Output:
Animal makes a sound
Dog barks
Types of Inheritance in Python
- Single Inheritance
- Multiple Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
Multilevel Inheritance Example
class GrandParent:
def show_gp(self):
print("I am GrandParent")
class Parent(GrandParent):
def show_p(self):
print("I am Parent")
class Child(Parent):
def show_c(self):
print("I am Child")
Method Overriding
When a child class provides its own implementation of a method already defined in the parent class, it is called method overriding.
class Vehicle:
def start(self):
print("Vehicle is starting")
class Bike(Vehicle):
def start(self):
print("Bike is starting")
b = Bike()
b.start()
Using super() Keyword
The super() function is used to call parent class methods from the child class.
class Car(Vehicle):
def start(self):
super().start()
print("Car is starting")
Polymorphism in Python
Polymorphism means many forms. In Python, polymorphism allows the same method name to behave differently based on the object.
Example of Polymorphism
class Bird:
def fly(self):
print("Bird can fly")
class Penguin(Bird):
def fly(self):
print("Penguin cannot fly")
b = Bird()
p = Penguin()
b.fly()
p.fly()
Polymorphism with Functions
def animal_sound(animal):
animal.speak()
class Dog:
def speak(self):
print("Dog barks")
class Cat:
def speak(self):
print("Cat meows")
animal_sound(Dog())
animal_sound(Cat())
Polymorphism in Built-in Functions
print(len("Python"))
print(len([1, 2, 3, 4]))
Inheritance and Polymorphism in Automation Testing
class Browser:
def open(self):
print("Opening browser")
class Chrome(Browser):
def open(self):
print("Opening Chrome")
class Firefox(Browser):
def open(self):
print("Opening Firefox")
This approach is commonly used in:
- Selenium Page Object Model (POM)
- Python test frameworks
- Robot Framework custom libraries
Difference Between Inheritance and Polymorphism
| Inheritance | Polymorphism |
|---|---|
| Reuses existing code | Provides different behaviors |
| Creates parent-child relationship | Uses same method name |
| Improves reusability | Improves flexibility |
Common Interview Questions
- What is inheritance?
- Types of inheritance in Python
- What is method overriding?
- What is polymorphism?
- Difference between inheritance and polymorphism
Conclusion
Inheritance helps reuse code by creating relationships between classes, while Polymorphism allows the same method to work differently for different objects.
Both concepts are essential for writing efficient Python applications and automation frameworks.
👉 Learn more Python OOP concepts on way2testing.com
No comments:
Post a Comment