Skip to content

Instantly share code, notes, and snippets.

@goyalrohit
Last active September 28, 2019 11:34
Show Gist options
  • Save goyalrohit/15a0cae12f93c2fa9722c8762676ac14 to your computer and use it in GitHub Desktop.
Save goyalrohit/15a0cae12f93c2fa9722c8762676ac14 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Example of Inheritance in Python
# A Parent Class (Base class) Employee having its own functionality
class Employee:
# Constructor of the Employee Class
def __init__(self, designation):
self.designation = designation
# Function to check the designation of the employee type
def checkDesignation(self):
return self.designation
# Function to check if the child class is from Employee
def isEmployee(self):
return True
# Function that must be customized in the Child Class
def getIntroduction(self):
return "Hi! I am an Employee"
# A Child Class (Derived class) extending the Employee
class Architect(Employee):
#overridden fuction from Employee
def getIntroduction(self):
return "Hi! I am an Architect"
# Function specific to the Architect Class
def myArcFunc(self):
return "I have expertise in designing"
# Another Child Class (Derived class) extending the Employee
class Developer(Employee):
#overridden fuction from Employee
def getIntroduction(self):
return "Hi! I am a Developer"
# Function specific to the Developer Class
def myDevFunc(self):
return "I have expertise in development"
# Main Invoking Program
emp = Employee("A Employee") # An Object of Employee
print(emp.getIntroduction(), emp.isEmployee(), emp.checkDesignation())
emp = Architect("Solution Architect") # An Object of Architect
print(emp.getIntroduction(), emp.isEmployee(), emp.checkDesignation(), emp.myArcFunc())
emp = Developer("Senior Developer") # An Object of Developer
print(emp.getIntroduction(), emp.isEmployee(), emp.checkDesignation(), emp.myDevFunc())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment