File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can u explais this Encapsulation question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can u explais this Encapsulation question" Watch "Can u explais this Encapsulation question" New topic
Author

Can u explais this Encapsulation question

Supriya Nimakuri
Ranch Hand

Joined: May 23, 2006
Posts: 83
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

Joined: Apr 25, 2005
Posts: 809
Encapsulation applies to data fields not methods.

As all classes have private instance fields.

Thats why answer is D.

Regards


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Michael Valentino
Ranch Hand

Joined: Nov 01, 2005
Posts: 96
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


SCJP 1.4, SCWCD J2EE 1.4, SCJD J2SE 1.5, SCBCD J2EE 1.3, SCDJWS (In Progress)
lavanya sankuappan
Greenhorn

Joined: Jun 02, 2006
Posts: 17
so,is the answer for the question 'b'?
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Even JavaBean provides the feature of tight encapsulation.

As per JavaBean convention, it defines private fields with public setters/getters.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
This old thread.
Ashish Shirbhate
Greenhorn

Joined: Jun 14, 2006
Posts: 8
Check out this one!
Goldina S P
Greenhorn

Joined: Sep 18, 2008
Posts: 1
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

Joined: Aug 03, 2008
Posts: 9317
    
  17

A class will also not be tightly encapsulated if it returns a reference to an object of some mutable class.





SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9317
    
  17

Whoaaa...the post is of 2006!!!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

"Goldina S P", please check your private messages for an important administrative matter.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can u explais this Encapsulation question