Untitled Document
OOP Part - 4 - Inheritance - Creating Subclasses
1. Simple Example of Inheritance Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ ***** 
# Simple Example of Inheritance
       
# Here we will create another class Developer Which will Inherit Employee Class 
# So now even if we don't write any code inside Developer class we will have access to all 
# the attributes of Employee class 
# Here we will Print email address of the two objects using instance ( object) of Developer class

class Employee:

	raise_amount = 1.04

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last + '@company.com'	

	def fullname(self):
		return '{} {}'.format(self.first,self.last)

	def apply_raise(self):
		self.pay = int(self.pay * self.raise_amount)

class Developer(Employee):
	pass

dev_1 = Developer('Mangesh', 'Pande', 50000)
dev_2 = Developer('Test', 'User', 60000)

print(dev_1.email)
print(dev_2.email)

# IMP - How Python Executes the code eventhough we don't have any init method or variables in 
# Developer class

# Here goes the ANSWER 
# Just run below print statement along with above code 
# Python uses MRO - Algorithm - Method Resolution Order 

print(help(Developer)) 
       
2. Program - Lets do a simple changes in Developer class Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Lets do a simple changes in Developer class 

# we have raise_amount class varibale which has 1.04 value in Employee class
# Now For Developer we want to change it 1.10 without impacting Employee class
# How can we acive that ?
# Here is the code for it 

class Employee:

	raise_amount = 1.04

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last + '@company.com'	

	def fullname(self):
		return '{} {}'.format(self.first,self.last)

	def apply_raise(self):
		self.pay = int(self.pay * self.raise_amount)

class Developer(Employee):
	raise_amt=1.10


dev_1 = Developer('Mangesh', 'Pande', 50000)
emp_1 = Employee('Test', 'User', 60000)

dev_1.apply_raise()
print(dev_1.pay)
emp_1.apply_raise()
print(emp_1.pay)
       
3. Program - IN the below Example we would like to pass one more argument - Programming Language to our Developer class Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - IN the below Example we would like to pass one more argument - Programming Language to our Developer class 

IN the below Example we would like to pass one more argument - Programming Language 
# to our Developer class . To Do this we will have to create Developer class init method 
# and then need to pass language as a 4th argument 
# Now in order to keep our code DRY we will first , last and pay variables from Employee class init method
# using super() keyyword . Here is the simple example for it . 

class Employee:

	raise_amount = 1.04

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last + '@company.com'	

	def fullname(self):
		return '{} {}'.format(self.first,self.last)

	def apply_raise(self):
		self.pay = int(self.pay * self.raise_amount)

class Developer(Employee):
	raise_amt=1.10

	def __init__(self,first,last,pay,prog_lang):
		super().__init__(first,last,pay)
		self.prog_lang = prog_lang


dev_1 = Developer('Mangesh', 'Pande', 50000,'Python')
dev_2 = Developer('Test', 'User', 60000,'Java')

dev_1.apply_raise()
print(dev_1.pay)
print(dev_1.email)
print(dev_1.prog_lang)
dev_2.apply_raise()
print(dev_2.pay)
print(dev_2.email)
print(dev_2.prog_lang)
       
4. Program - Lets Create Another sub class called Manager and add few more functioanlities to this new class Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Lets Create Another sub class called Manager and add few more functioanlities to this new class

class Employee:

	raise_amount = 1.04

	def __init__(self,first,last,pay):
		self.first = first
		self.last = last
		self.pay = pay
		self.email = first + '.' + last + '@company.com'	

	def fullname(self):
		return '{} {}'.format(self.first,self.last)

	def apply_raise(self):
		self.pay = int(self.pay * self.raise_amount)

class Developer(Employee):
	raise_amt=1.10

	def __init__(self,first,last,pay,prog_lang):
		super().__init__(first,last,pay)
		self.prog_lang = prog_lang

class Manager(Employee):
	def __init__(self, first, last, pay, employees=None):
		super().__init__(first, last, pay)
		if employees is None:
			self.employees = []
		else:
			self.employees = employees

	def add_emp(self, emp):
		if emp not in self.employees:
			self.employees.append(emp)

	def remove_emp(self, emp):
		if emp in self.employees:
			self.employees.remove(emp)

	def print_emps(self):
		for emp in self.employees:
			print ('--->', emp.fullname())


dev_1 = Developer('Mangesh', 'Pande', 50000,'Python')
dev_2 = Developer('Test', 'User', 60000,'Java')

mgr_1 = Manager('Sue', 'Smith', 90000, [dev_1])

print(mgr_1.email)
mgr_1.print_emps()
mgr_1.add_emp(dev_2)
mgr_1.print_emps()
mgr_1.remove_emp(dev_1)
mgr_1.print_emps()
       
5. Program - Python Builtin function isinstance() and issubclass Click Here
#********* Source Code From Website - Mangadaku - visit us at -http://mangadaku.com/ *****      
# Program - Python Builtin function isinstance() and issubclass
# isinstance will return True or False if instance is part of Subclass or Parent class

print(isinstance(mgr_1, Manager)) # O/p is true since mgr_1 is instance of Mangaer class

print(isinstance(mgr_1,Employee)) # O/P is true since mgr_1 is part of Manager and Manager is inheriting Em\
# Employee class 

print(isinstance(mgr_1, Developer)) # O/P is False since mgr_1 is not part of each other inheritance 

print(issubclass(Manager, Employee ) # O/p is true since manager is Sub class of Employee

print(issubclass(Developer,Employee)) # O/P is true Since Developer is sub class of Employee

print(issubclass(Manager, Developer)) # O/P is False since Manager is not part of each other inheritance 
       
Untitled Document