Type Of Inheritance.py

# Type Of Inheritance :

        # ---> Single Inheritance
        # ---> Hierarchial Inheritance
        # ---> Multilevel Inheritance
        # ---> Multiple Inheritance
        # ---> Hybrid Inheritance

# Single Inheritance
# Relationship like linear

class A:
    def function1(self):
        print("This is Class A")

class B(A):
    def function2(self):
        print("This is Class B")

cls = B()
cls.function1()
cls.function2()

# Exmaple-1

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def make_sound(self):
        pass

    def display_info(self):
        print(f"name : {self.name}")
        print(f"Species : {self.species}")


class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name, species="Dog")
        self.breed = breed

    def make_sound(self):
        print("Bark Bark!")

    def display_info(self):
        super().display_info()
        print("Breed : ", self.breed)


my_dog = Dog("Buddy", "Golden Retriever")
my_dog.display_info()

my_dog.make_sound()

# Exmaple-2

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print(f"Name : {self.name}")
        print(f"Age : {self.age}")


class Student(Person):
    def __init__(self, name, age, student_id):
        super().__init__(name, age)
        self.student_id = student_id
        self.course = []

    def enroll(self, course):
        self.course.append(course)
        print(f"{self.name} has enrolled in {course}")

    def display_info(self):
        super().display_info()
        print(f"Student ID : ", self.student_id)
        print(f"Course Enrolled : ", end="")
        for course in self.course:
            print(course, end=", ")


std = Student("Anik", 14, 18101073)
std.enroll("Computer Science")
std.enroll("Mathematics")
print(std.course)
std.display_info()

# Example-3

class Employee:
    def __init__(self, name, age, employee_id):
        self.name = name
        self.age = age
        self.employee_id = employee_id

    def display_info(self):
        print(f"Name : {self.name}")
        print(f"Age : {self.age}")
        print(f"Employee ID : {self.employee_id}")


class FullTimeEmployee(Employee):
    def __init__(self, name, age, employee_id, salary):
        super().__init__(name, age, employee_id)
        self.salary = salary

    def calculate_salary(self):
        return self.salary * 12

    def display_info(self):
        super().display_info()
        print(f"Type : Full-Time Employee")
        print(f"Annual Salary : ${self.calculate_salary():,.2f}")


class PartTimeEmployee(Employee):
    def __init__(self, name, age, employee_id, hourly_rate, hours_worked):
        super().__init__(name, age, employee_id)
        self.hourly_rate = hourly_rate
        self.hours_worked = hours_worked

    def calculate_salary(self):
        return self.hourly_rate * self.hours_worked

    def display_info(self):
        super().display_info()
        print(f"Type : Part-Time Employee")
        print(f"Monthly Salary : ${self.calculate_salary():,.2f}")


class ContractEmployee(Employee):
    def __init__(self, name, age, employee_id, contract_duration, hourly_rate):
        super().__init__(name, age, employee_id)
        self.contract_duration = contract_duration
        self.hourly_rate = hourly_rate

    def calculate_salary(self):
        # 40: This represents the number of hours worked per week.
        # 4: This represents the number of weeks in a month.
        return self.hourly_rate * 40 * 4 * self.contract_duration

    def display_info(self):
        super().display_info()
        print(f"Type : Contract Employee")
        print(f"Total Contract Salary : ${self.calculate_salary():,.2f}")


full_time_emp = FullTimeEmployee("Alice", 30, "FT001", 50000)
part_time_emp = PartTimeEmployee("Bob", 25, "PT001", 30, 120)
contract_emp = ContractEmployee("Charlie", 35, "CT001", 6, 50)

print("Full Time Employee Information : ")
full_time_emp.display_info()

print("\nPart Time Employee Information : ")
part_time_emp.display_info()

print("\nContract Employee Information : ")
contract_emp.display_info()

# Hierarchial Inheritance
# Hierarchical inheritance involves a structure where  one or more subclasses 
# inherit from a single base class, but additional sub-classes can also inherit from 
# those subclasses, forming a hierarchy. (Sub-Class Have Sub-Class)

# The inheritance structure resembles a tree, with the base class at the root 
# and multiple subclasses branching out from it.

class parent:
    def func1(self):
        print("\nThis is Parent Class")

class child1(parent):
    def func2(self):
        print("This is Child1 Class")

class child2(parent):
    def func3(self):
        print("This is Child2 Class")

c1 = child1()
c1.func1()
c1.func2()

c2 = child2()
c2.func1()
c2.func3()

Example-1

class Vehicle:
    def __init__(self, brand, model):
        self.brand = brand
        self.model = model

    def display_info(self):
        print(f"Brand : {self.brand}, Model: {self.model}")


class Car(Vehicle):
    def __init__(self, brand, model, seat_num):
        super().__init__(brand, model)
        self.seat_num = seat_num

    def display_info(self):
        super().display_info()
        print(f"Number of Seats : {self.seat_num}")


class Truck(Vehicle):
    def __init__(self, brand, model, cargo_capacity):
        super().__init__(brand, model)
        self.cargo_capacity = cargo_capacity

    def display_info(self):
        super().display_info()
        print(f"Cargo Capacity : {self.cargo_capacity}")


class ElectricCar(Car):
    def __init__(self, brand, model, seat_num, battery_range):
        super().__init__(brand, model, seat_num)
        self.battery_range = battery_range

    def display_info(self):
        super().display_info()
        print(f"Battery Range : {self.battery_range}")


class HybridCar(Car):
    def __init__(self, brand, model, seat_num, fuel_economy):
        super().__init__(brand, model, seat_num)
        self.fuel_economy = fuel_economy

    def display_info(self):
        super().display_info()
        print(f"Fuel Economy : {self.fuel_economy}")


tesla_car = ElectricCar("Tesla", "Model S", 4, 375)
tesla_car.display_info()

ford_truck = Truck("Ford", "F-150", 2)
ford_truck.display_info()

toyota_car = HybridCar("Toyota", "Prius", 5, 50)
toyota_car.display_info()

Example-2

class Employee:
    def __init__(self, name, employee_id, salary):
        self.name = name
        self.employee_id = employee_id
        self.salary = salary

    def display_info(self):
        print(f"Name : {self.name}, ID: {self.employee_id}, Salary: {self.salary} ")


class Manager(Employee):
    def __init__(self, name, employee_id, salary, department):
        super().__init__(name, employee_id, salary)
        self.department = department
        self.subordinates = []

    def add_subordinate(self, employee):
        if isinstance(employee, Employee):
            self.subordinates.append(employee)
            print(f"{employee.name} added as a subordinate to {self.name}")
        else:
            print(f"Invalid Employee Type. Could not add Subordinate.")

    def display_info(self):
        super().display_info()
        print(f"Department: {self.department}")
        if self.subordinates:
            print("Subordinates : ")
            for subordinates in self.subordinates:
                subordinates.display_info()


class Executive(Manager):
    def __init__(self, name, employee_id, salary, department, stock_options):
        super().__init__(name, employee_id, salary, department)
        self.stock_options = stock_options

    def display_info(self):
        super().display_info()
        print(f"Stock Options : {self.stock_options}")


class Intern(Employee):
    def __init__(self, name, employee_id, salary, supervisor):
        super().__init__(name, employee_id, salary)
        self.supervisor = supervisor

    def display_info(self):
        super().display_info()
        print(f"Supervisor : {self.supervisor}")


ceo = Executive("John Doe", "CEOOO1", 1000000, "Executive", 10000)
ceo.display_info()

cto = Manager("Jane Smith", "CT0001", 300000, "Engineering")
engineer1 = Employee("Anik", "ENG290", 10000)
intern1 = Intern("Alice Johnson", "INT001", 5000, "Anik")

cto.add_subordinate(engineer1)
cto.add_subordinate(intern1)

cto.display_info()


# Multilevel Inheritance
# Multilevel inheritance in Python refers to a scenario where 
# a class inherits from another class, and then another class 
# inherits from that derived class, creating a chain of 
# inheritance. It create a hierarchy of classes where each 
# class inherits attributes and methods from its parent class.

# Multilevel inheritance forms a linear chain of subclasses, 
# whereas hierarchical inheritance involves multiple subclasses
# branching out from a single base class.

class father:
    def fun1(self):
        print("\nThis is Father Class")

class son(father):
    def fun2(self):
        print("This is Son Class")

class grandson(son):
    def fun3(self):
        print("This is GrandSon Class")

a = grandson()
a.fun1()
a.fun2()
a.fun3()

# Example-1

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print(f"Name : {self.name}, Age: {self.age}")


class Employee(Person):
    def __init__(self, name, age, employee_id, department):
        super().__init__(name, age)
        self.employee_id = employee_id
        self.department = department

    def display_info(self):
        super().display_info()
        print(f"Employee ID : {self.employee_id}, Department: {self.department}")


class FacultyMember(Employee):
    def __init__(self, name, age, employee_id, department, research_interests):
        super().__init__(name, age, employee_id, department)
        self.research_interests = research_interests

    def display_info(self):
        super().display_info()
        print(f"Research Innterests: {', '.join(self.research_interests)}")


class Professor(FacultyMember):
    def __init__(self, name, age, employee_id, department, research_interests, publications):
        super().__init__(name, age, employee_id, department, research_interests)
        self.publications = publications

    def display_info(self):
        super().display_info()
        print(f"Publications : {', '.join(self.publications)}")


class AssistantProfessor(Professor):
    def __init__(self, name, age, employee_id, department, research_interests, publications, teaching_load):
        super().__init__(name, age, employee_id, department, research_interests, publications)
        self.teaching_load = teaching_load

    def display_info(self):
        super().display_info()
        print(f"Teaching Load : {self.teaching_load}")


professor_1 = Professor("John Smith", 45, "PROF001", "Compter Science", ["Machine Learning", "Data Science"], ["A. Smith et al.", "Journal of AI"])
professor_1.display_info()

assistant_professor_1 = AssistantProfessor("Bob Johnson", 35, "ASST001", "Mathematics", ["Number Theory", "Algebra"], ["C. Johnson et al., Journal of Mathematics"], "Introductory Courses")
assistant_professor_1.display_info()

Example-2:

class Employee:
    def __init__(self, name, employee_id, salary):
        self.name = name
        self.employee_id = employee_id
        self.salary = salary

    def display_info(self):
        print(f"Name: {self.name}, ID: {self.employee_id}, Salary: {self.salary}")


class Developer(Employee):
    def __init__(self, name, employee_id, salary, programming_language):
        super().__init__(name, employee_id, salary)
        self.programming_language = programming_language

    def write_code(self):
        print(f"{self.name} is writting code in {self.programming_language}")


class TeamLead(Developer):
    def __init__(self, name, employee_id, salary, programming_language, team_size):
        super().__init__(name, employee_id, salary, programming_language)
        self.team_size = team_size

    def conduct_meeting(self):
        print(f"{self.name} is conducting a meeting with the team")


class ProjectManager(TeamLead):
    def __init__(self, name, employee_id, salary, programming_language, team_size, project_name):
        super().__init__(name, employee_id, salary, programming_language, team_size)
        self.project_name = project_name

    def oversee_project(self):
        print(f"{self.name} is overseeing the project: {self.project_name}")


developer = Developer("Alice", "DEV001", 80000, "Python")
developer.display_info()
developer.write_code()

team_lead = TeamLead("Bob", "TL001", 100000, "Java", 5)
team_lead.display_info()
team_lead.write_code()
team_lead.conduct_meeting()

project_manager = ProjectManager("Charlie", "PM001", 1200000, "JS", 10, "Web Application")
project_manager.display_info()
project_manager.write_code()
project_manager.conduct_meeting()
project_manager.oversee_project()

# Multiple Inheritance
# Multiple inheritance in Python refers to a situation 
# where a class can inherit attributes and methods from more 
# than one parent class. This means that a child class can 
# inherit from multiple base classes.

class classA:
    def __init__(self):
        self.name1 = "A"

    def wrt1(self):
        print("\nThis is class ", self.name1)

class classB:
    def __init__(self):
        self.name2 = "B"

    def wrt2(self):
        print("This is Class ", self.name2)

class classC(classA, classB):
    def __init__(self):
        classA.__init__(self)
        classB.__init__(self)

    def write(self):
        classA.wrt1(self)
        classB.wrt2(self)

b = classC()
b.write()

Example-1

class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def display_info(self):
        print(f"Name : {self.name}, Age: {self.age}")


class Employee:
    def __init__(self, employee_id, department):
        self.employee_id = employee_id
        self.department = department

    def display_employee_info(self):
        print(f"Employee ID : {self.employee_id}, Department: {self.department}")


class FullTimeEmployee(Person, Employee):
    def __init__(self, name, age, employee_id, department, salary):
        super().__init__(name, age)
        Employee.__init__(self, employee_id, department)
        self.salary = salary

    def display_info(self):
        super().display_info()
        self.display_employee_info()
        print(f"Salary : {self.salary}")


class PartTimeEmployee(Person, Employee):
    def __init__(self, name, age, employee_id, department, hourly_rate, hours_worked):
        super().__init__(name, age)
        Employee.__init__(self, employee_id, department)
        self.hourly_rate = hourly_rate
        self.hours_worked = hours_worked

    def display_info(self):
        super().display_info()
        self.display_employee_info()
        print(f"Hourly Rate : {self.hourly_rate}, Hours Worked: {self.hours_worked}")


full_time_employee = FullTimeEmployee("Alice", 30, "FT001", "Finance", 5000)
full_time_employee.display_info()

part_time_employee = PartTimeEmployee("Bob", 25, "PT001", "HR", 20, 25)
part_time_employee.display_info()


Comments