• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

about overloading

 
Ranch Hand
Posts: 209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers ,
Help me plz...
I need explanation for the following qn

Consider the following code . what is the result of compiling & executing C.java ?


Options :

a . Prints AA to the console
b . Prints AB to the console
c . Prints BA to the console
d . Prints BB to the console
e . Prints CC to the console



Ans is e

How it is "CC"
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason is to be found in �15.12.2.2 of the Java Language Specification.

C is the most specific type argument because class C extends class B and class B extends class A.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my IMHO , when you invoke a static method and the static method is overloaded , you invoke the version compatible with the type of the class who invoked( I think I am being clear ) . I other words . if you replace your code like this " this.doSomething(null,null); " , you are invoking the version that takes an object as C as a paremeter
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have made the following observations Its probably not complete:-

Let us assume methods are overloaded- say we have methods method(int), method(long), method(char), method(Object), method(String), method(Integer).
If an anonymous primitive / object is passed the method invoked will be based on the anonymous primitive / object type. If the primitive / object was not anonymous the method invoked would be based on the primitive�s variable type or object�s reference type.
If the argument is an anonymous null and there are multiple overloaded methods that take reference arguments the compiler will complain about the ambiguity since there are multiple overloaded methods that take reference arguments but there are two inheritence heirachies String->Object & Integer->Object.
If however the methods were method(int), method(long), method(char), method(Object), method(String) and the argument is an anonymous null then since there are multiple overloaded methods that take reference arguments but there is only one inheritence heirachy it will take the highest type here -String.

If there is no corresponding methods an attempt for widening conversion or upcasting will be attempted as with polymorphism.
Otherwise the binding is compile time binding and not dynamic binding as with polymorphism
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic