• 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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Please check out the following code :


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

Correct answer is : AAA

This is a case of overloading , so it is the reference type which determines the method to be invoked.
Now c2.m1(a1) will return A
c2.m1(b1) ?? here the reference arguement is of type B
c2.m1(c1) ?? here the refernce arguement is of type C

Thanks
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anjali

https://coderanch.com/t/246076/java-programmer-SCJP/certification/Overloading

ur problem has been solved here ..
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic