A class can not be called "tightly encapsulated" unless which of the following is true?
a. The class is declared final. b. All local variables are declared private. c. All method parameters are declared final. d. No method returns a reference to any object that is referenced by an internal data member. e. None of the above
Answer : e
Here why option b is not correct? if all variables are private then it is said to be encapsulated. Or is there any differance between encapsulated and tightly encapsulated?
Originally posted by James Tharakan: I guess the below fragment of code would justify why option b is not correct.
Even though varibles are private.A sub class can change the values of varibles.(without the aid of particular class object) new subClass().setValue(5,6);
Hope i put my point corretly.
[ December 23, 2008: Message edited by: James Tharakan ]
Actually, the code above is tightly encapsulated. The whole point of encapsulation is to have private instance variables and to offer a public interface to change those variables. That way you can make sure that the state of the object doesn't become inconsistent (you could enforce any rules in setValue() above to make sure of that.)
I don't understand what you mean by "A sub class can change the values of varibles." The method is public, so a class doesn't need to be a subclass to change the private instance variables of A.
All code in my posts, unless a source is explicitly mentioned, is my own.