Page 1 of 1

About Question enthuware.ocajp.i.v7.2.997 :

Posted: Sat Mar 02, 2013 11:27 am
by The_Nick
Hi,
Why the answer
Write another method setRadius2(int r) and set radiusB accordingly in this method.
should not be a valid answer? by doing that and then replacing sum with 200 it should have done the trick.



The_Nick

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Sat Mar 02, 2013 5:22 pm
by admin
Because in that case other people can still call setRadius, which will set radiusB to 100 - radiusA.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Tue Jul 01, 2014 11:49 pm
by kashyapa
Could someone tell me, why the programmer cannot accomplish this goal by simply changing the sum to 200?

The explanation dos not make sense about it.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Wed Jul 02, 2014 9:33 am
by admin
kashyapa wrote:Could someone tell me, why the programmer cannot accomplish this goal by simply changing the sum to 200?

The explanation dos not make sense about it.
Please post exact code that you want to write. Please go through the explanation once more. It is correct and explains the issue.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Thu Jul 03, 2014 8:07 am
by kashyapa
Ok,

'sum' variable has public access, which means some other class may rely on the vale of this variable. so if we simply change the value of 'sum' from 100 to 200, it may cause subtle bugs on other classes.

am i missing something? :)

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Thu Jul 03, 2014 10:19 am
by admin
You got it!

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Thu Jul 24, 2014 6:04 am
by Marthinus
Even if the code was written with proper encapsulation principles in mind and you change the value of the private variable sum to 200 it would change the behaviour of the code and introduce a bug into legacy code.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Thu Jul 24, 2014 8:57 am
by admin
Marthinus wrote:Even if the code was written with proper encapsulation principles in mind and you change the value of the private variable sum to 200 it would change the behaviour of the code and introduce a bug into legacy code.
The point is to prevent users (and not the owner of the class) from messing with the behavior of the class and impact other users.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Wed Aug 27, 2014 10:28 am
by fabbbb
Make sum = 200 and make all fields (radiusA, radiusB, and sum) private.
The question does not mention that other classes access any of the fields. Under that circumstance I'd say this could be a correct answer.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Wed Aug 27, 2014 7:54 pm
by admin
fabbbb wrote:
Make sum = 200 and make all fields (radiusA, radiusB, and sum) private.
The question does not mention that other classes access any of the fields. Under that circumstance I'd say this could be a correct answer.
Once the class is released, it cannot be known who uses what. public members become part of the public API and cannot be changed because that may break other people's code. That is the point of this question.
For example, Java API has several such methods which have been deprecated since 1.1 (http://docs.oracle.com/javase/7/docs/ap ... -list.html ) but they have to carry them even now.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Sun Jan 17, 2016 5:59 am
by GuillaumeBailly
Hi,

Well, I answered incorrectly too, but I read it again @fabbbb and it says "without breaking existing code". Without this you could consider "Make sum = 200 and make all fields (radiusA, radiusB, and sum) private." to be true. Encapsulation is not always something that you can add later on while ensuring it will not break existing code.

Regards,
Guillaume

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Fri Feb 09, 2018 5:08 pm
by igor.simecki
"breaking existing code" statement is really a subjective matter and I really hope I don't get one of "these" questions.

I consider breaking existing code as in making whatever's using the method not compile, and there are such answers...
This particular matter is breaking the business logic and you assume that everybody's on the same boat with the change.
com.sun. package was always a subject for change and nobody called it code breaking.

why isn't setRadius2 an answer then? only new users would be aware of it and old users would have to rewrite their code... or use the old one, that's not "breaking their existing code".

It's a very ambiguous question if it's really just a matter of encapsulation.

Re: About Question enthuware.ocajp.i.v7.2.997 :

Posted: Fri Feb 09, 2018 11:26 pm
by admin
igor.simecki wrote:"breaking existing code" statement is really a subjective matter and I really hope I don't get one of "these" questions.

I consider breaking existing code as in making whatever's using the method not compile, and there are such answers...
This particular matter is breaking the business logic and you assume that everybody's on the same boat with the change.
com.sun. package was always a subject for change and nobody called it code breaking.

why isn't setRadius2 an answer then? only new users would be aware of it and old users would have to rewrite their code... or use the old one, that's not "breaking their existing code".

It's a very ambiguous question if it's really just a matter of encapsulation.
No, it is not ambiguous at all. "Breaking existing code" quite clearly implies that other existing code either fails compilation or fails to work as expected at runtime.

Encapsulation requires data fields to be private. If you make radiusA, radiusB, and sum private, existing code that is using these variables will fail to compile.

setRadius2 is not a valid option because that doesn't make this class well encasulated because the radiusA, radiusB, and sum will still remain public.

Classes of com.sun package cannot be accused of breaking other people's code because this package was not part of the public API of Java library. No one was supposed to use it in the first place. Further, breaking is not only about code of other people, it is also about code in the same package. In the given question, if you make the variables private, code is the same package will also break.

HTH,
Paul.