• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Twist in the Tale 3.3 in OCA Java SE 7 Programmer I Certification Guide (MALA GUPTA)

 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.
There is a question in Ch 3 of OCA Java SE 7 Programmer I
Certification Guide (MALA GUPTA)
Given the following definition of class
Phone, which of the options, when replacing the code on lines 1–3, makes it a wellencapsulated
class?




a)

b)

c)


d)

e)None of the above.

Correct answer is e. But not code here. What is the correct code?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote: Correct answer is e. But not code here. What is the correct code?

If you read the section Apply encapsulation principles to a class on page 150, you should be able to produce the appropriate code to turn the Phone class into a well-encapsulated class. Give it a try!
 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it enough or i must change the methods to private?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt in this ..the instance variable "model" is not accessed through getters / setters, so can it remain public and the class is still encapsulated ?
And if we set all the variables , their getters and setters private is it called tight encapsulation ?
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:
Is it enough or i must change the methods to private?


if you focus on why you've made the variable private (and what encapsulation is used for) it might help you work out what the methods should be. ie what are you trying to stop from happening?
 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think encapsulation is protect variables from using outside. Other class cant change the variables.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:I think encapsulation is protect variables from using outside. Other class cant change the variables*.


*directly

correct.

but say you want to give some controlled access? how would you do that?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:Is it enough or i must change the methods to private?

To have a well-encapsulated class you have to make sure that your data members can't be accessed directly by other classes. That's why you have to make them private. So assume the Person class has an instance variable weight. As you know, weight can never be less than 0. So you write the following codeNow it seems that every Person instance will have a valid weight. But because you have marked the instance variable weight as public, every other class can access this instance variable directly and set an invalid weight value (because the validation in the setWeight method is bypassed)That's why you should create a well-encapsulated class by changing the access modifier of the instance variable weight as private. And then the Doctor class won't compile anymore (as the instance variable weight is not visible anymore). So the Doctor class has to be changed toWith the well-encapsulated Person class it's impossible to have a weight below 0, because you can now only use the setWeight method to change the weight. And first the new value is validated and if it's invalid a runtime exception is thrown.

But what will happen if you also change the access modifier of the setWeight and getWeight methods to private?

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:I have a doubt in this ..the instance variable "model" is not accessed through getters / setters, so can it remain public and the class is still encapsulated ?

The question only asked to change lines 1-3 to create a well-encapsulated class. But because the instance variable model is still public (and therefore directly accessible by other classes), the Phone class can't be considered as a well-encapsulated class. But after the change to the instance variable weight, the Phone class is already better encapsulated than the original version.

Ramya Subraamanian wrote:And if we set all the variables , their getters and setters private is it called tight encapsulation ?

"This class is well-encapsulated" is equivalent to "this class is tightly encapsulated".
 
Nil. Hatamova
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i change method to private i cant call method in other class. Is it also encapsulation?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, even if there were no getters/setters the public instance variable "model" could still be accessed and modified. so it is important to make it private for the class to be completely encapsulated.



So the terms Encapsulated/Well-encapsulated/Tightly encapsulated ..all mean the same? There's no term as "tight encapsulation" in my OOAD(Grady booch) book

Only when you don't want other classes to access/modify the instance variables you make getters/setters private. But then it is equivalent to not writing the getters/setters, because it will not be visible to other classes.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nil. Hatamova wrote:If i change method to private i cant call method in other class.

Indeed!

Nil. Hatamova wrote:Is it also encapsulation?

No, encapsulation is about the instance (and class) variables, not about methods. But if you would mark all getters & setters as private in the Person class, the class would be completely useless because you can only create instances in other classes but not invoke any methods. You could of course have a few private methods to do some calculations or methods which should not be available to other classes (but that's not considered to be encapsulation).
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:okay, even if there were no getters/setters the public instance variable "model" could still be accessed and modified. so it is important to make it private for the class to be completely encapsulated.

Yes! But you should also provide methods to access and/or change it. Because in your current Phone class the instance variable model is pretty useless as it can be accessed or retrieved. And just for the record, it does not always have to be a getter/setter. This class is also well-encapsulated

Ramya Subraamanian wrote:So the terms Encapsulated/Well-encapsulated/Tightly encapsulated ..all mean the same? There's no term as "tight encapsulation" in my OOAD(Grady booch) book

Yes, these terms can be used interchangeably. Maybe the term "tight encapsulation" is not used, but I assume the OOAD book suggest to hide the implementation details and make sure your class is encapsulated.

When reference variables are involved, creating a well-encapsulated class can be very tricky! In this topic you'll find some excellent explanations (with code snippets) about the encapsulation of reference variables. Definitely worth reading!

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! But you should also provide methods to access and/or change it. Because in your current Phone class the instance variable model is pretty useless as it can be accessed or retrieved.


Okay, thats right. But, what if I have specific requirement that I dont want anyother class to access/modify my "model" variable. So I dont want to create any public mutator/accessor methods. So now would the class still be called encapsulated.

Having said that...I dont want to assume anything In exam perspective do you think this class is encapsulated or not..


I read the thread...in the case of StringBuilder you use toString() to convert to String.



String is immutable, but in the TestSpecial class even if you use a (return new StringBuilder(s)); it would still work right, it just returns bob without fred being appended. But I think I am missing an important scenario, where it could be modified.

And when encapsulating Arrays can we use Arrays.copyOf , which will create a valid copy of the array without exposing the original array?



BTW , there is is a stray 'a' in the printName() of OP's initial post, I think that is 's'. there is a 'x' in your add() method, I think it must be 'p'.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Okay, thats right. But, what if I have specific requirement that I dont want anyother class to access/modify my "model" variable. So I dont want to create any public mutator/accessor methods. So now would the class still be called encapsulated.

To have a well-encapsulated class you have to make sure that your data members can't be accessed directly by other classes. So because all data members of your Phone class are private, no other class can access them directly and thus is your class well encapsulated.

As already mentioned in previous posts: model can actually be removed as it is not used in the class. If you check the Phone class from my previous post (with the 1-arg constructor), the instance variable model is actually used (and will be initialized using the constructor), can't be modified (nor retrieved) by other classes and this class is well encapsulated too.

Ramya Subraamanian wrote:Having said that...I dont want to assume anything In exam perspective do you think this class is encapsulated or not..

for encapsulation you only have to wonder one thing: is it possible to access the data members directly by another class? For your Phone class, the answer is clearly no and thus is your class well encapsulated.

Ramya Subraamanian wrote:String is immutable, but in the TestSpecial class even if you use a (return new StringBuilder(s)); it would still work right, it just returns bob without fred being appended. But I think I am missing an important scenario, where it could be modified.

You are absolutely spot-on! Both code snippetswill make sure that the data member s is not exposed (and can be changed) by other classes while the instance variable s is private.
With reference variables the most important thing is to create a copy of the original object. It doesn't matter which constructor (or method) you use to create this copy. So StringBuilder has 4 constructors: I used the one with the String parameter, you have used the one with the CharSequence parameter. Both will guarantee that a copy of the original object (the object to which the instance variable s is referring) is created!

Ramya Subraamanian wrote:And when encapsulating Arrays can we use Arrays.copyOf , which will create a valid copy of the array without exposing the original array?

Based on its name, that's definitely one of the alternatives you could use. Probably one of the easiest to use

Ramya Subraamanian wrote:there is is a stray 'a' in the printName() of OP's initial post, I think that is 's'. there is a 'x' in your add() method, I think it must be 'p'.

You are correct on both occasions Have a cow for reading that topic very carefully and spotting these typos!

Hope it helps!
Kind regards,
Roel
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, that clears up my doubts on encapsulation . Thanks for the cow ! I keep wondering...what do with the cows
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramya Subraamanian wrote:Thanks for the cow ! I keep wondering...what do with the cows

Feed them Have you already read the Ranch Cows wiki?
 
Ramya Subraamanian
Ranch Hand
Posts: 178
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Feed them ...ha.. thats a big responsibility and I am too lazy.. just read ranch cows wiki ..thats cool
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic