• 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

overloading question

 
Ranch Hand
Posts: 3852
  • 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 c1 = new C(); C c2 = new C(); c1.m1(c2);
}}
What is the result of attempting to compile and run the program? (Select 1.)
a. Prints: A
b. Prints: B
c. Prints: C
d. Compile-time error
e. Run-time error
f. None of the above

I marked D but the answer given is : A

please help

thanks .
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c1.m1(c2); is only coorect at RUNTIME, but at compile time the compile wont check the underlying types, i mean when compiling c1 is of type A and c2 is of type C2, unfortunately when u compile there is no correlating method to void m1(C2) in your class A.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means i was right .

overloading questions , in this link question number 7th,9th & 11th are using same concept & i think all 3 are wrong .
[ January 12, 2005: Message edited by: rathi ji ]
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rathi,

I tried compiling and running your code and output is "A".
So option A is right..

You didnt get complier error because C extends A. So method m1(A a) can accept any class that is sub class of A as argument.

If you add

to your code, it would report compiler error as D doesnt extend A and not method m1() in class A that accepts D as parameter.

Thanks,
Raja.
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also tried the code is correct and giving output A
but I am not getting the point .



in the code , i am creating same situation & it is giving compilation error ( as per expectation )

can any body explain the case ...

thanks a lot .
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes because there is no method my() in your Test1 class.

The method binding takes place during compile type base on Reference Type of class and NOT by runtime type of class .

In earlier eg: the method invocation c1.m1(c2), c1 is of type class A and there is a method m1() in A which accepts argument of type A. As c2 references class C which if type A. This method runs.

Did you get it...?

Thanks,
Raja.
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
look at pg no 309 of K & B book...
 
ankur rathi
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yaa , I got it ..
thanks .
 
reply
    Bookmark Topic Watch Topic
  • New Topic