Data abstraction
Data abstraction is an essential part of object-oriented programming. It provides only the essential information to the outside world while hiding background details or implementation. This helps simplify the object and its task.
Example: A driver knows pressing the accelerator increases speed, but doesn't need to know the car's internal engine mechanisms. Including only critical data necessary for the object to perform a task will make it easier to make future changes and additions.
Encapsulation
Encapsulation explains the wrapping of data under a single unit. It is the mechanism that binds code and the data it manipulates.
The variables or data of a class are hidden from other classes and can only be accessed through member functions of their own class. This is also known as data-hiding.
It provides enhanced security and helps prevent unintended data corruption.
Example: A finance executive needs sales data for a report. They have to contact someone in the sales organization to access that information from their customer relationship management system (CRM) rather than directly accessing it themselves.
Inheritance
This pillar refers to a class's capability to derive properties and characteristics from another class. Inheritance enables code reuse, as properties and functions can be inherited rather than written repeatedly, reducing redundancy and saving time.
Example: An employee might inherit variables and characteristics from a general person.
Polymorphism
Polymorphism means "to have many forms." It explains that the same object can have multiple functions depending on the context.
Polymorphism allows a single method call to produce a different result depending on the object it's acting on. It allows a function to work with a specific interface, uniformly manipulating entities of different classes.
Example: A single person can simultaneously be a father, husband, and employee, exhibiting different behaviors in different contexts. If you have circle and square objects, they could fall into a shape class, with a draw method that can be called for both.
Other key principles of OOP include: class, object, dynamic binding, and message passing.
Object
An object is the basic unit of object-oriented programming. It represents real-life or abstract entities and is an instance of a class. Memory can be allocated only when an object is created.
Objects interact by sending and receiving messages, needing to know only the type of message accepted and the response returned, not each other’s internal details.
Example: A dog is a real-life object with characteristics like color and breed, and behaviors like barking, sleeping, and eating.
Class
Similar to inheritance, objects are categorized into classes. A class serves as a blueprint for individual objects, defining a set of properties or methods common to all objects of that type. Consists of data members (variables and attributes) and member functions (methods).
Example: A car class might define properties like four wheels, speed limit, and range.
Dynamic binding
Dynamic binding is the mechanism by which the code to be executed for a function call is determined at run time. This means the exact code associated with a procedure call is not known until the time of the call.
Dynamic binding often works with inheritance and polymorphism, allowing an object to search its parent class hierarchy to find and execute the correct version of that method. This flexibility enables an object to take on different behaviors at run time.
Example: Vehicle is your base class, and from that, you have two derived classes, car and motorcycle. Both have shared characteristics, like starting an engine to operate, but the way they perform that action is unique.
Message passing
Message passing is used in OOP, where objects interact by sending and receiving information to each other. It involves specifying the object's name, the function's name, and the information to be sent.
Example: A driver would be an object that interacts with another object, a vehicle.
Advantages of OOP