Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Encapsualtion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Encapsualtion" Watch "Encapsualtion" New topic
Author

Encapsualtion

Pratap Rana
Greenhorn

Joined: Dec 15, 2004
Posts: 15
what is encapsulation and abstraction ? Please provide me with the example.
Yasith Vidanaarachchi
Greenhorn

Joined: Sep 18, 2004
Posts: 10
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

Joined: Apr 14, 2004
Posts: 10336

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.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
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 ]

Mike Gershman
SCJP 1.4, SCWCD in process
Pratap Rana
Greenhorn

Joined: Dec 15, 2004
Posts: 15
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

Joined: Mar 13, 2004
Posts: 1272
Actually, interfaces have more to do with abstraction. They allow classes with different implementations to share a common type.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsualtion