• Post Reply Bookmark Topic Watch Topic
  • New Topic

Variable hiding/encapsulation question.  RSS feed

 
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there.  General question.   My instructor told me to use proper variable hiding/encapsulation techniques.  When you use proper variable hiding/encapsulation techniques does this always mean you will have more than once class file?   

I hope that makes sense.  I guess what I'm trying to ask is .  Is there a reason to hide/encapsulate if all you have is one class file?   So by my instructor telling me this, I'm being told that I really should be using multiple classes to handle the assignment.

Thank You
 
Saloon Keeper
Posts: 3330
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class can't hide variables from itself, so, probably means multiple classes with private fields and getters and setters.
 
Sheriff
Posts: 4931
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your whole application consists of singular class file, there is a big chance you don't use object oriented techniques. Once you don't use that, there is no much to hide/encapsulate.

So yeah, your instructor is right, you need to attempt to solve your assignment using multiple classes, where all (most likely) fields need to have private access modifier, and provide an ability to access them via public getters.

Now, what's regarding setters. Not always you need to have setters. You may find, that you can achieve that by initializing object via constructor facilities. I mean, that not always makes sense to modify fields later via setters. However, if you have setters, make sure you validate inputs, so object doesn't appear to become in an invalid state.

Do you have some actual code to share with us, so we could see better what we are talking about?
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:If your whole application consists of singular class file, there is a big chance you don't use object oriented techniques. Once you don't use that, there is no much to hide/encapsulate.

So yeah, your instructor is right, you need to attempt to solve your assignment using multiple classes, where all (most likely) fields need to have private access modifier, and provide an ability to access them via public getters.

Now, what's regarding setters. Not always you need to have setters. You may find, that you can achieve that by initializing object via constructor facilities. I mean, that not always makes sense to modify fields later via setters. However, if you have setters, make sure you validate inputs, so object doesn't appear to become in an invalid state.

Do you have some actual code to share with us, so we could see better what we are talking about?


Thank You. I'm going through the Cattle Drive so I don't think I can able to share my code .  I think however that you guys helped me understand.   Thank You so much
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!