Skip to main content

Encapsulation, what is really the point?

5 replies [Last post]
ceromus
Offline
Joined: 2008-08-16
Points: 0

Im new to java programming.

I've read what Encapsulation is but i'm still confused as to the point. Maybe i'm misunderstanding in some fashion.

Lets say I have 2 classes.

classone has the private variables and the method to retrieve those variables.

classtwo is the main class that makes use of those variables.

Why does Encapsulation exist? The only way for me to actually modify public variables in classone from classtwo is to actually use classone.varname right? Why is the protection needed?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kitmub
Offline
Joined: 2009-07-20
Points: 0

i think it can also protect and give privacy to where it needs

imagine making the method for deciphering password public making everyone can use it to get the password unencrypted or in plain text

gsuryaprasath
Offline
Joined: 2011-07-15
Points: 0

Encapsulation - is the ability of an object to hide its data and methods from the rest of the world. It is one of the fundamental principles of OOPs.

More Details : Cegonsoft

scphan
Offline
Joined: 2009-05-01
Points: 0

Encapsulation is using the supplied resources of the other class(s) in one class so that this class encompasses the components which allows it to do something else.

Message was edited by: scphan

homojedi
Offline
Joined: 2009-07-16
Points: 0

in my experiance, encapsulation is used to keep a class's resources untouchable by any other part of the program unless you wish them to be accessed, and in that case you have a method that you must explicitly call to access that variable. For the most part it keeps the code clean and if anything goes wrong you should keep solace in the fact that if you have encapsulated properly its not because a varible is being changed unwittingly due to varibles being set private all over the shop.

as a rather lapsidasical example, think of if you will an iPod. It's a small compact device that lets you store and play music, videos, games etc. think of those as the fields of a class ipod. And you as a user can access these fields through the ipod interface (which are the class methods), the touch sensitive click wheel and screen. and as far as you are allowed that is the only thing your allowed to see and do with the ipod. If however they decided to give you access to all the complexities of the ipod, and let you run rampant with varibles that are public instead of private inside our little iPod class, things would run amok, you would have song names that play the wrong song, or point to invalid memory, or vidoes that only play sound or something completley different imagine, you go to play metallica sad but true and to your shock and horror you get "its chico time" its a scary thought(also considering you must have had to have had chico on your ipod in the first place for this to occur).

i hope this clears things up.

kathrynvanstone
Offline
Joined: 2009-05-27
Points: 0

There are several uses for encapsulation.

First lets look at a more concrete example.

public class Square {

private double size;

public Square(double widthAndHeight) {
if (widthAndHeight <=0) {
throw IllegalArgumentException("A square must have a positive width and height");
}
this.size = widthAndHeight;
}

public double getWidth() {
return size;
}

public double getHeight() {
return size;
}

public double getArea() {
return size * size;
}
}

Note that the data one can get from a Square is not the same as what is internally stored. Futhermore if we made size public, then the size could be changed to a negative value. Encapsulation allows us to store internally different items than was is show externally; more importantly it allows the internal representation to *change* without affecting anyone who uses Squares. For example suppose we add a Rectangle

public class Rectangle {

private double width;
private double height;

public Rectangle(double width, double height) {
checkPositive(width);
checkPositive(height);
this.width = width;
this.height = height;
}

private static checkPositive(double value) {
if (value < 0) {
throw new IllegalArgumentException("Negative distance: " + value);
}

public double getWidth() {
return width;
}

public double getHeight() {
return height;
}

public double getArea() {
return width * height;
}
}

We can then change Square to

public class Square extends Rectangle {

public Square(double widthAndHeight) {
super(widthAndHeight, widthAndHeight);
}

}

Any code using the old Square class would work just as well with the new Square class.

Does that help?