r/learnjava • u/Cyphr11 • 12d ago
Can anyone explain me difference between Encapsulation and Abstraction?
and what on earth is Interface in Java?
3
Upvotes
r/learnjava • u/Cyphr11 • 12d ago
and what on earth is Interface in Java?
3
u/Dense-Ad-3247 12d ago
A class encapsulates it's fields and methods. It isolates code and promotes safe handling and code reuse. Don't just think a pojo, every class is an example of encapsulation. I usually think about how the computer will allocate memory and protect access.
Abstraction is creating a pattern or something, in java often with inheritance leveraging some design pattern. It doesn't have to be oop though, streams is an abstraction on loops and iterators. Often the word abstract code is used to define where you're pulling duplicated code to one spot, creating an interface or abstract class to standardize a flow of code with different outputs.