• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can u explais this Encapsulation question

 
Supriya Nimakuri
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • 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
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • 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
 
Michael Valentino
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • 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
 
lavanya sankuappan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This old thread.
 
Ashish Shirbhate
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this one!
 
Goldina S P
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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.



 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15480
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoaaa...the post is of 2006!!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Goldina S P", please check your private messages for an important administrative matter.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic