Oops

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

3/27/23, 6:28 AM Oops

In [148… class Math:


def __init__(self, name):
self.name = name
self._hands = 2
self.__legs = 2
self.__eyes = 2

def walk(self):
print(f'This human is walking on {self.__legs} legs')

def eat(self):
print(f'This human is eating as it has {self.hands} hands')

@staticmethod
def pi():
return 3.141

In [149… class Indian(Human):


def __init__(self, name, nl, food):
super().__init__(name)
self.food = food
self.national_language = nl

def eat(self):
print(f'Indians love {self.food} foods')

In [150… ram = Indian('ram','english', 'Indian')

Polymorphism in python defines methods in the child class that have the
same name as the methods in the parent class. In inheritance, the child
class inherits the methods from the parent class. Also, it is possible to
modify a method in a child class that it has inherited from the parent class.

In [151… ram.walk()

This human is walking on 2 legs

In [18]: from abc import ABC, abstractmethod

class Iquack(ABC):
@abstractmethod
def quacking(self):
pass

class Ifly(ABC):
@abstractmethod
def flying(self):
pass

In [19]: class simpleQuack(Iquack):


def quacking(self):
return 'simple quack'

class normalQuack(Iquack):
def quacking(self):

file:///C:/Users/DELL/Desktop/Corpnce/Class Notes/PDF/Oops.html 1/2


3/27/23, 6:28 AM Oops

return 'normal quack'

class noQuack(Iquack):
def quacking(self):
return None

In [20]: class highFly(Ifly):


def flying(self):
return 'High fly'

class lowFly(Ifly):
def flying(self):
return 'low fly'

class noFly(Ifly):
def flying(self):
return None

In [21]: class IDuck(ABC):


@abstractmethod
def flying():
pass
@abstractmethod
def quacking():
pass

In [22]: class wildDuck(IDuck):


def __init__(self, Iflying, Iquacking):
self.fly = Iflying
self.quack = Iquacking

def flying(self):
return self.fly.flying()

def quacking(self):
return self.quack.quacking()

In [23]: johny = wildDuck(highFly(), simpleQuack())

In [24]: johny.flying()

Out[24]: 'High fly'

In [ ]:

file:///C:/Users/DELL/Desktop/Corpnce/Class Notes/PDF/Oops.html 2/2

You might also like