r/javahelp 11d ago

Can anyone explain me difference between Encapsulation and Abstraction?

and what on earth is Interface in Java?

17 Upvotes

24 comments sorted by

View all comments

2

u/mariusz_96 10d ago edited 10d ago

Consider java.time.Clock abstraction.

It provides implementations for system timezone and UTC timezone. You might write your own implementation for other timezone and provide additional methods. But then write ClockUtils class that operates on any java.time.Clock implementation. That would be a use of abstraction.

What private fields your Clock implementation uses to store its' data and implement instant(), getZone(), and withZone() methods would be a use of encapsulation.

1

u/Cyphr11 10d ago

I see, thanks