Object Oriented Programing In 5 Minutes

El Akioui Zouhaire
Dev Genius
Published in
4 min readAug 5, 2021

--

Photo by Lagos Techie on Unsplash

When it comes to object oriented programming I found many tutorials complicates the understanding of this concept. To understand it very well, let’s understand the problem it solves.

In the past, we have procedural programming, which divides a program into a set of functions and these functions operates on data.

But as the program grows we get many functions related between each others.

When we make a change on one function, this can break other functions, which is problematic.

So, the OOP (Object Oriented Programming ) comes to solve this problems. It comes with 4 Mini-concepts : Encapsulation, Abstraction, Inheritance, and Polymorphism.

1- Encapsulation

This means creating a group of related fields and functions into a unit called object.

We refer these fields as properties and these functions as methods.

Example:

A Teacher has as properties for example baseSalary, overtime, rate, as a methods, for example getWage(), and setters for updating the properties values and getters for getting the properties values.

package com.selcote.abstraction;public class Employee{
private float baseSalary;
private float rate;
private float overtime;

public float getWage(){
return this.baseSalary + (rate * overtime);
}

public float getBaseSalary() {
return baseSalary;
}

public void setBaseSalary(float baseSalary) {
this.baseSalary = baseSalary;
}

public float getRate() {
return rate;
}

public void setRate(float rate) {
this.rate = rate;
}

public float getOvertime() {
return overtime;
}

public void setOvertime(float overtime) {
this.overtime = overtime;
}

}

As you see the getWage() method has no parameters, it access to the objects properties and operates on them, with no need to pass parameters to it. And it is one of the best practices, having the methods without parameters.

The fewer the number of parameters, the easier to use and maintain that function.

In addition to that, we have made the properties private, we can get access to them just by passing by setters and getters, which hides the values or state of a structured data object inside a class, and preventing unauthorized parties direct access to them.

2- Abstraction

This technic is all about hiding the details from the outside, which gives us many benefits.

We can hide many object methods by making them private, and expose just fewer public method.

So, by doing this we make our object interface simpler.

Using and understanding an object with fewer methods and properties, is better than an object with many methods and properties.

Also, this concept reduces the impact of the change, which means if you change any private or inner methods this will not leak the outside. Because none of these methods or properties is known by the outside of this containing object.

Also, we can expose just an interface to outside and the implementation will be defined at run time.

Example:

package com.selcote.abstraction;  public interface Car {  
void turnOn();
void turnOff();
}
package com.selcote.abstraction;public class ManualCar implements Car { @Override
public void turnOn() {
System.out.println("turn on the manual car");
}
@Override
public void turnOff() {
System.out.println("turn off the manual car");
}

}
package com.selcote.abstraction;public class AutomaticCar implements Car {

@Override
public void turnOnCar() {
System.out.println("turn on the automatic car");
}
@Override
public void turnOffCar() {
System.out.println("turn off the automatic car");
}
}

Let’s test the car functions:

package com.selcote.abstraction;public class CarTest {  
public static void main(String[] args) {
Car car1 = new ManualCar();
Car car2 = new AutomaticCar();
car1.turnOnCar();
car1.turnOffCar();
car2.turnOnCar();
car2.turnOffCar();

}
}

The output:

turn on the manual car
turn off the manual car
turn on the automatic car
turn off the automatic car

The client program only knows about the Car and the functions that the Car provides.

The internal implementation details are hidden from the client program.

3- Inheritance

This mechanism allows us to eliminates the redundant code.

Let’s think about html elements like TextBox, Select, CheckBox, etc. These elements have some things in common like hidden, and innerHtml properties and methods like click() and focus().

Instead of redefining these properties and methods for every type of Html element, we can define ones in a generic object called HtmlElement and have other object inherits from it.

4- Polymorphisme

If we try to understand this composing name, Poly means Many, and morphism means Form, So :

Polymorphisme = Many Forms

This is a technic that allows to get rid of if and else or switch statements.

So, Polymorphism is the capability of a method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementations.

Example:

public class Animal{
public void sound(){
System.out.println("Animal is making a sound");
}
}
class Dog extends Animal{
@Override
public void sound(){
System.out.println("Barking");
}
public static void main(String args[]){
Animal obj = new Dog();
obj.sound();
}
}

The Output:

Barkingclass Cat extends Animal{
@Override
public void sound(){
System.out.println("Meow");
}
public static void main(String args[]){
Animal obj = new Cat();
obj.sound();
}
}

The output:

Meow

Conclusion:

OOP has simplified a lot the programming field, by solving many problems:

Encapsulation => REDUCE COMPLEXITY + INCREASE REUSABILITY

Abstraction => REDUCE COMPLEXITY + REDUCE THE IMPACT OF CHANGES IN THE CODE

Inheritance => ELIMINATE REDUNDANT CODE

Polymorphisme => Refactor if/switch case statements

Share with me your thoughts in a comment, and make a clap :-)

Also,consider following me on medium and subscribing to my channel : Selcote (problem-solving community) for more amazing content.

--

--

I am a software engineer and entrepreneur. My focus is on Developing technical skills,Learning marketing,and taking care of the health.