Object Oriented Programming (OOP) refers to a type of software design in which programmers define the data type of a data structure, as well as the types of operations that can be applied to that data structure. OOP aims to implement real-world entities like inheritance, hiding, polymorphism, etc and to bind together the data and functions that operate on them so that no other part of the code can access this data except the function. There are a few key concepts essential to gaining an understanding of OOP that I will go over in this article.
Encapsulation
Encapsulation is achieved when an object keeps its state private, inside a class. Other objects donโt have direct access to this state. Instead, they can only call a list of public functions called methods.
Abstraction
In OOP, programs are often extremely large and separate objects communicate with each other a lot so maintaining this kind of codebase over many years can be a difficult challenge. Abstraction is a concept that aims to ease this problem. Applying abstraction means that each object should only expose a high-level mechanism for using it. In other words, only the essential details will be displayed to the user. In Java, abstraction is achieved by interfaces and abstract classes.
Inheritance
Inheritance allows you to create a (child) class by deriving from another (parent) class to form a hierarchy. The child class reuses the fields and methods of the parent class and can also implement its own unique fields and methods.
Polymorphism
Polymorphism refers to the ability of OOP languages to differentiate between entities with the same name efficiently. This typically happens by defining a parent interface to be reused. It outlines some common methods, then each child class implements its own version of these methods. A real life example of polymorphism could be how a single person can possess different behaviors in different situations, like a man can be a father, a husband, an employee, all at the same time. Polymorphism allows you to perform a single action in different ways. In other words, it allows you to define one interface and have multiple implementations.
Why Use OOP?
Object oriented programming has several advantages over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the programs
- OOP helps to keep code DRY (โDonโt Repeat Yourselfโ), and makes the code easier to maintain, modify, and debug
- OOP makes it possible to create full reusable applications with less code and shorter development time.
For further reading on this topic, check out some of these sources:
- A Basic Explanation of OOP by Alexander Petkov of freeCodeCamp
- OOP Concepts in Java by RishabhPrabu of GeeksForGeeks
Top comments (1)
Good one :)