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

#PyP Inheritance

class Car:
def __init__(self, name, age):
self.name = name
self.age = age
def old(self):
print(f"{self.name} is {self.age} years old.")

class SUV(Car):
def __init__(self, name, age, company):
super().__init__(name, age)
self.company = company

def show_company(self):
print(f"{self.name} is a {self.company} brand.")

crysta = Car("Crysta", "6")

duster = SUV("Duster", "4", "Renault")

crysta.old()
duster.old()

duster.show_company()

#PyP Polymorphism

class Car:
def __init__(self, name):
self.name = name

def age(self):
return "how old?"

class SUV(Car):
def age(self):
return "12"

class Hatchback(Car):
def age(self):
return "19"

car = Car("Civic")
suv = SUV("Baleno")
hatch = Hatchback("City")

print(car.name + " is " + car.age())


print(suv.name + " is " + suv.age())
print(hatch.name + " is " + hatch.age())

#PyP Encapsulation

class BankAccount:
def __init__(self, name, balance):
self.__name = name
self.__balance = balance

def get_name(self):
return self.__name
def get_balance(self):
return self.__balance

def deposit(self, amount):


if amount > 0:
self.__balance += amount

def withdraw(self, amount):


if 0 < amount <= self.__balance:
self.__balance -= amount

account = BankAccount("kartikay", 1000)

print("Your name is", account.get_name())


print("Balance is", account.get_balance())

add = int(input("Enter ammount added: "))


account.deposit(500)

print("Balance is", account.get_balance())

cut = int(input("Enter ammount to withdraw: "))

account.withdraw(cut)

print("Balance is", account.get_balance())

print(account.__name)
print(account.__balance)

#PyP Abstraction

from abc import abstractmethod


class Car:
def __init__(self, name):
self.name = name

@abstractmethod
def age(self):
pass

class SUV(Car):
def age(self):
print(f"{self.name} is an SUV")

class hatchback(Car):
def age(self):
print(f"{self.name} is a hatchback")

suv = SUV("Scorpio")
suv.age()

hatch = hatchback("Jazz")
hatch.age()

You might also like