In my initial college days, I always wondered why object-oriented programming exists as it only makes programming complex. Why can't everything can be done in functional programming with different variables?
But then one day I came to know that when writing code we need to write in such a way that It could be understood and scaled easily.
Ok, What the heck I wrote above?
Let's put this in simple words, Let's say we need to make a debit card that can be used in UPI and NetBanking payments. Now, a Debit card has a card number and cardholder name, etc. There are also handlers to pay like UPI_Handler and NetBanking_Handler. Both handlers have their own payment functionality.
To implement this using functional programming we can
- Store card number and information in a local data structure like Strings
String CardHolder = "Jon Snow";
String CardNumber = "123456789";
Here, are some flaws in using variables. Because if our functions are using UPI_Handler and NetBanking_Handler they use these variables. But these variables can be changed or accessed directly by anyone because they are not private.
Also, CardHolder and card number belong to a Debit Card. So, why can't we have a custom data structure or data type which can store all the information belonging to the Card in one place? Well, we can use arrays here to store both (The same datatype, like strings here, would be allowed).
ArrayList<String> card = new ArrayList<>();
card.add("Jon Snow");
card.add("123456789");
Well, Again in the array the fields are publically available and can be accessed by outsiders (Outsiders means other classes can use and manipulate the things).
Now, here comes the concept of classes and access modifiers (Public, Private, Protected, Default).
public class Card{
private String cardHolder;
private String cardNumber;
public void pay(){
// Method Function
}
}
We can see that the Card class has fields belonging to the Card only. And we can add methods that can use these fields. Isn't we call it encapsulation?
Exactly, Encapsulation is combining all the related fields and methods using those fields in a single class. Encapsulation ensures the Data Hiding using Access modifiers like the private we are using here.
Here we go,
OOPS has four pillars:-
- Encapsulation
- Abstraction
- Inheritance
- Polymorphism
We saw why we need classes and access modifiers. Also, we took a slight look at encapsulation. We will explore four pillars in detail. Why we need these and how they make code modular and easily understandable.
- Make sure you follow for Low-Level Design, System Design, Backend Development, and much more.
Top comments (3)
Please don't confuse using functions with functional programming.
Mutating data? In functional programming not possible or rather limited.
Encapsulating data and restricting access is also in FP possible.
Something like polymorphism and generics is also availible.
Java itself might not be the best language to demonstrate functional programming. Although, these days there are quite some language enhancements in the right direction.
Yeah Got it Herbert, Thanks for pointing out. I was pointing out Why we need Classes in some cases while that thing can be done using functions too.
Checkout my new Blog that gonna help all developers out there
dev.to/lovepreetsingh/make-thousan...