Andrew, In the future it mght be better to post each question in its own thread that way people in the future can find them easily by their subject and also each explaination can get long and it easier to keep them separate to make them easier to read. Q1-- Check out this link for a previous discussion on this topic. There have been many others too. Click on the search link at the top of this page and type in 'protected implementation' (without the quotes) as your search words, you'll get a few other links that might help you out. Q2-- The output of the code you posted should actually be false, true. The == will test to see if the two arrays are the same (basically if they occupy the same location in memory) in this case they dont, the first comparison is false. The second comparison should be true though, because it is comparing primitives (by value). Q3-- According to the JLS section 8.7 the static intializers are executed when the class is first loaded and the they are executed in the order they occur. That is why you get the a,b,c listed first. After that the steps in the JLS section 12.5 are followed. -- instance initializers and instance variable initializers for this class are executed -- the body fo the constructor is finished. Q4-- When an array is created the dimension creation expression are checked from left to right and evaluated in the same order. Each expression is fully evaluated before the next one is. So the expression you posted has x = to 4 initially but the second dimension expresion assigns 3 to x so for all expression after that x is 3 not 4. Q5-- When an array is created the JLS section 15.10.1 explains that when the array is created, right after the space is allocated the elements of the array are intialized to their default values, in this case the default for a boolean would be false. [ April 15, 2002: Message edited by: Dave Vick ] [ April 15, 2002: Message edited by: Dave Vick ]

Dave

Andrew Parker

Ranch Hand

Posts: 178

posted 15 years ago

Thanks a lot for your help. I understand Q.2-5. But, I cannot see the link you attached. Would you mind tell me briefly about the concept of protected implementation? Thanks Andrew

Dave Vick

Ranch Hand

Posts: 3244

posted 15 years ago

Andrew Sorry about the link, I fixed it now. But here is a quote of Janes that explains it pretty well:

'protected' is a strange beast JLS �6.6.2 states:

quote: -------------------------------------------------------------------------------- 6.6.2 Details on protected Access 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. -------------------------------------------------------------------------------- Which basically means that the subclass can only access the protected members of the superclass if it actually creates and uses the superclass member. You can't just pass a superclass object to one of it's methods.

Dave

Andrew Parker

Ranch Hand

Posts: 178

posted 15 years ago

Thanks for help. Andrew

Ricardo Cortes

Ranch Hand

Posts: 140

posted 15 years ago

So, let me see if I have this right. The statement b.process(a) fails because the object reference by a is typed as an A object but really is a B object?

No, the statement b.process(a) fails because the declared typed of a in "a.i" is A, and not the same type in which this access expression ocurrs (B), or one of its subclasses. In this way, the b object cannot access a field for which it is not responsible, given that it was created in the super class A. If the declaration of the parameter a would have been "B a", now the access check would have been allowed because the declared type of a is the same as the class in which ocurrs. Or, in the same speech as JLS: the b object would be able to access now a field (otherwise) declared in the class B, for which it is responsible; after all it was initialized in its constructor. Just a minor thought:

The second comparison should be true though, because it is comparing primitives (by value).

ia[0] and ij[0] are pointing to the same object, that is {1, 2}. While ia[1] and ij[1] are holding the same reference: null. The primitives are in ia[0][0] and ij[0][1] [ May 18, 2002: Message edited by: Jose Botella ]