Pillars of Object Oriented Programming

Reading Time: 3 minutes

This is probably the top case that is known in object oriented software development jobs and asked in many of interviews. There are many of descriptions out there on the internet, but somehow i felt the immense need of explaining things in my words. These pillars can be listed like this;

1. Encapsulation,

2. Inheritance,

3. Polymorphism

4. Abstraction

Encapsulation

Encapsulation is an Object-Oriented programming concept that binds together the data and functions that manipulate the data and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

Data encapsulation is a mechanism of bundling the data and the functions that use them, and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.

For instance, imagine you have an Integer type instance named “totalCosts”. So you want to hide it in the object and any references made to this object will not be reached out, because it is used in the object and data that holds is manipulated still in the object. When you define it as private, no other class will be accessing this instance, but the object itself.

In java we mention this section as access modifiers. There are four different types of access modifiers exist in java and they are listed from the least to the most accessible ones;

P.S.: No modifier is mostly considered as Default, which is not written like other modifiers, if you do not mention the access modifier of an instance or a method, then it will be automatically taken as a default access modifier.

https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

 

Inheritance

Inheritance is a way of acquiring properties of another. With the aid of inheritance, the information is more manageable and in order. This is done using extends keyword in all OOP languages. There are two forms of inheritance;
-Single Inheritance: A sub class can extend only one base class

public class A{}
public class B extends A{}

-Multiple inheritance: A sub class can extend multiple base classes. However, multiple inheritance in Java is not allowed, but it is in C++.

public class A{}
public class B{}
public class C extends A,B{}

-Hierarchical Inheritance: Derived classes extend base classes in a hierarchical manner

public class A{}
public class B extends A{}
public class C extends A{}

-Multi level Inheritance: Derived classes extend bases classes in a sequence

public class A{}
public class B extends A{}
public class C extends B{}

 

Polymorphism

The word polymorphism means having many forms. Typically, polymorphism takes place when there is a hierarchy of classes and they are related by inheritance.

public interface Vegetarian{}
public class Animal{}
public class Deer extends Animal implements Vegetarian{}

A Deer IS-A Animal
A Deer IS-A Vegetarian
A Deer IS-A Deer
A Deer IS-A Object

Abstraction

As per dictionary, Abstraction is the quality of dealing with ideas rather than events. Abstract classes represent the way more than implementations it may also implement implementations too depending the definition of the method in the abstract class. Moreover, abstract classes cannot be instantiated, but extended. In a such case an error will occur;

public abstract class MyClass{}
public class TestClass{
     private static MyClass my = new MyClass();
}

but it can be extended

public abstract class MyClass{}
public class TestClass extends MyClass{}