As the technology evolved and software became more complex, the need for new and flexible programming modes emerged. The need to create reliable software, reduce overall development and maintenance costs and deliver software in a timely manner led to the development of the object-oriented programming model. The focus of object-oriented programming is on objects.
Any real object that has certain characteristics and behaviors that can be used to describe it is considered an object. There are several objects that have certain common characteristics. They can be grouped into categories or classes. Thus, each object of a class will be treated as an instance of that class. In programming terms, a class is a structure that contains data (characteristics) and methods (behavior) for working with that data.
For example, a Vehicle class vehicle may have characteristics such as color and type, as well as behavior characteristics such as starting, stopping, accelerating, and so on. The following figure shows a Unified Modeling Language (UML) class diagram of a Vehicle class:
Here Fields represent the characteristics and Methods represent the behavior of the object. The Vehicle class can include vehicle types such as motorcycle, bicycle, car, etc. This is described in detail in the following image:
|color = blue||start|
|type = bike||stop|
Here the bicycle object has color=blue and type=bike characteristics with behaviors that include starting, stopping, and accelerating. Similarly, there can be other objects of class “Vehicle”, such as a car, bicycle, etc., with the same or different characteristic values and similar behavior.
An Object-Oriented programming language is based on the following principles:
Encapsulate means to enclose. Encapsulation thus allows you to enclose data members and methods in a closed structure called a class. Encapsulation provides data security by hiding data in such a way that the user cannot directly access class members.
Abstraction is a programming concept in which irrelevant details of an entity are hidden from the user’s view. For example, in the case of a washing machine, the user presses a button on a digital panel only to set up the process and start the machine. However, the inner workings of the washing machine are unknown to the user. This means that a non-essential aspect of how the washing machine washes clothes is abstracted from the user. Similarly, abstraction can also be implemented in code to hide unnecessary details from the user.
To inherit means to acquire some characteristic or property from an ancestor. For example, a child acquires certain aspects of appearance and behavior from its biological parents. In programming, inheritance also plays a similar role. It allows common characteristics and behaviors of objects to be combined into a parent class, also called a superclass. This class can be inherited by other classes, allowing the developer to extend and reuse the capabilities of existing classes. New/inherited classes are called child classes, derived classes, or subclasses.
A polymorph is a combination of the words “poly” meaning “many” and “morph” meaning “forms”. This polymorph is an object that can have multiple shapes/behaviors? For example, a chameleon can change its color depending on its environment to protect itself from predators. In programming, polymorphism is the ability of an object to behave differently depending on requirements. Polymorphism can be implemented in several ways in programming, depending on the programming language used.