• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Doubt with constructor overloading

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source http://www.valoxo.ch/jr/mocks/mock01a.html Q42

What is the output of the following code when compiled and run? Select one correct answer.
public class Question42{
Question42(byte b){System.out.println("in byte");}
Question42(short s){System.out.println("in short");}
Question42(char c){System.out.println("in char");}
Question42(int i){System.out.println("in int");}
public static void main(String[] args){
Question42 q42 = new Question42(2);
}
}

A. Prints: in int

B. Prints: in char

C. Prints: in byte

D. Prints: in short

E. Compilation error because of ambiguous constructor invocation.

Ans: A
Doubt: why not C as it is the best match?


pls help,
Gitesh
[ September 06, 2007: Message edited by: Gitesh Ramchandani ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The value 2 could be set into byte int and short also if am not wrong. There is no way to identify who should be given preference. I think thats the reason for compilation to fail.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A number literal that is less than long, is taken as int, even if it fits a smaller type.
By the way, this is not constructor overriding, but overloading.
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I modify the question as follows:



I get answer:


In the above case whats happening as compared to my doubt in 1st post?

Please help,
Gitesh
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy, cowboys!

You cannot override constructors, because they are not inherited.
You are talking about overloading - and be prepared for text-only questions that will test your knowledge about nomenclature as well.
You should not mix the technical terms, not even in talking to your co-ranchers.

The question does not necessarily has anything to do with constructors at all, the same would work with methods:

prints:
int
float



In the first part, no type conversion is needed, so foo just takes an int as the int number.
In the second part, there is no bar method for an int parameter, so in this case the compiler takes the "slowest" higher method possible.


Quizzzzz:
What constructor will be chosen here?


;)

Yours,
Bu
 
Gitesh Ramchandani
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bu thanks..

well i compiled the quiz, it is looking for an int constructor.

I got my answer thanks.
 
What's that smell? Hey, sniff this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic