Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Abstraction and Encapsulation i.e basic oops concepts gist and their unders

JavaFundaJul 29 2011 — edited Aug 6 2011
I have gone to many sites and everywhere got some difference in defintion of abstraction and encapsulation.
Below i tried to summarize the findings of both and have some questions which are inline with understanding.

Encapsulation:-
Hiding internal state and requiring all interaction to be performed through an object's methods is known
as data encapsulation — a fundamental principle of object-oriented programming.

Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. For this reason, encapsulation is also referred to as data hiding.


Question1:- Agreed that we are hiding the instance variable details but again we do expose them with getter and setter
methods. Is it not the violation of encapsulation? What advantage it provides exposing the internal state thru methods
(like getter and setter) but not declaring stae with public modifier.?

Question2:- If class does not have any state variable and has some just simple methods printing hello world.
Can we still say it is encapsulation?


Abstraction:-Abstraction in Java allows the user to hide non-essential details relevant to user.

Question1:- so we can say Encapsulation is also a feature of abstraction because in ecapsulation also we hide data(state) of object. Right?

Question2:-Say a class has 10 methods . Out of these 10 , only two are exposed for outside world with public modifier.Can we say this is the example of abstraction?

Question3:- Can we say interface/abstract class are the baset examples of abstraction as they just expose the masic details i.e signature of method to outside world?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 3 2011
Added on Jul 29 2011
16 comments
4,755 views