• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Can u explais this Encapsulation question

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class GFC500 {private String name;}
class GFC501 {
private String name;
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC502 {
private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}

Which class is not tightly encapsulated?
a. GFC501
b. GFC502
c. GFC503
d. None of the above

Answer is D
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Encapsulation applies to data fields not methods.

As all classes have private instance fields.

Thats why answer is D.

Regards
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class GFC500 {private String name;}
class GFC501 {
private String name;
private void setName(String name) {this.name = name;}
private String getName() {return name;}
}
class GFC502 {
private String name;
public void setName(String name) {this.name = name;}
public String getName() {return name;}
}

Which class is not tightly encapsulated?
a. GFC501
b. GFC502
c. GFC503
d. None of the above

GFC500 totally encapsulated because you can't access the member 'name' at all.

GFC501 totally encapsulated because you can't access the private member 'name' with a private method. No different than GFC500

GFC502, while it is encapsulated, it's not a good encapsulation because the set method allows any value to be set for name. It's as if the member 'name' were public.

Tight encapsulation will not only protect direct acess to data members, but will also prevent those members from being set to improper values. Signs of tight encapsulation might be: Read-only members (only has a getter), or setters that look like:

where the integrity of the new value is checked in some way.

Hope that helps you
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so,is the answer for the question 'b'?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even JavaBean provides the feature of tight encapsulation.

As per JavaBean convention, it defines private fields with public setters/getters.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This old thread.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this one!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Conditions for a class to be tightly encapsulatd:
1. Restrict access to all member variables ( usually with private access modifier)

2. Provide public accessor methods for member variables. Thus the users of your class are compelled to access the member variables only through the accessor methods you specified.

3. Expose the service methods of the class by declaring them as public.

502 satisfy these conditions, so it is the class which is tightly capsulated.

500,501 are not tightly encapsulated.
So out of the options,501 is the right answer.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A class will also not be tightly encapsulated if it returns a reference to an object of some mutable class.



 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't wake the zombies. The original post is over two years old; it's unlikely that the poster is still waiting for an answer.
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whoaaa...the post is of 2006!!!
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Goldina S P", please check your private messages for an important administrative matter.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic