• Post Reply Bookmark Topic Watch Topic
  • New Topic

Encapsulation - data hiding issue  RSS feed

 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm learning the concept of encapsulation in Java which teach us on data hiding from outside world or code. It says that object knows its state and behavior. Any outside code can touch the values of instance variable so it is best practice to secure them such that no one can access it directly using dot(.) operator on reference variable but can modify it by calling setter method(after data getting validated by the setter method).
Basic thumb rule is that -> make private to all instance variables and public to all getter and setter methods.

Here is my code:-

EncapsulationBasic.java



TestEncapsulationBasic.java:-


I'm getting following error:-
./EncapsulationBasic.java:35: getSize() is already defined in EncapsulationBasic
public void getSize(){
^
1 error


May I know why I'm getting this error.
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have found the root cause of this issue by myself. Actually, I have repeatedly wrote getSize() method twice which was causing this issue. Not even method loading here If that would have been then atleast my program worked somehow.

Thanks
 
Randall Twede
Ranch Hand
Posts: 4696
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
because you have 2 getSize() methods that have the same signature(i think thats the word for it)
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinod Vijay wrote:I have found the root cause of this issue by myself...

Well done.

It's worth noting that you should only include setters for those values that you want outside parties to be able to change. Don't go creating them willy-nilly.

Winston
 
Vinod Vijay
Ranch Hand
Posts: 165
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Vinod Vijay wrote:I have found the root cause of this issue by myself...

Well done.

It's worth noting that you should only include setters for those values that you want outside parties to be able to change. Don't go creating them willy-nilly.

Winston


Thanks for your useful advice. I'll take care of that in future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!