In this article, I will try to explain the fundamentals of Object Oriented Programming and I will try to give an answer to one of the most frequently asked interview question. The Object Oriented Programming paradigm based on four concepts. These concepts are called the 4 Pillars of Object Oriented Programming.

  • Abstraction
  • Encapsulation
  • Inheritance
  • Polymorphism

Abstraction

In software development, abstraction is the concept of providing functionality to users without exposing the implementation details on design level. Oh that sounds fancy! In simple words, we want to show the users what the application can do but we do not want to show how it does it. Anyway an average user wouldn’t be interested in the details anyway.

Example: When we want to check if tomorrow will be sunny or not, we wouldn’t care how the forecasting internally works. We would be only interested in the answer to our question.

Ok, it is clear that a user wouldn’t care about our amazing implementation details but why should we as developers hide our implementation details?

  • The exposed functionality without any complexity is more understandable to users
  • It is easier to change the implementation details later if necessary (because the implementation details are hidden)

Encapsulation

Encapsulation is the concept to bind the data and behavior together and to control the access to this object from outside. Simply put, related data and behavior are put together to achieve creating real-life similar objects. Encapsulation is also often referred as data hiding. The main difference between encapsulation and abstraction is, the hiding of the implementation details happens on design level for abstraction and for encapsulation it happens on implementation level.

Example: A car has properties such as brand, model, color etc… also a car has its behavior such as start, accelerate, break etc…

Encapsulation is used for data-behavior grouping and control of access. By making a car class we are grouping all the car-related data and the behavior.

In the example below, we have created a Car class. As described above, fields of the class (model and color) represent the properties of a car and the method (accelerate) represents its behaviour.

	public class Car {

		private String model;
		private String color;

		public Car(String model) {
			this.model = model;
		}

		public void accelerate() {
			System.out.println("I am accelerating");
		}

		public String getColor() {
			return color;
		}

		public void setColor(String color) {
			this.color = color;
		}

		public String getModel() {
			return model;
		}

	}

Also the properties are marked with **private **keyword. Using private keyword restricts the direct access to the field from outside of the class. Instead, the access is achieved through getters and setters. Getters and setters can be used to make fields read-only/write-only.

In this example, one can read the color and model, change the color however it is not possible to change the model of the car. As an example, model here is set when we are initializing the object. Private keyword does not allow direct access to model field and by not defining any setter for model, we are making it non-modifiable (read-only).

Inheritance

Like in real life, similiar objects have similar properties and behaviors. Instead of writing the same functionality for each object one can use inheritance. Inheritance is used to reduce code duplication among sub-types.

Example: A car is a generic type, let’s say in our example we want to build a racing game and we have a race car. A race car has the same properties of a car and it can do everything that a car does. In addition to that a race car can use “nitro”.

In code below, the RaceCar class inherits the properties and the behaviour of Car class. The inheritance is achieved with extends keyword. Another keyword we must use when using inheritance is super. Super keyword refers to the super class (in this example Car class). When initializing the sub class, first we need to make sure that the constructor of the super class is called. One can also access the fields and the methods of the super class by using the super keyword.

#programming #object-oriented #oop-concepts #programming-paradigms #oop-interview-questions

4 Pillars of Object Oriented Programming
1.50 GEEK