Object-Oriented Programming (OOP)

Object-Oriented Programming (OOP)

What Is Object-Oriented Programming?

Object-Oriented Programming allows us to design and develop a software application using real-world entities such as school, student, course, employer, employee, salary as objects. In Object-Oriented Programming, a class represents a real-world entity. A class is a template from which an object is created. A class will specify the features and tasks of the object. These features and tasks are known as attributes and methods. An object is created from a class and will have all the features and tasks of the class. For example, a Student class can have a student number, name, last name, and course as features. The Student class can have tasks that search courses, find student by student number, or calculate the student grades. We can create many student objects from the student class. Object-Oriented Programming is used in Object-Oriented Programming languages such as Java, Python, C#, C++, etc.

Object-Oriented Programming Concepts

1. Abstraction

Abstraction is hiding complex code implementation and only exposing the functionality. A real-life example of abstraction is the sending of an email. As a sender, we don't care about the internal mechanism of sending an email. All we care about is when we provide the correct email address, email content, and press send, the email is sent to the intended recipient. We only know how the sending of email works and not how it sends the email. The benefit of abstraction is flexibility and scalability.

2. Inheritance

Inheritance is when one object inherits the features and tasks of another object. The object that inherits from another object is called a child object and the object whose features and tasks are inherited is called a parent object. Inheritance allows the child object to use the parent object's features and task. For example, a person object can have a name and last name. A student is a person, therefore a student object can inherit the name and the last name from the person object. An employee is also a person, therefore an employee's object can inherit the name and the last name from the person object. Both student and employee will reuse person's features. The benefit of inheritance is the reusability of code.

3. Encapsulation

Encapsulation is hiding an object's features from being accessed directly. The purpose of encapsulation is to prevent unauthorized changes to an object’s data. We achieve encapsulation by creating a class and making the features private. Public methods such as getters and setters (also known as accessors and mutators), are used to provide access to these hidden features. A getter/accessor method retrieves data from an object. A setter/mutator method modifies the data of an object.

4. Polymorphism

Polymorphism is the ability of an object to perform a task in different ways or forms. There are two ways to achieve polymorphism: method overloading and method overriding. Method overloading is when an object has two methods with the same name and return type, but different parameters. For example, we can retrieve a student by a (student number) or by a (name, last name, and date of birth). Method overriding is achieved through inheritance when a child object has the same method as the parent object but the execution is different.

Object-Oriented Programming enables us to implement software programs that are maintainable, reusable, and flexible. The code is easy to read, follow, and understand. It is easier to achieve software programs that are high in cohesion and loosely coupled. Object-Oriented Programming allows us the ability to add new functionality without affecting current functionality.