OOPS(Object Oriented Programming) Concepts

Maitri Gandhi
5 min readJul 24, 2020

--

Object oriented programming and its 4 basics.

What is OOP(Object Oriented Programming)?

Object oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields which is often known as attributes or properties and code, in the form of procedures which is often known as methods.

Object oriented languages include Java, C++, C#, Python, R, PHP, JavaScript, Ruby, Perl, Object Pascal, Objective-C, Dart, Swift, Scala, Kotlin, Common Lisp, MATLAB, and Smalltalk.

Today we know more about OOPs (Object Oriented Programming ) and also know that why we need this?

Why we need OOP(Object Oriented Programming)?

Object oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism etc in programming. The main aim of OOP is to bind together the data and the functions that operate on them so that no other part of the code can access this data except that function.

OOPs Concepts:

  1. Encapsulation
  2. Abstraction
  3. Polymorphism
  4. Inheritance

So Let’s see all the concepts of OOP(Object Oriented Programming)

Encapsulation:

Suppose, we have a program. It has a few logically different objects which communicate with each other according to the rules defined in the program.

Encapsulation is achieved when each object keeps its state private, inside a class. Other objects don’t have direct access to this state. Instead, they can only call a list of public functions i.e. called methods.

So, the object manages its own state via methods and no other class can touch it unless explicitly allowed. If you want to communicate with the object, you should use the methods provided. But by default, you can’t change the state.

For example:

Abstraction:

Abstraction can be thought of as a natural extension of encapsulation.

In object-oriented design, programs are often extremely large. And separate objects communicate with each other a lot. So maintaining a large codebase like this for years with changes along the way is difficult.abstraction is a concept aiming to ease this problem.

Applying abstraction means that each object should only expose a high-level mechanism for using it.

Think a coffee machine. It does a lot of stuff and makes quirky noises under the hood. But all you have to do is put in coffee and press a button.

Preferably, this mechanism should be easy to use and should rarely change over time. Think of it as a small set of public methods which any other class can call without “knowing” how they work.

Another real time example of abstraction is given below.

Inheritance:

So, we saw how encapsulation and abstraction can help us develop and maintain a big codebase.But do you know what is another common problem in OOP design?

Objects are often very similar. They share common logic. But they’re not entirely the same. So how do we reuse the common logic and extract the unique logic into a separate class? One way to achieve this is inheritance.

It means that you create a (child) class by deriving from another (parent) class. This way, we form a hierarchy.

The child class reuses all fields and methods of the parent class (common part) and can implement its own (unique part).

Here is a simple example of Inheritance:

Polymorphism:

Polymorphism means “many shapes” in Greek.

So we already know the power of inheritance and happily use it. But there comes this problem.

Suppose, we have a parent class and a few child classes which inherit from it. Sometimes we want to use a collection .for example a list which contains a mix of all these classes. Or we have a method implemented for the parent class but we’d like to use it for the children, too.

This can be solved by using polymorphism.

Simply put, polymorphism gives a way to use a class exactly like its parent so there’s no confusion with mixing types. But each child class keeps its own methods as they are.

This typically happens by defining a (parent) interface to be reused. It outlines a bunch of common methods. Then, each child class implements its own version of these methods.

Any time a collection (such as a list) or a method expects an instance of the parent (where common methods are outlined), the language takes care of evaluating the right implementation of the common method regardless of which child is passed.

Let’s see schematic figure of polymorphism:

Example of polymorphism:

Conclusion:

So , It is some basic concepts of OOP(Object Oriented Programming) which is very useful in our day to day life . Based on this we can solve many real time problems and comes into a interesting solution.

Thanks for reading this article if you find anything that could be improved please let me know, I would love to improve.😊

If this article has helped you a bit and found interesting please clap!👏

--

--