OOP: Everything you need to know about Object Oriented Programming

Skrew Everything
Mar 21, 2018 · 12 min read

What is Object Oriented Programming (OOP)?

An object-oriented programming is a way programming which enables programmers to think like they are working with real-life entities(a thing with distinct and independent existence) or objects. In real-life, people have knowledge and can do various works. In OOP, objects have fields to store knowledge/state/data and can do various works — methods.

Basic Terminologies:


It’s the instance of a class/ it’s the working entity of a class.


It is a template or blue print about the capability of what an object can do.


The behaviours of a class. It tells what a method can do.


Object and Instance both are same with small difference.

A blueprint for a house design is like a class description. All the houses built from that blueprint are objects of that class. A given house is an instance.

The creation of instance is called instantiation!

Instance != Instance Variable:

Instance variables belong to an instance of a class. Another way of saying that is instance variables belong to an object, since an object is an instance of a class. Every object has it’s own copy of the instance variables.

OOPs Concepts:

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

NOTE: Exceptions are not part of OOPs concepts.


Encapsulation is a mechanism of wrapping the data (instance variables) and code acting on the data (methods) together as a single unit like a Class.

The main purpose of encapsulation is you would have full control on data by using the code.

In encapsulation, the variables of a class can be made hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.

Still didn’t understand?

Encapsulation can be described as a protective barrier that prevents the code and data being randomly accessed by other code defined outside the class. Access to the data and code is tightly controlled by a class.

Data Hiding/Data Encapsulation:

Encapsulation leads to data hiding.

We use access specifiers to hide the data(properties) and methods. Use of private and public comes under this.

The user can only perform a restricted set of operations on the hidden members of the class by executing special functions commonly called methods


Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user.

In other words, the user will have the information on what the object does instead of how it does it.

By hiding complex steps/details/computations/statements inside your classes and creating public methods to access them. Ex: Wrapper Classes

Abstraction is a very broad topic. Above can be seen as basic definition to say whenever anyone asks.

If we want to go deep, Abstraction mainly depends on point of view and many definitions.

  • Hiding unnecessary details;
  • Can have multiple implementations;( like dealing with idea instead of the actual event(like When you know something needs to be there but not sure how exactly it should look like.))
  • Building complex system by splitting the complexity level by level.

Hiding unnecessary details:

Every function can be seen as basic abstraction.

For example: A programmer doesn’t care about how malloc() works or how garbage collection works.

Can have multiple implementations:

Don’t get confused this with polymorphism.

Abstraction is outlined by the top left and top right images of the cat. The surgeon and the old lady designed (or visualized) the animal differently. In the same way, you would put different features in the Cat class, depending upon the need of the application.

Every cat has a liver, bladder, heart and lung, but if you need your cat to ‘purr’ only, you will abstract your application’s cat to the design on top-left rather than the top-right.

Still didn’t get it?

An abstraction is a concept that can have one or more implementations.

For example: There are many different ways that your computer can connect to a local network (Ethernet, Wi-Fi, dial-up modem, etc.), but your Web browser doesn’t generally have to worry about which of these you’re using at any given time, because lower-level software provides a common abstraction that your browser can rely on. So “connection to the network” is the abstraction, and Ethernet and Wi-Fi and so on are implementations of that abstraction.

You’ll notice that so far I haven’t mentioned object-oriented programming at all. That’s because the idea of implementing abstractions is common to all sorts of designs and architectures; it’s not specific to OOP.

In object-oriented programming, where an implementation is likely to be represented as an object specified by a class, it’s common for an abstraction to be specified by an abstract class or an interface. But this does not mean that abstract classes and interfaces are only for specifying abstractions, and conversely, it doesn’t mean that all abstractions are specified by abstract classes and interfaces.

For example, the famous Design Patterns (the singleton pattern, the factory method pattern, etc.) are all abstractions that are not explicitly specified in code; rather, we have separate (unrelated) pieces of code for the various implementations of these abstractions, and the abstraction merely resides in the programmer’s head (and in design documentation, and in the names of classes and variables and so on).

The abstraction is for clients (users) of the class. There needs to be an implementation, of course; but, for example, code that accepts a List<String> doesn’t have to worry about whether the passed-in instance is an ArrayList or a LinkedList , because they both implement the required abstraction.

Building complex system by splitting the complexity level by level:

Abstraction in Object Oriented Programming helps to hide the irrelevant details of an object.

Abstraction is separating the function and properties that logically can be separated to a separate entity which the main type depends on.

This kind of Abstraction helps in separating the members that change frequently.

Abstraction is one of the key principles of the OOAD(Object oriented analysis and Design).

Applying Abstraction during the design and domain modelling, helps a lot in design the a system which is flexible and maintainable.

Abstraction can be achieved by Composition.

Abstraction Example:
A Car has Engine, wheels and many other parts. When we write all the properties of the Car, Engine, and wheel in a single class, it would look this way:

public class Car 
int price;
String name;
String color;
int engineCapacity;
int engineHorsePower;
String wheelName;
int wheelPrice;
void move()
//move forward
void rotate()
//Wheels method
void internalCombustion()
//Engine Method

In the above example, the attributes of wheel and engine are added to the Car type. As per the programming, this will not create any kind of issues. But when it comes to maintenance of the application, this becomes more complex.

Abstraction has three advantages:
1. By using abstraction, we can separate the things that can be grouped to another type.

2. Frequently changing properties and methods can be grouped to a separate type so that the main type need not under go changes. This adds strength to the GOAD principle -”Code should be open for Extension but closed for Modification”.

3. Simplifies the representation of the domain models.

Applying the abstraction with composition, the above example can be modified as given below:

public class Car 
Engine engine = new Engine();
Wheel wheel = new Wheel();
int price;
String name;
String color;
void move()
//move forward

public class Engine
int engineCapacity;
int engineHorsePower;

void internalCombustion()
//Engine Method
public class Wheel
String wheelName;
int wheelPrice;

void rotate()
//Wheels method

You can see that the attributes and methods related to the Engine and Wheel are moved to the respective classes.
Engine and Wheel are referred from the Car type. When ever an instance of Car is created, both Engine and Wheel will be available for the Car and when there are changes to these Types(Engine and Wheel), changes will only be confined to these classes and will not affect the Car class.

Abstraction is one of the fundamental principles of Object Oriented Programming languages. It helps to reduce the complexity and also improves the maintainability of the system. When combined with the concepts of the Encapsulation and Polymorphism, Abstraction gives more power to the Object oriented programming languages.

Abstraction is of 2 types:

  1. Control Abstraction
  2. Data Abstraction

Control Abstraction:

Programming languages offer control abstraction as one of the main purposes of their use. Computer machines understand operations at the very low level such as moving some bits from one location of the memory to another location and producing the sum of two sequences of bits. Programming languages allow this to be done in the higher level. For example, consider this statement written in a Pascal-like fashion:

a := (1 + 2) * 5

To a human, this seems a fairly simple and obvious calculation (“one plus two is three, times five is fifteen”). However, the low-level steps necessary to carry out this evaluation, and return the value “15”, and then assign that value to the variable “a”, are actually quite subtle and complex. The values need to be converted to binary representation (often a much more complicated task than one would think) and the calculations decomposed (by the compiler or interpreter) into assembly instructions (again, which are much less intuitive to the programmer: operations such as shifting a binary register left, or adding the binary complement of the contents of one register to another, are simply not how humans think about the abstract arithmetical operations of addition or multiplication). Finally, assigning the resulting value of “15” to the variable labeled “a”, so that “a” can be used later, involves additional ‘behind-the-scenes’ steps of looking up a variable’s label and the resultant location in physical or virtual memory, storing the binary representation of “15” to that memory location, etc.

Without control abstraction, a programmer would need to specify all the register/binary-level steps each time they simply wanted to add or multiply a couple of numbers and assign the result to a variable. Such duplication of effort has two serious negative consequences:

  1. It forces the programmer to constantly repeat fairly common tasks every time a similar operation is needed
  2. It forces the programmer to program for the particular hardware and instruction set

Data Abstraction:

Abstraction of data structures. Data abstraction refers to defining the behaviour of the data structure. Data may be internally represented in different ways in concrete implementations.


Data abstraction is the way to create complex data types and exposing only meaningful operations to interact with data type, where as hiding all the implementation details from outside works.

Benefit of this approach involves capability of improving the implementation over time e.g. solving performance issues is any. The idea is that such changes are not supposed to have any impact on client code, since they involve no difference in the abstract behaviour.


  • In DBMS: Database Management Systems have abstractions of Table and View. Every DBMS software may store underlying data differently. But to users, Table is always represented as Rows and Columns. This allows performing high level operations like query on this data abstraction.
  • In Java Collections: Collection API’s Collection, List, Set and Map interfaces are example of data abstractions. These interfaces define some of the behaviour characteristics to be implemented by concrete classes. For example, List defines characteristics and methods for random access from the underlying elements. Implementing class may choose to use array data structure for storing the data elements — ArrayList. Or it can be stored in any ad-hoc manner — LinkedList. Anything should be fine as long as the behaviour defined by List interface is achieved.

In short,

  • Abstractions are ideas, not specific events
  • To abstract something is to move away from its implementation ( interface and abstract class) and think about big ideas
  • Abstractions can be used to organize code (and many other things) effectively
  • Object-oriented programming is entirely dependent on the abstractions.
  • Abstraction is the practice of breaking a large problem into smaller components, so each smaller problem can be worked on in (relative) isolation.

Next you might think about Encapsulation vs. Abstraction

  • The difference between concepts of encapsulation and abstraction is that encapsulation is about the packaging of the class (like how data should be accessed (setters/getters) and what data should be accessed (access specifiers)), whereas abstraction more about what the class does for you at a conceptual level.
  • Encapsulation is hiding unnecessary data in a capsule or unit and Abstraction is showing essential feature of an object
  • Abstraction encompasses encapsulation, information hiding, and generalisation.
  • abstraction is a form of generalisation: writing code that applies to more than one specific situation.
  • Abstraction is where you focus on only those details that are important for your purpose.
  • Abstraction is the process of refining away all the unneeded/unimportant attributes of an object and keep only the characteristics best suitable for your domain.E.g. for a person: you decide to keep first and last name and SSN. Age, height, weight etc are ignored as irrelevant.
  • Assembly language can be thought of as an abstraction of machine code — assembly expresses the essential details and structure of the machine code, but frees you from having to think about the opcodes used, the layout of the code in memory, making jumps go to the right address, etc.
  • Your operating system’s API is an abstraction of the underlying machine. Your compiler provides a layer of abstraction which shields you from the details of assembly language. The TCP/IP stack built into your operating system abstracts away the details of transmitting bits over a network. If you go down all the way to the raw silicon, the people who designed your CPU did so using circuit diagrams written in terms of “diodes” and “transistors”, which are abstractions of how electrons travel through semiconductor crystals.
  • Inheritance is also an example of abstraction.
  • Encapsulation leads to Abstraction


Inheritance can be defined as the process where one class acquires the properties (methods and fields) of another.


In object-oriented programming, inheritance is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class) specifying implementation to maintain the same behavior

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.

Types Of Inheritance:

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multi-Level Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

NOTE: Most of the languages like Java, JavaScript etc doesn’t support Multiple Inheritance. One of the problems we face while using it is Diamond Problem. Without Multiple Inheritance, Hybrid Inheritance is not possible.

Advantages of inheritance are as follows:

  • Inheritance promotes reusability. When a class inherits or derives another class, it can access all the functionality of inherited class.
  • Reusability enhanced reliability. The base class code will be already tested and debugged.
  • As the existing code is reused, it leads to less development and maintenance costs.
  • Inheritance makes the sub classes follow a standard interface.
  • Inheritance helps to reduce code redundancy and supports code extensibility.
  • Inheritance facilitates creation of class libraries.

Disadvantages of inheritance are as follows:

  • Inherited functions work slower than normal function as there is indirection.
  • Improper use of inheritance may lead to wrong solutions.
  • Often, data members in the base class are left unused which may lead to memory wastage.
  • Inheritance increases the coupling between base class and derived class. A change in base class will affect all the child classes.

Inheritance is one of the most mis-used feature of OOPs by beginner programmers.

TIP: Unless there is an absolute need for an inheritance, don’t use it!


In object-oriented programming, polymorphism (from the Greek meaning “having multiple forms”) is the characteristic of being able to assign a different meaning or usage to something in different contexts — specifically, to allow an entity such as a function, or an object to have more than one form.

There are 2 types of polymorphism implementations:

  1. Static Polymorphism
  2. Dynamic Polymorphism

Features of polymorphism mainly depends on the programming languages.

Like, function overloading is possible in Java, C++ etc., but not possible in JavaScript. Operator overloading is only possible in C++.

So, I highly recommend to read about specific polymorphism depending upon the language you use.

For basic idea what polymorphism is:

If anybody says CUT to these people

  1. The Surgeon
  2. The Hair Stylist
  3. The Actor

What will happen?

  • The Surgeon would begin to make an incision.
  • The Hair Stylist would begin to cut someone’s hair.
  • The Actor would abruptly stop acting out of the current scene, awaiting directorial guidance.

So above representation shows What is polymorphism (same name, different behaviour) in OOP.

Got any doubts/questions/suggestions? Comment down below.

From The Scratch

Everything From Scratch related to programming for people who are curious to know how things work from bottom.

Skrew Everything

Written by

I just do things! My GitHub: github.com/SkrewEverything

From The Scratch

Everything From Scratch related to programming for people who are curious to know how things work from bottom.