• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encapsulation

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Thanks,
Geeta Vemula.
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See option b is saying about local variable that is method local/automatic variable not about instance variables.
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by punit singh:
See option b is saying about local variable that is method local/automatic variable not about instance variables.



I dont think a local variables( i.e varibles inside a method) can have access modifiers, so they MIGHT be talking about instance variable

correct me, if needed

:roll:
[ December 23, 2008: Message edited by: James Tharakan ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

b. All local variables are declared private.



But James option b is talking about local variable to be private.

like:


class A{
void method(){
private int i=0;//error: not possible
}
}



I think you are talking about instance variables, aren't you ?
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So when can we say that a class is tightly encapsulated?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if instance variable are declared private, so no other class can access or modify directly these variables, I am talking about directly accessing and modifying, not through public methods.

If option b would have been : b. All instance variables are declared private.
then class can be called tightly encapsulated, so it must be true then.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ geeta vemula

Please specify the source.

@punit singh

I think you are talking about instance variables, aren't you ?



Because the local varibles cannot be declared with modifiers, i assumed the question meant ,instance variables.
 
geeta vemula
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it from
http://www.danchisholm.net/oct1/mybook/chapter15/exam1.html
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic