aspose file tools*
The moose likes Beginning Java and the fly likes how can I use Encapsulation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how can I use Encapsulation?" Watch "how can I use Encapsulation?" New topic
Author

how can I use Encapsulation?

umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
Can anybody give me an example how can i use encapsulation to hide the non-essential details of an object?
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
I doubt if encapsulation in Java means "hiding non-essential details of an object".
"Encapsulation is the ability of an object to place a boundary around its properties (ie. data) and methods (ie. operations). "

Reference URL
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Abdulla Mamuwala:
I doubt if encapsulation in Java means "hiding non-essential details of an object".
"Encapsulation is the ability of an object to place a boundary around its properties (ie. data) and methods (ie. operations). "

Reference URL


Don't be so quick to shoot somebody down like that.

If you look up the definition of encapsulation on Wikipedia, for example, it claims that encapsulation is the culmination of both separation of concerns and information hiding.

It is a concept that is relatively new, in the grand scheme of things (as are all concepts in computer science), and it's meaning is often different depending on who uses it.

While Wikipedia is certainly not the authoritative source of all definitions, I'd like to see your proof that your definition is more accurate.


It is, however, hard to show an good example that says "this is encapsulation through hiding of non-essential details".

What you really need to understand is the difference between "interface" and "implementation".

Anything that is implementation and not part of the public interface of a class is "hidden" or "encapsulated" in the class in which it is defined. That is, it cannot be seen or directly used by other classes.

- Adam
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
Anything that is implementation and not part of the public interface of a class is "hidden" or "encapsulated" in the class in which it is defined. That is, it cannot be seen or directly used by other classes.


Yes thats right about the encapsulatin. But I used private for all variables in a class and it gives me the error "Illegal start of expression" Why?
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by umut uzumcu:


Yes thats right about the encapsulatin. But I used private for all variables in a class and it gives me the error "Illegal start of expression" Why?



Okay, illegal start of expression has nothing to do with whether variables are private or not.

Usually, this problem is caused by forgetting a semi-colon or by forgetting a closing bracket or brace.

Try looking at the lines of code surrounding the line number where the exception is generated.

If you still can't figure it out, post the code, and one of us might be able to spot the problem for you.

- Adam
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
I think we can initialize private members in other classes but not in the main class. Am i right?
umut uzumcu
Ranch Hand

Joined: Jan 10, 2007
Posts: 43
Adam,
My program is working without any error. When i used private for mail programs variables it gave me error for each variable. But I can do it in other classes.
eg.
public static void main()
{
private String name;
private boolean flag=false;
}

Error :Illegal start of expression.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18989
    
  40

Originally posted by umut uzumcu:
Adam,
My program is working without any error. When i used private for mail programs variables it gave me error for each variable. But I can do it in other classes.
eg.
public static void main()
{
private String name;
private boolean flag=false;
}

Error :Illegal start of expression.


You can't use access modifiers on local variables (except for final). Local variables can only be accessed by the one thread that called the method.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by umut uzumcu:
Adam,
My program is working without any error. When i used private for mail programs variables it gave me error for each variable. But I can do it in other classes.
eg.
public static void main()
{
private String name;
private boolean flag=false;
}

Error :Illegal start of expression.


Okay, the reason for this is because you can't use private (or public or protected for that matter) modifiers on local variables (i.e. variables defined within a method).

However, you really don't need to do this anyway, because a local variable only exists inside the method where it is defined. It is automatically encapsulated on the basis that it is not a member variable, and cannot be seen, used, accessed, etc, by any other object. It isn't created until you enter the "main" method, and it dies when you leave the "main" method.

This is why you get the "illegal start of expression" error -- the key word private cannot be used to start an expression inside of a method. Just remove the private keyword here, and you're good to go.



It seems to me like you have a little bit of confusion as to what the difference is between class, member, and local variables are. You should attempt to understand this distinction, as it will help you to understand encapsulation better.

- Adam
Jayaraman Suresh Babu
Greenhorn

Joined: Jun 15, 2006
Posts: 11
I think thats wrong. Private members are fully accessible to all methods inside the class. However they simply not visible anywhere else.
Adam Nace
Ranch Hand

Joined: Jul 17, 2006
Posts: 117
Originally posted by Jayaraman Suresh Babu:
I think thats wrong. Private members are fully accessible to all methods inside the class. However they simply not visible anywhere else.


I'm not sure who or what you are replying to here. I didn't see anywhere where anyone posted anything to the contrary...
 
Consider Paul's rocket mass heater.
 
subject: how can I use Encapsulation?