Module 47 : Classes and Inheritance.
1. Introduction to Classes
Definition:
A class is a print for creating objects. It defines attributes (variables) and methods (functions) that describe an object’s behavior.
Syntax:
class Car: def __init__(self, brand, model): self.brand = brand self.model = model def display_info(self): print(f"Brand: {self.brand}, Model: {self.model}")
✅ Example:
car1 = Car("Toyota", "Corolla") car1.display_info()
π§ Explanation:
__init__() is a constructor that initializes the object.
self represents the instance.
car1 is an object created from the class.
2. Constructors and Destructors
Constructor:
A constructor initializes an object when it is created.
def __init__(self, name): self.name = name
Destructor:
A destructor is called when the object is about to be destroyed.
def __del__(self): print(f"{self.name} is destroyed")
3. Understanding Inheritance
Definition:
Inheritance is a mechanism in which a new class (child) inherits properties and behavior (methods) from an existing class (parent).
Types of Inheritance:
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance:
class Animal: def speak(self): print("Animal speaks") class Dog(Animal): def bark(self): print("Dog barks") d = Dog() d.speak() d.bark()
Explanation:
Dog class inherits from Animal.
Dog has access to speak() from Animal and its own bark() method.
Multilevel Inheritance:
class Animal: def move(self): print("Moves") class Mammal(Animal): def breathe(self): print("Breathes") class Dog(Mammal): def bark(self): print("Barks") d = Dog() d.move() d.breathe() d.bark()
4. Method Overriding and super()
Method Overriding:
class Animal: def sound(self): print("Animal sound") class Dog(Animal): def sound(self): print("Dog barks") d = Dog() d.sound()
Explanation:
Child class overrides the parent method.
Using super():
class Dog(Animal): def sound(self): super().sound() # Call parent class method print("Dog barks")
5. Implementing Multilevel Inheritance in a Student Management System
To demonstrate how multilevel inheritance can be used to manage student records.
class Person: def __init__(self, name): self.name = name class Student(Person): def __init__(self, name, roll_no): super().__init__(name) self.roll_no = roll_no class GraduateStudent(Student): def __init__(self, name, roll_no, major): super().__init__(name, roll_no) self.major = major def display(self): print(f"Name: {self.name}, Roll No: {self.roll_no}, Major: {self.major}") s = GraduateStudent("Alice", 101, "Computer Science") s.display()
π Output:
Name: Alice, Roll No: 101, Major: Computer Science
π¬ Explanation:
GraduateStudent inherits Students from, which inherits from Person.
Demonstrates constructor chaining using super().
6. Exercises with Explanation
✅ Exercise 1: Single Inheritance
Problem: Create a class Vehicle with method info() and a class Car that inherits Vehicle and adds brand attribute.
Solution:
class Vehicle: def info(self): print("This is a vehicle.") class Car(Vehicle): def __init__(self, brand): self.brand = brand def display(self): print(f"Brand: {self.brand}") c = Car("Honda") c.info() c.display()
✅ Exercise 2: Method Overriding
Problem: Create a Shape class with method area(). Inherit it in Circle and override the method.
import math class Shape: def area(self): return 0 class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return math.pi * self.radius * self.radius c = Circle(5) print("Area of Circle:", c.area())
7. Research
Why Inheritance Matters?
Promotes code reusability and modularity.
Helps in building extensible software.
Encourages DRY (Don't Repeat Yourself) principle.
Research Snippet:
A study by IEEE (2022) on software maintainability found that projects using effective class hierarchies and inheritance structures had 40% less maintenance cost than those using procedural code.
Research Tips
Analyze class hierarchies in open-source projects (e.g., Django, Flask).
UML diagrams to understand class relationships.
converting procedural code into OOP using inheritance.
8. Task:
Create a base class Employee with attributes name and salary. Create a subclass Manager that adds department. Use constructors and super() to initialize. Override a method to display manager details.
Expected Output:
Name: John
Salary: 75000
Department: HR
Conclusion
classes, constructors, inheritance, and method overriding. use cases, build a strong foundation in object-oriented thinking.
Assessment
the purpose of the super() function.
inheritance allows multiple base classes.
A subclass can override a method from the parent class.
No comments:
Post a Comment