Inheritance
Inheritance
Process of acquiring properties from one class to another classes. Create hierarchy between classes.
Hierarchy flow:
Coding example:
Parent class.
class Animal:
weight: int
color: str
age: int
animal_type: str
def eat(self):
print('Animal eating')
def sleep(self):
print('Animal sleeping')
Child class:
class Dog(Animal):
# All animal attributes are inherited.
can_shed: bool
domestic_name: str
#All animal methods are inherited.
Accessing through child class instance parent class methods.
dog = Dog()
dog.eat()
It will provide the result as child has inherited all public attributes and methods of parent class.
Adding child specific function to child class.
class Dog(Animal):
# All animal attributes are inherited.
can_shed: bool
domestic_name: str
def talk(self):
print("Bark!")
#All animal methods are inherited.
dog = Dog()
dog.talk() -> it will provide result as talk is child method and child instance has access to it.
animal = Animal()
animal.talk() -> it will not execute and will provide error as it is a child class method and parent don't have any means to know about it.
What is method override?
If we have a method in parent class and child class inherited it and modified it's definition at it's class then that use of method is own as overriding.
Example:
class Animal:
weight: int
color: str
age: int
animal_type: str
def eat(self):
print('Animal eating')
def sleep(self):
print('Animal sleeping')
class Dog(Animal):
# All animal attributes are inherited.
can_shed: bool
domestic_name: str
def talk(self):
print("Bark!")
def eat(self):
print('Chews bone!")
dog=Dog()
dog.eat() - > It will print the information from child class as child class has already overridden parent class method eat().
dog.sleep() -> It will check first child class for method when it won't find then will search in parent class and will print its result.
Code from IDE:
Scenario want to implement inheritance in our arena game:
- Currently our only class is Enemy()
- Enemy() is our parent class
- We will now create two children classes
- Zombie()
- Ogre()
# Parent class Enemy is also known as super class or base class.
class Enemy:
def __init__(self, typeOfEnemy: str,
healthPoints: int = 10,
attack_damage: int = 1):
self.__typeOfEnemy = typeOfEnemy
self.healthPoints = healthPoints
self.attack_damage = attack_damage
def get_typeOfEnemy(self):
return self.__typeOfEnemy
def talk(self):
print(f"I am a {self.__typeOfEnemy}. Be prepared to fight!")
def walk_forward(self):
print(f"{self.__typeOfEnemy} moves closer to you!")
Zombie class:
# Child class of Enemy Zombie
from Enemy import Enemy
class Zombie(Enemy):
def __init__(self, healthPoints, attack_damage):
super().__init__(typeOfEnemy='Zombie', healthPoints=healthPoints,
attack_damage=attack_damage)
self.healthPoints = healthPoints
self.attack_damage = attack_damage
# method overriding
# This method is overriding the talk method of parent class Enemy
def talk(self):
print(f"Zombie: Braaaains!")
# new method specific to Zombie class
def spread_diesease(self):
print(f"Zombie: Spreading disease!")
Ogre class:
# Child class of Enemy Ogre
from Enemy import Enemy
class Orge(Enemy):
def __init__(self, healthPoints, attack_damage):
super().__init__(typeOfEnemy='Ogre', healthPoints=healthPoints,
attack_damage=attack_damage)
self.healthPoints = healthPoints
self.attack_damage = attack_damage
# method overriding
# This method is overriding the talk method of parent class Enemy
def talk(self):
print(f"Ogre: Smash them!")
main.py
# using implementation
from Enemy import Enemy
from Zombie import Zombie
from Ogre import Orge
zombie=Zombie(healthPoints=15, attack_damage=3)
# This will call the getter method for typeOfEnemy
print(f"Enemy type = {zombie.get_typeOfEnemy()}")
# This will call the talk method from the Zombie class
print(f"{zombie.talk()}")
# This will call the spread_diesease method from the Zombie class
print(f"{zombie.spread_diesease()}")
ogre=Orge(healthPoints=25, attack_damage=7)
# This will call the getter method for typeOfEnemy
print(f"Enemy type = {ogre.get_typeOfEnemy()}")
# This will call the talk method from the Ogre class
print(f"{ogre.talk()}")


Comments
Post a Comment