Student Activity

Student Activity Guide:

1) Classes and Objects

  • Create a class Car with attributes like brand, model, and year.

  • Instantiate objects from the class and access their attributes.

Example Code:

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

    def display_info(self):
        print(f"{self.year} {self.brand} {self.model}")

car1 = Car("Toyota", "Corolla", 2022)
car1.display_info()

2) Attributes and Methods

  • Create a class Student with attributes name and age.

  • Add a method that prints the student's details.

Example Code:

3) Inheritance and Polymorphism

  • Create a base class Animal and a derived class Dog.

  • Override a method to demonstrate polymorphism.

Example Code:

4) Encapsulation and Abstraction

  • Implement encapsulation by defining private attributes.

  • Use property decorators to manage attribute access.

Example Code:

Student Exercises:

  1. Create a class Book with attributes title, author, and price. Implement a method to display book details.

  2. Implement a Vehicle class and create subclasses Car and Bike, each with their own move() method.

  3. Use encapsulation to create a Person class with private attributes and provide getter/setter methods.

  4. Experiment with overriding methods in a derived class.

Last updated