• 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

A SCJP cram test question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
The following is a cram test question which I don't agree with author's answer. Please tell me what's your answer and why? Your help is highly Appreciated.

====
Consider following two classes:
What will be the output of compiling and running class B ?

Select 1 correct option.
a. It will print 10.
b. It will print 20.
c. It will not compile.
d. It will throw an exception at Run time.


=============
My answer is 'b'. I compiled and tested code in linux machine with java 1.4.2 and result is 20.

But the author's answer is 'c', the reason is : Although, class B extends class A and 'i' is a protected member of A, B still cannot access i, (now this is imp) THROUGH A's reference because B is not involved in the implementation of A.
Had the process() method been defined as process(B b); b.i would have been accessible as B is involved in the implementation of B.
For more information read Section 6.6.7 of JLS:
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36191
[ September 06, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiled with Java 5.0 (in Eclipse) it says that the field A.i is not visible on the line:
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liping-

Check and make sure that you have each of those classes in separate packages. The variable should not be visible according to the specification as class B is in a different package and than class A and the code that is trying to access the protected variable is not "responsible for the implementation of the object".

From the Java Language Specification (3rd edition)


A protected member or constructor of an object may be accessed from outside the package in which it is declared only by code that is responsible for the implementation of that object.



Josh
 
Ping Li
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Joshua,
You are right. When I put class B in a different package, the code cannot be compiled and indicated that a.i is not accessiable from class B. Thanks a lot!
 
Ranch Hand
Posts: 481
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey

can you please explain me this one clearly

i havenot come out of process()

i am preparing for scjp 1.4

i would be happy if you can explain me this one
 
Joshua Smith
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthik-

Here is a quote from Chapter 3 of Complete Java 2 Certification (5th edition) that may answer your question.


A protected feature of a class is available to all classes in the same packages, just like a default feature. Moreover, a protected feature of a class can be available in certain limited ways to all subclasses of the class that owns the protected feature. This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.

It's important to be aware that different-package subclasses don't have unlimited access to protected superclass features. In fact, different-package subclasses have only the following privileges:

-They may override protected methods of the superclass.

-An instance may read and write protected fields that it inherits from its superclass. However, the instance may not read or write protected inherited fields of other instances.

-An instance may call protected methods that it inherits from its superclass. However, the instance may not call protected methods of other instances.



Does that help?

Josh
[ September 08, 2005: Message edited by: Joshua Smith ]
 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might be instructive to examine several ways the "bug" would be fixable in real work.

1. use a setter...
public class A { ...
protected void setI( int i) {
this.i = i;
}
...
public class B {...
public void process(A a) {
a.setI( i*2); }

2. change the type of the parameter in the process method to B...
public void process(B a) {
a.i = a.i*2; }
// this makes parameter a an instance of B, so it can access i.
// and the a instance can be passed as the argument
// into process(B a)
// since class A's a is in fact a subtype of B. wheew!

3. make A's i attribute public. ... but don't do that in reality
reply
    Bookmark Topic Watch Topic
  • New Topic