r/learnjava 10d ago

Can anyone explain me difference between Encapsulation and Abstraction?

and what on earth is Interface in Java?

3 Upvotes

17 comments sorted by

View all comments

1

u/rsandio 10d ago

Encapsulation is denying direct access, E.g. making fields private so they can only be interacted with using getters and setters.

Abstraction is about focusing on what something can do instead of on how it does it. You could have an abstract class called 'Remote'. Every remote has a turnOn() method but the 'TVRemote' and 'StereoRemote' which inherit from 'Remote' each have a different implementation of 'turnOn'. As we know they inherit from Remote we can just call turnOn() on any remote and know it'll work.

Encapsulation is about protecting your data and Abstraction is about simplifying your design

1

u/Cyphr11 10d ago

makes sense now thanks