Principles of Object Oriented Programming

Get introduced to Object Oriented Programming (OOP) and learn about its principles.

Introduction

Object-Oriented Programming (OOP) is a programming style that uses “objects” to design software. Imagine objects as real-world entities like cars, people, or even numbers. In Object-Oriented Programming (OOP), these objects have attributes (properties) and methods (behaviors). For example, a car object might have attributes like color, make, and model, and methods like starting, stopping, and accelerating.

How is it different from procedural programming?

Object-Oriented Programming (OOP) and Procedural Programming are two different programming paradigms. Procedural programming focuses on writing procedures or functions that operate on teh data. It follows a step-by-step approach, where teh main emphasis is on functions and the sequence of actions. In contrast, OOP focuses on objects that encapsulate both teh data and the functions that work on that data, leading to better organization and reuse of code.

Here are some of the key differences of both approaches:

  • Data and Functionality: Procedural programming separates data and functions, while OOP combines them within objects.

  • Modularity: OOP allows for better modularity through classes, making maintaining and expanding the software easier.

  • Code Reusability: OOP promotes code reusability through inheritance, where new classes can inherit properties and methods from existing ones.

Principles of Object-Oriented Programming

Object-Oriented Programming (OOP) is built upon four fundamental principles listed below:

  • Encapsulation

  • Abstraction

  • Inheritance

  • Polymorphism

Let's have a look at all of these principles individually.

Encapsulation

Encapsulation encloses the data (attributes) and the methods (functions) that operate on the data into a single unit called a class. By hiding the implementation details, encapsulation protects the data from accidental modification.

For Example: A Car class encapsulates properties like color, model, and year of the car along with methods like start(), stop(), and accelerate(). These details are hidden within the class, and other parts of the program interact with the car object through the public (accessible) interface of the class.

Abstraction

Abstraction involves hiding the complex details and showing only the necessary features of an object. It helps us to manage complexity by providing a simplified interface.

For Example: A Car class can have a method like fuel_comsumption that hides all the complex details and the complex internal workings. Abstraction allows us to hide this detail and provide a simple interface like start() and stop() for users.

Inheritance

Inheritance allows us to create a new (derived) class using an existing (base) class. This derived class inherits the attributes and methods from the base class and can add its own attributes and methods. This helps us in code reusability and establishing a hierarchical relationship.

For Example: A SportsCar class can inherit from a Car class, inheriting properties like color and model but adding specific attributes like turbo and methods like drift().

Polymorphism

Polymorphism means “many forms.” In Object-Oriented Programming, t allows objects of different types to be treated as if they were of the same type. This enables flexible and adaptable code.

For Example: A Car class might have a method start(). The subclasses of Car , e.g., Convertible, SUV, and SportsCar can each have their way of implementing the draw() method.

Object-Oriented Programming helps in organizing software design by using objects and the four core principles of encapsulation, abstraction, inheritance, and polymorphism. These principles make the code more manageable and scalable.

Create a free account to access the full course.

Continue your learning journey with a 14-day free trial.

By signing up, you agree to Educative's Terms of Service and Privacy Policy