wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Predict the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Predict the output" Watch "Predict the output" New topic

Predict the output

Prahalad Deshpande

Joined: Oct 31, 2006
Posts: 14
Please refer to the code given below

What will be the output of compiling and running class B ?

Can someone please help me in predicting the output?
venkatesh pendharkar
Ranch Hand

Joined: Apr 29, 2006
Posts: 106
O/P should be 20
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Please tell us where this question came from.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Maciej Zpolski

Joined: Nov 02, 2006
Posts: 11
I think, that this code results in compile time error

public void process(A a)
a.i = a.i*2;//error, i is protected, so we can't use it via reference

Sorry for my english..
John Johnson
Ranch Hand

Joined: Nov 05, 2006
Posts: 39
I'm pretty sure that output is 20. Maciej, class B extends A, so it can refer to protected members.
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
COMPILATION ERROR(It can not be colored with redd...)
The reason being that is the protected members can be accessible only through inheritance,the super class reference can not be used to access the protected members(so accessible only through inheritance).To make it work change the class B as

To understand this statement protected members are accessible only through inheritance,look at the codes below from JLS �(6.6.7)


The one other trick which can make you puzzle is the access of inherited protected x,y from a third class which may be or may not be in a package where the subclass is. So the answer is once the subclass outside the package inherits the protected member,that member (as inherited by the subclass) becomes private to any code outside the subclass, with the exception of subclasses of the subclass.

Hope this will help.

~Sanjeev Singh<br />SCJP 1.5
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10032

This one is clearly explained in the K&B book that we cannot access protected members of the superclass in a subclass other package with a superclass reference.

Damn sure that the above code results in compile time error.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
This is also addressed in the Java Language Specification 6.6.7

[I]Consider this example, where the points package declares:

and the threePoint package declares:

which defines a class Point3d. A compile-time error occurs in the method delta here: it cannot access the protected members x and y of its parameter p, because while Point3d (the class in which the references to fields x and y occur) is a subclass of Point (the class in which x and y are declared), it is not involved in the implementation of a Point (the type of the parameter p). The method delta3d can access the protected members of its parameter q, because the class Point3d is a subclass of Point and is involved in the implementation of a Point3d.

The method delta could try to cast (�5.5, �15.16) its parameter to be a Point3d, but this cast would fail, causing an exception, if the class of p at run time were not Point3d.

A compile-time error also occurs in the method warp: it cannot access the protected member z of its parameter a, because while the class Point (the class in which the reference to field z occurs) is involved in the implementation of a Point3d (the type of the parameter a), it is not a subclass of Point3d (the class in which z is declared).[/I]
I agree. Here's the link: http://aspose.com/file-tools
subject: Predict the output
It's not a secret anymore!