This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsualtion

 
Pratap Rana
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is encapsulation and abstraction ? Please provide me with the example.
 
Yasith Vidanaarachchi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Pratap,
Encapsulation is securing data.You can only access a field of a tightly
encapsulated class by using methods and you cant change the data in a
tightly encapsulated class because all their variables are declared
final.
Cheers

YASITH
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, not exactly. Encapsulation is the concept whereby common data and functionality is enclosed in an object. There's bound to be a better explanation of this topic somewhere in JavaRanch - hopefully one of the moderators will point you to it, but suppose you wanted to use the concept of a "Person" in your program. Java (and other OO languages) would allow you to gather the properties of a person (name, age, sex etc.) and encapsulate it in one object. Its a very useful concept, because it ring-fences what a Person is from affecting other parts of your program. If you add another property or some behaviour to Person your changes are only applied to that object and other classes which use it don't necessarily need updated.

"Security" is not the best word to use to describe why encapsulation is used. Java Security is a completely different concept and is not involved in encapsulation. Neither does encapsulation require variables be declared final.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
encapsulation in Java usually means making the variables of a class private so that a client of the class must use methods like setX() and getX() to access the variables. The idea is that the client should not know or rely on how the class works, only on its published behavior. One common illustration is a Point class that seems to have X and Y coordinates but actualy keeps the coordiates internaly as polar coordinates. The client still uses cartesian coordinates and the getX/getY/setX/setY methods quietly translate them to and from angle and distance.

abstraction is the idea of a reusable class that represents the common behavior of a type of thing, like a car, rather than just one case of that thing, like my car sitting in my driveway. If you do it correctly, the next time you write a program involving cars you can use your car class again instead of writing another class. If this time you need to save more data like transmission type, you can either subclass your car class to add more behavior and state or modify the class in a way that doesn't change anything car already does but now also tracks transmission type.
[ December 20, 2004: Message edited by: Mike Gershman ]
 
Pratap Rana
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
I am still not clear.This is what i have in my mind.

Regarding Encapsulation
--------------------------
1.Define interface A
interface A{
void implementMe();
}
2.B Implements the A
Class B implements A{
private String modifyme;
public void implementMe(){
//--------------------
modifyme = "Yes done";
//--------------------
}
}
If i define variable as private and controlling that variable through method and lets expose method definition through interface A.

Can i call this as encapusaltion? because the one can access the behaiuor of the system through interface without knowing the method implementaion .

Regarding Abstraction
--------------------------
Identifying of the above class B implementation.

Please correct me if i am wrong.
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, interfaces have more to do with abstraction. They allow classes with different implementations to share a common type.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic