F
failure_to
Hello
I haven't been learning Java for the past week or so, but now that I
finally got back to it, I decided to refresh on few things I
previously learned and thus the following questions about dynamic
binding came to be:
Say we have class A reference variable called a. According to my book,
when a.x() is called, the following steps take place ( I won't write
all the steps taken ( or at least not in detail ), but just the ones
that relate to my questions )
1) Compiler enumerates all methods in class A and all public methods
in the superclasses of A to find methods named x
a) Why does compiler only look at public methods in superclasses of
A?
b) besides, doesn't compiler also look at methods ( in superclasses of
A ) which have default access specified ( it does on my system )? I
find it strange that author of the book would forget to mention that
c) If compiler does look for both default and public methods in A's
superclasses, then why doesn't it also look for methods with protected
access modifier?
2) In the next step compiler determines the types of parameters
supplied in the parameter call and finds exact or best match ( this
process is called overloading resolution ) ...
3) if method is declared final, static or private then compiler knows
what method to call else ...
Why point out that if method is private then compiler knows what
method to call? I'm assuming that book is only talking about the case
where private method x() is defined in class A and not in one of A's
superclasses?!
Else the above statement wouldn't make much sense since if method x()
was declared only in one of A's superclasses ( with private access
modifier ), then compiler would report an error and nothing else.
* This may be a silly question, but why can't you override a protected
method? Surely there is some very good reason for java developers
deciding not to allow this?!
thank you
I haven't been learning Java for the past week or so, but now that I
finally got back to it, I decided to refresh on few things I
previously learned and thus the following questions about dynamic
binding came to be:
Say we have class A reference variable called a. According to my book,
when a.x() is called, the following steps take place ( I won't write
all the steps taken ( or at least not in detail ), but just the ones
that relate to my questions )
1) Compiler enumerates all methods in class A and all public methods
in the superclasses of A to find methods named x
a) Why does compiler only look at public methods in superclasses of
A?
b) besides, doesn't compiler also look at methods ( in superclasses of
A ) which have default access specified ( it does on my system )? I
find it strange that author of the book would forget to mention that
c) If compiler does look for both default and public methods in A's
superclasses, then why doesn't it also look for methods with protected
access modifier?
2) In the next step compiler determines the types of parameters
supplied in the parameter call and finds exact or best match ( this
process is called overloading resolution ) ...
3) if method is declared final, static or private then compiler knows
what method to call else ...
Why point out that if method is private then compiler knows what
method to call? I'm assuming that book is only talking about the case
where private method x() is defined in class A and not in one of A's
superclasses?!
Else the above statement wouldn't make much sense since if method x()
was declared only in one of A's superclasses ( with private access
modifier ), then compiler would report an error and nothing else.
* This may be a silly question, but why can't you override a protected
method? Surely there is some very good reason for java developers
deciding not to allow this?!
thank you