• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sekhar Kadiyala
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes your assumption on my comment is correct Narashima
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, all.
Thank you for your explanations. Now it's clear for me too.
reply
    Bookmark Topic Watch Topic
  • New Topic