Lecture notes on Class 20: Introduction to Object-Oriented Programming (OOP) - Part 2

Rashmi Mishra
0

 

Lecture notes on Class 20

Introduction to Object-Oriented Programming (OOP) Part 2

Objective

  • Explore class properties and methods.
  • Learn about access modifiers.

Learning Outcomes

By the end of this class, students will be able to:

  • Define and use class properties and methods.
  • Apply appropriate access modifiers to control access to properties and methods.

1. Recap of Object-Oriented Programming (OOP)

In our previous class, we covered the basics of Object-Oriented Programming (OOP), including the concepts of classes and objects. Here’s a brief recap:

  • Class: A blueprint for creating objects. It encapsulates data for the object.
  • Object: An instance of a class containing properties and methods.

2. Class Properties

2.1 Definition

Properties (also known as attributes or fields) are variables that hold the state of an object. Each object can have its own unique values for these properties.

2.2 Defining Class Properties

To define properties in a class, you typically specify the data type and name of the property. Here’s how you can do this in a sample class:

class Car {

    // Defining properties

    public $color;

    public $model;

    private $engineType;

}

In this example, we defined three properties: color, model, and engineType.


3. Class Methods

3.1 Definition

Methods are functions defined within a class that operate on the properties of the class. They define the behaviors of the objects created from the class.

3.2 Defining Class Methods

Methods can be defined as follows:

class Car {

    public $color;

    public $model;

 

    // Method to display car details

    public function displayDetails() {

        return "Model: $this->model, Color: $this->color";

    }

}

3.3 Accessing Properties in Methods

You can access class properties within methods using the $this keyword. For example:

$myCar = new Car();

$myCar->color = "Red";

$myCar->model = "Toyota";

echo $myCar->displayDetails(); // Output: Model: Toyota, Color: Red


4. Access Modifiers

Access modifiers determine the visibility of class properties and methods. In PHP, there are three main access modifiers:

4.1 Public

  • Properties and methods declared as public can be accessed from anywhere outside the class.
  • Example:

class Car {

    public $color;

 

    public function getColor() {

        return $this->color;

    }

}

 

$myCar = new Car();

$myCar->color = "Blue"; // Accessible

echo $myCar->getColor(); // Output: Blue

4.2 Private

  • Properties and methods declared as private can only be accessed within the class itself.
  • Example:

class Car {

    private $engineType;

 

    private function setEngineType($type) {

        $this->engineType = $type;

    }

 

    public function configureEngine($type) {

        $this->setEngineType($type); // Accessible within the class

    }

}

4.3 Protected

  • Properties and methods declared as protected can be accessed within the class itself and by subclasses (child classes).
  • Example:

class Vehicle {

    protected $fuelType;

 

    protected function setFuelType($type) {

        $this->fuelType = $type;

    }

}

 

class Car extends Vehicle {

    public function configureFuel($type) {

        $this->setFuelType($type); // Accessible in child class

    }

}


5. Summary

  • We learned about class properties and methods, and how to define them in a class.
  • We discussed the three access modifiers: public, private, and protected, and their significance in controlling access to class members.

6. Practical Exercise

Task:

  1. Create a class called Book with the following properties:
    • title (public)
    • author (public)
    • isbn (private)
  2. Add methods to:
    • Display book details (public).
    • Set the ISBN (private).
  3. Create an object of the Book class, set its title and author, and call the method to display the details.

Example Template:

class Book {

    public $title;

    public $author;

    private $isbn;

 

    // Methods go here

}

 

// Create a Book object and use it


7. Homework

  1. Research additional access modifiers or visibility features in other programming languages (like Java or C#).
  2. Write a short essay on the importance of encapsulation in OOP.

References




Post a Comment

0Comments

Post a Comment (0)