OOPs, which stands for “Object-Oriented Programming,” is a programming paradigm or methodology used in software development. It is based on the concept of “objects,” which are instances of classes that encapsulate data (attributes) and behaviour (methods) into a single unit. OOPs is one of the most popular and widely used programming paradigms, and it provides several key principles and concepts, including:
Structure of OOPs
The structure, or building blocks, of object-oriented programming include the following:
- Classes are user-defined data types that act as the blueprint for individual objects, attributes and methods.
- Objects are instances of a class created with specifically defined data. Objects can correspond to real-world objects or an abstract entity. When the type is defined initially, the description is the only object that is defined.
- Methods are functions defined inside a class that describe the behaviours of an object. Each method contained in class definitions starts with a reference to an instance object. Additionally, the subroutines contained in an object are called instance methods. Programmers use methods for reusability or keeping functionality encapsulated inside one object at a time.
- Attributes are defined in the class template and represent the state of an object. Objects will have data stored in the attributes field. Class attributes belong to the class itself.
Pillar 1: Abstraction
- Data Abstraction is the belongings with the aid of virtue of which handiest the crucial info are displayed to the user. The trivial or non-critical gadgets are not exhibited to the person. Ex: An automobile is regarded as a vehicle rather than its personal components.
- Data Abstraction can also be defined as the manner of figuring out most effective the desired traits of an object, ignoring the beside-the-point information. The properties and behaviours of an object differentiate it from other objects of comparable type and also help in classifying/grouping the item.
- Consider an actual lifestyle example of a man using a vehicle. The guy is aware that pressing the accelerator will grow the car velocity or that applying brakes will stop the auto, but he does no longer recognize how on pressing the accelerator, the rate is clearly increasing.
- He does not realize the inner mechanism of the automobile or the implementation of the accelerators, brakes etc. In the automobile. This is what abstraction is.
Pillar 2: Encapsulation
It is defined as the wrapping up of data under a single unit. It is the mechanism that binds together the code and the data it manipulates. Another way to think about encapsulation is that it is a protective shield that prevents the data from being accessed by the code outside this shield.
- Technically, in encapsulation, the variables or the data in a class is hidden from any other class and can be accessed only through any member function of the class in which they are declared.
- In encapsulation, the data in a class is hidden from other classes, which is similar to what data hiding does. So, the terms “encapsulation” and “data-hiding” are used interchangeably.
- Encapsulation can be achieved by declaring all the variables in a class as private and writing public methods in the class to set and get the values of the 8.
Pillar 3: Inheritance
Inheritance is an important pillar of OOP (Object Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features (fields and methods) of another class. We are achieving inheritance by using the extends keyword. Inheritance is also known as an “is-a” relationship.
Let us discuss some frequently used important terminologies:
- Superclass: The class whose features are inherited is known as superclass (also known as base or parent class).
- Subclass: The class that inherits the other class is known as a subclass (also known as derived or extended or child class). The subclass can add its own fields and methods in addition to the superclass fields and methods.
- Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.
Pillar 4: Polymorphism
It refers to the ability of object-oriented programming languages to differentiate between entities with the same name efficiently. This is done by Java with the help of the signature and declaration of these entities. The ability to appear in many forms is called polymorphism.
Polymorphism, frequently associated with its organic means of genetic diversity, also can be implemented metaphorically to describe a charming factor of human behaviour and adaptableness. In the world of human interactions and society, polymorphism refers to the first-rate ability of people to display an extensive variety of behaviours, roles, and characteristics.
Much just like the manner genetic polymorphism allows for distinct physical trends inside a species, human polymorphism manifests in our capability to conform to diverse conditions and roles. It’s a testament to our versatility and potential to thrive in numerous environments.
In the grand symphony of life, the principles of Object-Oriented Programming (OOP) provide a profound lens through which we can understand the complexities of human behaviour and interaction. Just as OOP brings order and structure to the digital world, humans exhibit a remarkable innate capacity for encapsulation, inheritance, abstraction, and polymorphism within the intricate fabric of society.
FAQs About OOPs
OOP is a programming paradigm that uses objects, which are instances of classes, to model real-world entities and their interactions. It emphasizes encapsulation, inheritance, abstraction, and polymorphism to organize and structure code.
A class is a blueprint or template for creating objects. It defines the attributes (data) and methods (functions) that objects of that class will have. Objects are instances of a class.
Encapsulation is the concept of bundling data (attributes) and the methods (functions) that operate on that data into a single unit (an object).