About Question com.enthuware.jfcja.v8.2.414 :

Oracle Certified Foundations Associate Java Certification Questions and Discussion
1Z0-811

Moderator: admin

Post Reply
nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

About Question com.enthuware.jfcja.v8.2.414 :

Post by nick12345 »

Hello,

can you please explain what does this sentence mean?
It helps make sure that clients have no accidental dependence on the choice of representation
thank you :)

admin
Site Admin
Posts: 9700
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question com.enthuware.jfcja.v8.2.414 :

Post by admin »

Let me explain it with an example. Let's say you have a ExamResult class and it has a method passed(), which returns a true or false depending on whether the user has passed the exam or not. Something like this:

Code: Select all

class ExamResult{
   public int marksScored;
   public  boolean passed(){
      return marksScored>50; //pass if marksScored is >50. 
   }
}
In the above code, the abstraction is passed/failed and you are representing that abstraction through the marksScored variable. However,
observe that marksScored is public. Therefore, any user of this class can start relying on the marksScored field, because it is accessible to all, instead of passed() method.
Later on, you decide to use Grades (A B C D and F) instead of marks as the choice of representation and want to change passed() method code to: return !'F'.equals(grade)?

But other user's code will now fail to compile because they are depending on the marksScored field to be present in the ExamResult class. They were not really supposed to use this field. They should have used the passed() method. But you didn't make that field private, so, they found it and used it anyway. Had you made marksScored as private (i.e. made the class encapsulated), they would not have used this field and their logic based on passed() method would still work even if you change marksScored to grade.

Thus encapsulating a class properly prevents this breakage from happening.
If you like our products and services, please help us by posting your review here.

nick12345
Posts: 17
Joined: Fri Aug 13, 2021 12:43 pm
Contact:

Re: About Question com.enthuware.jfcja.v8.2.414 :

Post by nick12345 »

Thank you very much for your great explanation :)

1z0811808
Posts: 1
Joined: Wed Sep 22, 2021 3:00 am
Contact:

Re: About Question com.enthuware.jfcja.v8.2.414 :

Post by 1z0811808 »

Also the method returns a value so the VALUE can be changed from marksScored which is int to Grade(A,B,C,F) which is 'char'.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests