File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Predict the output

 
Prahalad Deshpande
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O/P should be 20
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please tell us where this question came from.
Thanks
 
Maciej Zpolski
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}
 
John Johnson
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure that output is 20. Maciej, class B extends A, so it can refer to protected members.
 
Sanjeev Singh
Ranch Hand
Posts: 381
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)

and


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.
 
Joe Harry
Ranch Hand
Posts: 10047
3
Eclipse IDE Mac PPC Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic