Dan Andrei

Ranch Hand
+ Follow
since Jan 21, 2004
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Dan Andrei

I posted the same q a week ago, but nobody came up with an explanation
Can please someone shed some light on it...
Give the code below:
public class X {
class Y {
private void f (){}
}
class Z extends Y {
{ //initializer
//f(); // error; not inherited
//this.f(); // error; not inherited

((Y)this).f(); // 1 - OK access granted
super.f(); // 2- OK access granted

}//end initializer
}//end class Z
}// end class X

it compiles fine.
The commented statements do not compile because method Y.f(), being private is not inherited in class Z private
Please tell if I’m correct in my reasoning:
For statement 1 to compile two conditions must be satisfied:
A) both classes must be enclosed by a third (they can access each others private members)
B) class Z has to extend class Y
But I do not understand why it works
Can someone tell why the statemnent 2 also compiles
Thanx
"almost" is the key here Bose, thats why I posted the piece of code
using the f() method call doesn't intrigue since even if its private we are in an inner class, so its allowed
The super call is the one which I cannot "grasp", how the compiler "looks" at it at "says" its ok ??,
AFTER ALL ITS CLEAR THAT F() IS NOT INHERITED AS I SHOWED ON THE COMMENTED LINES
class Outer {
class Inner { }
}
public class InheritInner extends Outer.Inner {
InheritInner (Outer o1) {o1.super(); }
public static void main(String [ ] args) {
Outer o = new Outer( ) ;
InheritInner ii = new InheritInner(o) ;
}
}
Sorry Nicholas but you are wrong...
since the superclass is enclosed by another class u must pass it to the c-tor of the subclass, like I did above...and the using the outer instance you call the superclass c-tor
hope I make sense
A class can have ONLY field declarations, methods definitions...
an SOP is a method invocation OR expression, you can have it in intializers or in other methods NOT in a class directly
hope I make sense
Give the code below:

it compiles fine.
The commented statements do not compile because method Y.f(), being private is not inherited in class Z private
Please tell if I’m correct in my reasoning:
For statements 1 to compile two conditions must be statisfied:
A) both classes must be enclosed by a third (they can access each others private members)
B) class Z has to extend class Y
The expression((Y)this).f() refers to method f() of the current object, but with the current object viewed as an instance of the superclass (because of the casting). Method f() is accessible because condition A is satisfied so the statement compiles
Please tell me if I missed something
Can someone tell why the statemnent 2 compiles

(code tags and indentation added)
[ May 02, 2004: Message edited by: Barry Gaunt ]
[ May 02, 2004: Message edited by: Barry Gaunt ]
Please go to this link http://www.sys-con.com/story/?storyid=34292&page=1
and the mystrey is solved !!!
Boy I went NUTS on these one...
Ok let me explain this again.
when I compile the code a get no error (i use the sun jdk 1.4.2_03)
however i,ve seen this example on earlier posts and people said that they got error on it because of the ambiguos method call.
I agree with that since if we look at JLS 15.2.2
aMethod(int) which is more specific is defined in the superclass.
if we swap the methods it satisfies the two conditions listed in jls 15.2.2
My question is
Is this a compiler bug or am I misreading the rule mentioned above
thanx
Given the code below:

it compiles and runs with the output:
This is the superclass2
so the method aMethod(int) in picked in class Pointx being more specific
HOWEVER: IN JLS 2.0/15.12.2.3 - OVERLOADING - specific methods
" Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:
* T can be converted to U by method invocation conversion.
* Tj can be converted to Uj by method invocation conversion, for all j from 1 to n. "
according to these definition the first (*) is not true in our case, since the T is a superclass.
In an earlier post dated march 16 2000 from where I took the example, for this code they get compile error, because of the rules above
I use the latest compiler fron Sun, so which one is right ???
Edited by Corey McGlone: Added CODE Tags and formatted code for readability.
[ April 29, 2004: Message edited by: Corey McGlone ]
[ May 01, 2004: Message edited by: Barry Gaunt ]
thanx for the reply, I had a "hunch" that was the reason , but I needed a confirmation.
One more thing:
SO the only time I really need to use an
aceess like Obj1.Obj2=new Obj1.new Obj2() IS WHEN CLASS Obj3 HAS ALSO A
CLASS Obj2, which hides the Obj2 class in Obj1 ?

When I compile the above its OK
at point (***1) I instatiate an instanance of the Obj2 class inherited in Obj3
at point (***3) I do the same thing and I can “drop” the outter class instance since the this is passed implicitly to the method. I can also drop the qualifing Obj3.Obj2 since I’m in a method of Obj3 so I use just Obj2
Question:
At point 2 I do NOT understand why the line Obj2 oo2=new Obj1().new Obj2()
its ok !!! I think it should be Obj1.Obj2 oo2=new Obj1().new Obj2(); since the variable instantiated it’s the inner class of Obj1, and we are in a method of Obj3 ???
I notice that if Obj3 does not extend Obj1 I get a compile error for the above, but I do not understand how inheritance can make the line compile, after all we are dealing with an object that does not belong to this class: new Obj1().new Obj2();
Edited by Corey McGlone: Added CODE Tags and Reformatted Code for readability.
[ April 22, 2004: Message edited by: Corey McGlone ]
java language specification - jls
to get the concepts read jls 14.20, If I get into details I'll just repeat the text there
its just a warning not an error, the code compiles
If your question IS WHY DOES IT GIVE A WARNING ?
I,ll try a long shot here:
the finally block must comlplete before the method retruns. The compiler sees a return which means abnormal completion, so it barks a warning.
If some stement was after the return it would give a compile error
oops.. disregard the line below from the first quote:
"The first statement in a nonempty block that is not a switch block is reachable iff the block is reachable"
Also note that if you put the continue in an If clause you avoid the compilation error:
"An if-then statement can complete normally iff it is reachable. The then-statement is reachable iff the if-then statement is reachable."
jls 14.20
Hi,
I'm in a bnit of a hurry so I'll just give u some pointers, if is something unclear please post again
So:
System.out.print (....) is an experssion statement
the below is from JLS 14.20
"An expression statement can complete normally iff it is reachable.
A break, continue, return, or throw statement cannot complete normally.
The first statement in a nonempty block that is not a switch block is reachable iff the block is reachable. "
and this is tmhe most important
"Every other statement S in a nonempty block that is not a switch block is reachable iff the statement preceding S can complete normally."
since continu vannot compelete normally System.out(...) is unreacheable
Hpe I make sense
see you soon