Skip to content

Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm that uses objects as the basic units of program design. An object encapsulates both data and functions that operate on that data.

In contrast, procedural programming views a computer program as a collection of commands executed in sequence. To simplify program design, procedural programming breaks functions into sub-functions, thus reducing the complexity of the system by dividing large functions into smaller, manageable ones.

Object-oriented programming, on the other hand, considers a program as a collection of objects. Each object can receive messages from other objects and process those messages, making the execution of the program a series of message passing between objects.

In Python, all data types can be considered objects, and you can also define your own objects. The user-defined object data type corresponds to the concept of a class.

Example: Student Grades

Let's illustrate the differences between procedural programming and object-oriented programming using an example involving student grades.

In procedural programming, a student's grade can be represented using a dictionary:

python
std1 = { 'name': 'Michael', 'score': 98 }
std2 = { 'name': 'Bob', 'score': 81 }

You could handle the printing of a student's grade with a function:

python
def print_score(std):
    print('%s: %s' % (std['name'], std['score']))

With object-oriented programming, instead of focusing on the flow of the program, you would think about how a Student data type should be represented as an object. This object would have properties such as name and score. To print a student's grade, you first create the student object and then send a print_score message to that object, allowing the object to print its own data.

Here’s how you would define a Student class:

python
class Student(object):
    def __init__(self, name, score):
        self.name = name
        self.score = score

    def print_score(self):
        print('%s: %s' % (self.name, self.score))

You can then create instances of the Student class and call the method to print their scores:

python
bart = Student('Bart Simpson', 59)
lisa = Student('Lisa Simpson', 87)
bart.print_score()
lisa.print_score()

Class and Instance

The design philosophy of object-oriented programming stems from concepts found in nature. In nature, the concepts of class and instance are quite intuitive. A class (e.g., Student) is an abstract concept that represents the idea of a student, while instances (e.g., Bart Simpson and Lisa Simpson) are specific examples of that class.

Thus, the object-oriented design approach involves abstracting a class and then creating instances based on that class.

Advantages of Object-Oriented Programming

OOP allows for a higher level of abstraction than procedural programming, as a class can encapsulate both data and methods that operate on that data.

Summary

The three main features of object-oriented programming are data encapsulation, inheritance, and polymorphism, which will be discussed in detail later.

Object-Oriented Programming has loaded