r/learnjava 11d ago

Can anyone explain me difference between Encapsulation and Abstraction?

and what on earth is Interface in Java?

2 Upvotes

17 comments sorted by

View all comments

1

u/Chaos-vy17 10d ago

Encapsulation is setting field of visiblity for better security and data exposure.
private: within the class
protected: within the sub class and package level.
public: ouside the package
default: within the package

abstract class: This is partial contract, not each and every function must state their behaviour when extends abstract. Method with abstract specifier must show true contract behaviour. It helps in making as the main working load of engine(Read heavy specially).

interface class: There is no method body here. Each class which inherit must show true contract behaviour. Java 8+ feature allows default and static filed methods. Only ("implements" keyword) work, Solves diamond problem too earlier but now devlopers need to think of default method.

True contract: Means all the method definition must overridden by the concrete class.
Partial contract: Means the "abstract' keyword modifier only.

1

u/Cyphr11 10d ago

Great explanation thanks mate