File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how can I use Encapsulation?

 
umut uzumcu
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anybody give me an example how can i use encapsulation to hide the non-essential details of an object?
 
Abdulla Mamuwala
Ranch Hand
Posts: 225
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think we can initialize private members in other classes but not in the main class. Am i right?
 
umut uzumcu
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20829
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Adam Nace
Ranch Hand
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 117
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic