DEV Community

rahul shukla
rahul shukla

Posted on

Oops Concepts

I will explain oops concepts with an example. I will try to explain a simple way. if you have any doubt about any topic regarding oops than I will provide new more example.

  1. Class
  2. Object
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  6. Encapsulation
  7. Association
  8. Aggregation
  9. Composition

Class

A class is a plan
or
A class is a blueprint
or
A class is a template
or
It is only a logical component and not the physical entity.

For example, I want to create 1000 chairs. So if I create a chair with exact parameter width and height. that will take a lot of times. For creating 1000 chairs. if I developed one blueprint or plan or template then it is easy for me to create lots of chairs. using that plain, I can Create 1000 chairs under less time.

or
For example, I want to create 1000 briquettes. if I create every briquette with the same size then it takes a lot of time. if I create a dye or class or plan or blueprint for creating a briquette.that is more helpful for me.

A class works like a dye. We can create a lot of thing with one logic.


Object

An object is remote of a class. using an object, We can access or operate our class like tv or remote. A tv is a class and remote is an object. that access tv.
or
An object can be defined as an instance of a class, and there can be multiple instances of a class in a program. An object contains both the data and the function, which operates on the data. For example - chair, bike, marker, pen, table, car, etc.


Inheritance

Inheritance is an OOPS feature. In inheritance, let take 2 class school and student. I create a school class. in school class, there are some variable school_name, school_Code, school_address. In student, there are some variable student_id, student_name, student_father_name. In a simple case, if I want to add school variable school_name, school_code. in student class then I have to create two variable another.that is not good for us. We should be developed reusable code. in that case, we inherit a class to another class. which class is inherited that is called "child class".that class is inherited. that is called "Parent class".


Polymorphism

The word polymorphism means having many forms.
or
Polymorphism is one of the OOPS features. that allows us to perform a single action in different ways. For example, let say we have a class Animal that has a method sound(). Since this is a generic class so we can’t give it an implementation like Roar, Meow, Oink etc. We had to give a generic message.
Polymorphism can be achieved in two of the following ways:

  1. Method Overloading(Compile time Polymorphism)

  2. Method Overriding(Run time Polymorphism)


Abstraction

Abstraction means to show only the necessary field and hide details.

Consider a real-life example of a man driving a car. The man only knows that pressing the accelerators will increase the speed of the car or applying brakes will stop the car but he does not know about how on pressing accelerator the speed is increasing, he does not know about the inner mechanism of the car or the implementation of an accelerator, brakes etc in the car. This is what abstraction is.

Advantages of Data Abstraction:

  1. Helps the user to avoid writing the low-level code
  2. Avoids code duplication and increases reusability.
  3. Can change the internal implementation of class independently without affecting the user.
  4. Helps to increase the security of an application or program as only important details are provided to the user.

Encapsulation

Encapsulation is defined as wrapping up of data and information under a single unit.

Consider a real-life example of encapsulation, in a company, there are different sections like the accounts section, finance section, sales section etc. The finance section handles all the financial transactions and keeps records of all the data related to finance. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data. This is what encapsulation is. Here the data of the sales section and the employees that can manipulate them are wrapped under a single name “sales section”.


Association

Association is a connection or relation between two separate classes that are set up through their objects. Association relationship indicates how objects know each other and how they are using each other’s functionality. It can be one-to-one, one-to-many, many-to-one and many-to-many.

For example, a person can have only one passport. That is a “one-to-one” relationship.

If we talk about the association between a Bank and Employee, a bank can have many employees, So it is a “one-to-many” relationship.

Similarly, every city exists in exactly one state, but a state can have many cities, which is a “many-to-one” relationship.
Lastly, if we talk about the association between a teacher and a student, multiple students can be associated with a single teacher and a single student can also be associated with multiple teachers but both can be created or deleted independently. This is a “many-to-many” relationship.

As an example, imagine the relationship between a doctor and a patient. A doctor can be associated with multiple patients. At the same time, one patient can visit multiple doctors for treatment or consultation. Each of these objects has its life cycle and there is no “owner” or parent. The objects that are part of the association relationship can be created and destroyed independently.

Let’s take an example of a teacher and student. Multiple students can associate with a single teacher, and a single student can associate with multiple teachers, but both have their own lifecycles (both can be create and delete independently); so when a teacher leaves the school, we don’t need to delete any students, and when a student leaves the school, we don’t need to delete any teachers.


Aggregation

An aggregation is a special form of association. It is a relationship between two classes like association, however its a directional association, which means it is strictly a one-way association. Aggregation is "the" relationship among objects.

Let’s take an example of a cell phone and a cell phone battery. A single battery can belong to a phone, but if the phone stops working, and we delete it from our database, the phone battery will not be deleted because it may still be functional. So in aggregation, while there is ownership, objects have their own lifecycle.


Composition

Composition is about expressing relationships between objects. Think about the chair example. A chair has a Seat. A chair has a back. And a chair has a set of legs. The phrase "has a" implies a relationship where the chair owns, or at minimum, uses, another object. It is this "has a" relationship which is the basis for composition.

if I forgot any point please comment. I will improve my post.

Discussion (1)