aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes question 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 "question" Watch "question" New topic
Author

question

Irina Barbu
Greenhorn

Joined: Feb 19, 2004
Posts: 8
class A {void m1(A a) {System.out.print("A");}}
class B extends A {void m1(B b) {System.out.print("B");}}
class C extends B {void m1(C c) {System.out.print("C");}}
class D {

public static void main(String[] args) {
A a1 = new A(); B b1 = new B();
C c1 = new C(); A c2 = new C();
c2.m1(a1); c2.m1(b1); c2.m1(c1);
}}
What is the result of attempting to compile and run the program?
a. Prints: AAA
b. Prints: ABC
c. Prints: CCC
d. Compile-time error
e. Run-time error
f. None of the above
The answer is a). I thought that it should be b) because when the method is not static, then it is selected based on the runtime type of the object.
Please explain.
Thanks
Bojan Knezovic
Ranch Hand

Joined: Nov 20, 2003
Posts: 90
Hi,
first of all, I too am puzzled - I thought this was legal overriding because, technically, method did not change the list of arguments (C-IS-A(n)-A). But if it was overridden, this

couldn't be executed: C-IS-A(n)-A and C-IS-A-B but not the other way round: A is not a C and B is not a C.
So, since it's not overriding it can only be overloading. But there's another strange thing, it seems that methods are executed starting from the top of the hierarchy tree, not the class C itself... like exceptions.
Good example.
Cheers,
B.
P.S. I have found another example in the book from which it is more than clear that changing an argument to whatever other type ( including something that's IS-A! ) is NOT considered overriding, so the example above is overloading, not overriding and therefore those method calls will be resolved at compile time.
[ March 04, 2004: Message edited by: Bojan Knezovic ]
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,
The example regards an overload, try to change the code as follows:

and you will see that the compiler won't complain.
If m1 in class B would have been overriding then
changing the return type would have been a problem.
The compiler would say something like "...cannot override...
attempting to use incompatible return type."
Bye,
Gian Franco Casula


"Eppur si muove!"
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
From the example we can say that it is overloading as the argument type is different in each of the methods (whether there is any relation between them or not is a different story which gets resolved at run time).
Having said this, (Pardon me If i am asking a silly question) Can we assume that for overloading, resolution happens at compile time and for overriding, it happens at run time?


PMP CSQA SCJP SCWCD SCBCD INS 21 INS 23
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi,
I am really puzzled with the way the programme is working.
Please go through the below program(is part of the above program),
----------
1class A {void m1(A a) {System.out.print("A");}}
2class B extends A {void m1(B b) {System.out.print("B");}}
3class C extends B {void m1(C c) {System.out.print("C");}}
4class D {
5
6public static void main(String[] args) {
7C c1 = new C();
8
9A c2 = new C();
10c2.m1(c1);
11}}
-----------
I am getting the answer as 'A'
Why it is not 'C'.
At line 10, i am invoking the method m1(non static method), on the object of class C, which should invoke the method from 'C' class ( line no, 3).
May be at compile time it resolves the call to the method from class 'A', but at run time it should invoke the method from class, C.
Is it becuase some thing of overloading?
If so, what are all the rules involved here.
Thanks,


Narasimha
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Hi Friends,
I have got the answer to my question.
Please read carefully below 3 points(from K&B Book)
1. Polymorphism applies to overriding, not to overloading.
2. Object type determines which overriden method is used at runtime.
3. Reference type determines which overrloaded method will be used at compile time.
If you see the above question, originally posted by Irina, method m1() is overloaded. Hence here only overloading applies and not overriden. Hence there is no polymorphism and reference type determines which overrloaded method will be used at compile time. Here c2 is of type, 'A', hence all the methods will be called only from the class 'A' and not from any other class, i.e. B or C.
I hope you will understand this, if not we can discuss with some more examples.
Thanks a lot to Irina, for posting the nice example. Previously i have read these points, but till now i didn't think in this view.
Thanks
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
Hi Irina,
the best way to see that the check is done at compile time is to comment the method in class A, you'll see that you get a compile error !!!
the way the method are choosen has nothing to do here if they are static or not
To have overriding you must have the exact type
For example if all the method arguments would have type A instead of type A, B, C the you get output CCC
Hope that helps.


"Did anyone understand what I have just explained? ... because I did not!"
Sekhar Kadiyala
Ranch Hand

Joined: Feb 17, 2004
Posts: 170
Does this mean we can safely assume that for overloading, resolution happens at compile time and for overriding, it happens at run time?
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Originally posted by Dan Andrei:


the way the method are choosen has nothing to do here if they are static or not
.

Dan, do you mean, at compile time always compiler will check whether the methods are available in reference class or not.
If so, then what you told is correct.
If you mean, at run time.
Then it is wrong. At run time, if the methods are overriden it is always matters, whether the methods are static or not. If the methods are overloaded then it is not matter, whether the methods are static or not.
Some one please correct me, if i am wrong. Here i want to put, what all i know, to confirm my understanding.
Thanks,
Narasimha Rao B.
Ranch Hand

Joined: Aug 26, 2002
Posts: 205
Originally posted by Sekhar Kadiyala:
Does this mean we can safely assume that for overloading, resolution happens at compile time and for overriding, it happens at run time?

Hi Sekhar,
Yes, what you told is correct?
Someone please correct me, if the statement is wrong.
Thanks.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Narasimha Rao B.:

Hi Sekhar,
Yes, what you told is correct?
Someone please correct me, if the statement is wrong.
Thanks.

Yes, that's right. The compler chooses among overloads at compile time; the JVM resolves overridden methods at runtime.


[Jess in Action][AskingGoodQuestions]
Dan Andrei
Ranch Hand

Joined: Jan 21, 2004
Posts: 92
Yes your assumption on my comment is correct Narashima
Ashok Vijayakumar
Greenhorn

Joined: Mar 04, 2004
Posts: 2
Hai all,
Its simple ya, its all in understanding the inheritance hierarchy,
B extends A, and C extends B.So from this we can get a point,B's , C's object r nothing but A's object. As B extends A and C also extends A indirectly by extending B. I think this is right.If my thinking is wrong.Anybody can suggest the fault in my thinking.
Irina Barbu
Greenhorn

Joined: Feb 19, 2004
Posts: 8
Hi, all.
Thank you for your explanations. Now it's clear for me too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question