aspose file tools*
The moose likes Beginning Java and the fly likes oop concept Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "oop concept" Watch "oop concept" New topic
Author

oop concept

prakash pawar
Ranch Hand

Joined: Mar 11, 2010
Posts: 42
Hi,

can any one expain the Encapsulation with example

Thanks in advance.


Regards,
Prakash Pawar.
W Pearce
Ranch Hand

Joined: Jan 06, 2009
Posts: 32
The big concept behind it is that you don't want your member variables being able to be manipulated outside of their own class. For example, if you have an int called height in a Bacon class, you don't want any code to be able to make an instance of that class and set height directly:



Instead, you want to have your instance variables wrapped in getter and setter methods, so you can perform validation on them when something is passing them a variable. So for the example above, we would do something like:

And these would be called from your code like this:


SCJP 6, SCWCD 5
Anupam Jain
Ranch Hand

Joined: Mar 16, 2010
Posts: 61

W Pearce wrote:The big concept behind it is that you don't want your member variables being able to be manipulated outside of their own class. For example, if you have an int called height in a Bacon class, you don't want any code to be able to make an instance of that class and set height directly:



Instead, you want to have your instance variables wrapped in getter and setter methods, so you can perform validation on them when something is passing them a variable. So for the example above, we would do something like:

And these would be called from your code like this:



That's a good explanation... but I have something to ask....
with this explanation there doesn't seem to be any difference between "Encapsulation" and "Abstraction".

So far, I've been thinking that Encapsulation is all about just putting the things together... while...

Abstraction concerns the usability of the members....

so by that meaning.... the above thing is "abstraction"...
Please explain if I am wrong.... ???


SCJP-6.0 OCPJWCD-5.0
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

you might want to look at this:
http://www.coderanch.com/t/383749/Java-General/java/difference-between-abstraction-encapsulation


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: oop concept