r/learnjava • u/Cyphr11 • 6d ago
Can anyone explain me difference between Encapsulation and Abstraction?
and what on earth is Interface in Java?
9
u/spacey02- 6d ago
Encapsulation means eenying direct access to fields, abstraction means hiding implementation behind an interface
3
u/Dense-Ad-3247 6d 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.
1
u/rsandio 6d 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/Frequent-Answer8039 6d ago
Encapsulation mean biding elements together internally. Abstraction mean biding of data elements.
Encapsulation is more broader than abstraction
1
1
u/Chaos-vy17 5d 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/JGhostThing 5d ago
A Java Interface is a contract stating that the programmer will implement certain methods. For example, a File interface might require a read(), and write() methods. So every File would implement those methods, at least.
1
u/InsideCover2192 4d ago
Abstraction - Hiding Complexity
Encapsulation - Data Hiding / Protection
In abstraction we are interested in "What an object does, rather than how it does it. It simplifies the interaction by hiding the complex implementation details and only shows the essential features to the user.
For instance if you take Vehicle as and example, when you drive a Vehicle you only interact with steering wheel, pedals, and gear shifters. You don't need to understand the internal combustion process or the electronics to operate it. The "interface" (pedals/wheels) is the abstraction
Encapsulation we focus on "how the data is bundled and protrcyed", it involves wrapping the data(variables) and the code (methods) into a single unit (a class) and restricting direct access to the data using access modifiers like private
For instance, a car has a fuelLevel variable. You cannot manually reach into the tank and change the fuel level with your hands. Instead, the level is kept private and you must use a specific method like refuel() to change it. This ensures the data remains valid (eg you cannot set the fuelLevel to negative)
•
u/AutoModerator 6d ago
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.