Method Overridden.py

 # Method Overridden

# Method overriding in Python refers to the ability of a 
# subclass to provide a specific implementation of a method 
# that is already defined in its superclass. When a method is 
# overridden in a subclass, the subclass provides a new 
# implementation of that method that replaces the 
# implementation inherited from the superclass.

class parent: ## parent = parent(object) is same by default there called a object
def write(self):
print("Called From Parent.")

class child(parent):
def write(self):
print("Called From Child.")

c1 = child()
c1.write()
# if we call parent class method like
# parent.write(c1)


# Multiple Inheritance
class parent1: ## parent1 = parent1(object) is same by default there called a object
def write(self):
print("It Parent1 Function")

class parent2: ## parent2 = parent2(object) is same by default there called a object
def write(self):
print("It Parent2 Function")

class child1(parent1, parent2):
def write(self):
print("It Child Function")

obj = child1()
obj.write()
# if we call parent1 and parent2 class method like
# parent1.write(obj)
# parent2.write(obj)

# Multilevel Inheritance
class father :
def write(self):
print("Called From Father")

class son(father):
def write(self):
print("Called From Son")

class grandson(son):
def write(self):
print("Called From Grand-Child")

cls = grandson()
cls.write()
print("\nWe Can Call Son Function By Son Class :: ")
son.write(cls)
print("We Can Call Father Function By Father Class :: ")
father.write(cls)

###############################################
class parent1:
def printf(self):
print("\nFrom Parent1")

class parent2:
def printf(self):
print("From Parent2")

class child1(parent1, parent2):
pass

class child2(parent2, parent1):
pass

obj1 = child1()
obj1.printf()

obj2 = child2()
obj2.printf()


###############################################
class father1(object):
def write(self):
print("Called From Father 1")

class father2(object):
def write(self):
print("Called From Father 2")

class father3(object):
def write(self):
print("Called From Fahter 3")

class child(father1, father2, father3):
def write(self):
father1.write(self)
father2.write(self)
father3.write(self)

obj1 = child()
obj1.write()
Example-1: class Account: def __init__(self, balance): self.balance = balance def calculateInterest(self): return self.balance * 0.01 class SavingsAccount(Account): def calculateInterest(self): return self.balance * 0.02 class CheckingAccount(Account): def calculateInterest(self): return self.balance * 0.005 class InvestmentAccount(Account): def calculateInterest(self): return self.balance * 0.05 savings_account = SavingsAccount(5000) checking_account = CheckingAccount(5000) investment_account = InvestmentAccount(5000) print("Interest For Savings Account : ", savings_account.calculateInterest()) print("Interest For Checking Account : ", checking_account.calculateInterest()) print("Interest For Investment Account : ", investment_account.calculateInterest())

Comments