OOP in a nutshell

Asela Dasanayaka
3 min readFeb 25, 2018

--

According to Wikipedia Object-oriented programming (OOP) is a programming paradigm based on concepts of objects. The object may contain data as a form of instance variables and behaviours in form of methods.

First, you need to understand two concepts class and objects.

Well, Class is the blueprint of all the objects and objects are one specific representation of the class.

Objects vs Class

There are four main OOP concepts.

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

For understanding the concepts we will follow following diagram.

OOP concepts

Inheritance

Inheritance means sub-class inherits from the super-class. In animal class, there are methods and attributes that are common to all animals by using inheritance concept other child classes can use those attributes and methods in the parent class.

As example lion has a picture, locations and lion eat() etc.

Encapsulation

Encapsulation is basically information hiding. It describes the idea of bundling data and methods that work on that data within one unit. In here access to data need to be controlled using access modifiers (public, private, protected etc.) and expose them to the outside world using getters and setters.

In above example also,

public class Animal {private picture;public getPicture (){
return this.picture;
}
public setPicture (picture){
this.picture = picture;
}
}

Abstraction

Abstraction is basically implementation hiding. Its main goal is to handle the complexity of the program by hiding unnecessary details from the user. That enables the user to implement the more complex logic on top of the provided abstraction without understanding or even thinking about all the hidden complexity.

Polymorphism

Polymorphism is simply many forms. It describes the concept that objects of different types can be accessed through the same interface. Each type can provide its own, independent implementation of this interface.

As example:

Animal a = new Lion();
Animal b = new Dog();

Note :

  • In this example Animal, Canine and Feline classes cannot be instantiated. Therefore, those classes should be abstract. Also, makeNoise() and eat() functions also abstract methods need to implement inside child classes.

Good Luck. Cheers!

--

--