Polymorphism, Encapsulation, Data Abstraction and Inheritance in Object-Oriented Programming

Object-Oriented programming refers to the concept in high-level languages, such as Java and Python that uses Objects and classes in their implementations. OOP has four major building blocks which are, Polymorphism, Encapsulation, Abstraction, and Inheritance. There are other programming paradigms, such as Procedural programming in which code is written sequentially.

Python and Java are multi-paradigm high-level, programming languages. This means they support both OOP and procedural programming. A programmer decides on the paradigm to use based on their expertise and the problems they're trying to solve. However, there is no debate that OOP makes programming easier, faster, more dynamic, and secure. This is a major reason Java and Python are two fo the top most popular programming languages in the world today

Why Keep Variables Private?

It’s suggested that no variable should be public in OOD. If you make your variable private, in order to access the data for reading or updating, you need getters and setters, but this seems nonsense at first since you are still able to access it with additional bureaucracy.

I would like to give an example to explain. Let's say you have a month variable which is an integer. This month variable should hold a value between 1 and 12 inclusively, right?

Object Modeling Best Practices

OOP Is All About Encapsulation

Yes, that’s right. Object-oriented programming aims to encapsulate properties and methods into a consolidated object so that operations can be carried out on the object. The whole aim is to move away from procedural functions, which are not easy to reason with or prove correctness. But this principle often gets violated and people write procedural code using objects. Here is a classic example:

class Rectangle {
    private Long length;
    private Long breadth;

    //gettters
    //setters
    //constructors
}