Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation

 
Nilesh Parikh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which is the advantage of encapsulation?
A.Only public methods are needed.
B.No exceptions need to be thrown from any method.
C.Making the class final causes no consequential changes to other code.
D.It changes the implementation without changing the interface and causes no consequential changes to other code.
E.It changes the interface without changing the implementation and causes no consequential changes to other code.

I think the correct answer is c)
Any comments
Alkesh
 
veena devi
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct answer is d.
D.It changes the implementation without changing the interface and causes no consequential changes to other code
because encapsulation is meant for hiding your implementation
(code inside your method).your classes are used only through the interfaces(methods name) you provide.so you can change the code inside your method without actually affecting the interface

making a class final is related to another feature of oops called inheritance
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alkesh,
A very good encapsulation implies keeping all states(member vars) as private, and all methods as public. Messages are passed between objects through their methods. Since all member vars are kept private (encapsulated) within the class itself, all other objects can talk to this object only through their public methods. Outsider classe objects CAN NOT change the member vars. So when you want to change the implementation of a method you can always do it without affecting the other parts of the program which uses this class. Which means you can easily change the implementaion at any time without affecting the public interface methods. Outsider classes don't even know that the implemantaion has been changed in the class which uses tight encapluation because outsider classes were never allowed to access the member vars. So changing the implementation makes no difference as far as outsider classes are concerned.
This info can be useful to answer the above qstn. You can also easily say which choices in the above qstn are related to encaptulation.
Please confirm us whether you are convinced with your answers and the reasoning for the same.
regds
maha anna
[This message has been edited by maha anna (edited March 25, 2000).]
 
Nilesh Parikh
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Anna,
I totally agree with you now.
It changes the implementation without changing the interface and causes no consequential changes to other code.
This is the right option
Alkesh
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Your ans is correct and so quick also.
regds
maha anna
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is one of the sample questions at the sun site and i must say it worries me a bit. The grammer is atrocious!
D.It changes the implementation without changing the interface and causes no consequential changes to other code.
Sun says they got all sorts of language experts going thru all questions in the real exam. One would think the few sample questions would at least make sense.
[This message has been edited by Rolf Weasel (edited March 25, 2000).]
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rolf,
Which part of the answer worries you and why? Is that the word interface ? If so, if you read the qstn in this particular context it makes sence.( for Maha atleast. . )
regds
maha anna
 
Rolf Weasel
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
'It' changes the implementation without changing the interface and causes no consequential changes to other code. Encapsulation does not change implementation, a programmer does! and changing the interface can't `cause consequential changes to the other code', it can only `require consequential changes to the other code'!
if there's questions worded like this on the actual exam, gawd help us!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic