• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

oop concept

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

can any one expain the Encapsulation with example

Thanks in advance.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Ranch Hand
Posts: 61
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.... ???
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you might want to look at this:
https://coderanch.com/t/383749/Java-General/java/difference-between-abstraction-encapsulation
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic