First we need to clarify what the class and object are. Class is a blueprint or template from which objects are created. But objects are instances of a class. For example, let’s have the vehicle class.
Objects: Car, Van, Lorry, …
We can identify four basic concepts in object oriented programming.
Encapsulation: Encapsulation means containing all important information inside an object and only exposing selected information to the outside world. Simply this is called information hiding. We used the private, public, and protected keywords for defining the hiding level of the information. We use attributes and behaviors as information.
According to the vehicle example, vehicle class can contain the following attributes and behaviors.
Attributes: Price, Brand, Color
Behaviors: Drive(), breaks(), accelerate()
Let’s assume that the price of a vehicle needs to be hidden from the outside world. So we can use private keywords for the price attributes and others can use the public keyword. Now the price cannot be accessed from the outside world.
Inheritance: Inheritance is used to inherit the attributes and behaviors from one class to another class. It means if a child class extends the parent class, the child class can have the attributes and behaviors of the parent class. So we call inheritance to support reusability.
According to the previous vehicle example, we can use a vehicle as the parent class and a car as the child class. So if we extend the vehicle class into car class all the attributes and behaviors can have the car class.
Abstraction: Abstraction means that the user interacts with only selected attributes and methods of an object. Abstraction uses simplified, high-level tools, to access a complex object. Usually abstraction can be defined as hiding the lower level from a class.
According to the previous vehicle, example let’s assume the vehicle class is an abstract class. The methods in the vehicle class can be used in their child classes but they hide the implementations from the child class.
Polymorphism: Polymorphism means designing objects to share behaviors. Mainly we use two methods in Polymorphism.
- Method overriding
- Method overloading
Method overriding : In method overriding, a child class can provide a different implementation than its parent class. Method overriding uses runtime polymorphism.
According to the previous example, we can override the drive() method in the vehicle class by using different implementations.
Method overloading: In method overloading, Methods or functions may have the same name, but a different number of parameters passed into the method call. Different results may occur depending on the number of parameters passed in. Method overloading uses the compile time polymorphism.
Benefits of OOP
- Object oriented programming model makes complex scenarios as simple scenarios
- Reusable components across the program
- Allows to have class specific behaviors using polymorphism
- Secure the needed information from encapsulation
Author: Sachith Ariyathilaka