3. Topic 3(COLON) Data Structures and Programming Paradigms

Lesson 3.4: Object-oriented Programming

#### Lesson focus #### Learning outcomes Students should be able to:.

Lesson 3.4: Object-Oriented Programming

Introduction

Welcome to Lesson 3.4 of Foundation Computing! Today, we're diving into Object-Oriented Programming (OOP), a fundamental programming paradigm that allows you to model real-world entities using code. 💻

Objectives

By the end of this lesson, you should be able to:

  • Understand the concept of classes and objects.
  • Explain attributes and methods, and how to use the constructor (__init__).
  • Describe encapsulation and its importance.
  • Create multiple instances from a class and model real-world entities.
  • Understand the basics of inheritance and code reuse.

What is Object-Oriented Programming?

Object-Oriented Programming is a way to structure your code to model real-world problems better. Think of it as building a digital version of something tangible, like a car or a student.

Classes and Objects

  1. Classes: Think of a class as a blueprint. Just like you have a blueprint to build a house, a class defines the structure of an object.
  • Example:
        class Car:
            def __init__(self, make, model):
                self.make = make
                self.model = model

Here, Car is a class that describes what a car is, including its properties like make and model.

  1. Objects: When you use a class to create an instance, you create an object. Each object can have different values for its attributes.
  • Example:
        my_car = Car('Toyota', 'Corolla')
        your_car = Car('Honda', 'Civic')

In this case, my_car and your_car are both objects of the Car class, but they have different makes and models.

Attributes and Methods

  • Attributes: These are like properties of an object. In our Car example, make and model are attributes.
  • Methods: Functions that belong to the class, allowing actions to be performed with the object's data.
  • Example:
        class Car:
            def __init__(self, make, model):
                self.make = make
                self.model = model
            def display_info(self):
                return f'Car make: {self.make}, Model: {self.model}'

The display_info method lets us output the car's information. To use it:

        print(my_car.display_info())

Encapsulation

Encapsulation is the principle of bundling data (attributes) and behaviors (methods) together. It helps manage complexity by hiding the internal workings of classes and exposing only what is necessary. 🛡️

  • Example:

Using access modifiers:

    class Car:
        def __init__(self, make, model):
            self.__make = make  # private attribute
            self.__model = model  # private attribute
        def display_info(self):
            return f'Car make: {self.__make}, Model: {self.__model}'

By using double underscores (__), we make the make and model attributes private. They cannot be accessed directly from outside the class, which protects the integrity of the data.

Creating Multiple Objects

One of the key features of OOP is creating multiple instances (objects) from one class. Each object can have its unique properties, reflecting various real-world scenarios.

  • Example:
    car1 = Car('Ford', 'Fiesta')
    car2 = Car('BMW', 'M3')

Here, car1 and car2 are instances of the Car class but represent different cars.

Introduction to Inheritance

Inheritance is a way to create a new class using the characteristics of an existing class. This encourages code reuse and helps in organizing code effectively. Think of it like inheriting traits from your parents. 🌳

  • Example:
    class ElectricCar(Car):
        def __init__(self, make, model, battery_size):
            super().__init__(make, model)
            self.battery_size = battery_size
        def display_battery_info(self):
            return f'Battery size: {self.battery_size} kWh'

The ElectricCar class inherits from the Car class, and we can add specific features like battery_size. To create an electric car instance:

    tesla = ElectricCar('Tesla', 'Model S', 100)
    print(tesla.display_info())
    print(tesla.display_battery_info())

Conclusion

In this lesson, we covered the foundations of Object-Oriented Programming. We explored the concepts of classes, objects, attributes, and methods. We also discussed encapsulation and inheritance in detail. Understanding these concepts will help you structure your code more efficiently as you progress in your programming journey! 🚀

Study Notes

  • Classes serve as blueprints for creating objects.
  • Objects are instances of classes that can have different attributes.
  • Attributes are data properties, while methods are actions associated with the class.
  • Encapsulation hides internal data and allows controlled access.
  • Inheritance enables classes to share methods and attributes, enhancing code reuse.
  • Use the __init__ method to initialize attributes when creating an object.

Practice Quiz

5 questions to test your understanding

Lesson 3.4: Object-oriented Programming — Computing | A-Warded