Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Method Overriding...

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem : The Constructor sample(String) is ambigious. How?
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler will complain cause either constructors can be passed a null, it will not be able to distinguish the right constructor to invoke!

Best of luck ...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sample(String) isn't ambiguous.

sample(null) is ambiguous.

null is accepted by both constructors, but neither constructor is more specific than the other.
 
Pankaj Patel
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Keith, So according to you the following code should also give error. But it does not give any error. Why?




Thanks for efforts.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This wont give any error because of the inheritance relation between
String and Object.
It will call the more specific class that is the String Constructor.
[ April 20, 2007: Message edited by: Tina Tibrewal ]
 
Pankaj Patel
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, in the case of String and StringBuilder, both are at the same level of hierarchy, i mean both are extended from Object. That is why it gives error. Accepted.

But what in the case of Integer and String argument type.

: String -> Object
: Integer -> Number -> Object

// -> Indicates Extends From



But Still Compiler error Resides. Can you explain me in this case?
Thanks in Advance.
[ April 20, 2007: Message edited by: Pankaj Patel ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


package SCJP;

class sample
{
sample(String s)
{
System.out.println("String");
}

sample(Object o)
{
System.out.println("Object");
}
}

public class Constructor
{
public static void main(String arg[])
{
sample s1=new sample(null);
}
}




In case of sample(Object o) and sample(String o):

String IS-A(n) Object:
so calling a constructor with String arg is more specific that Object; because what is passed to an String can also be passed to object and vice versa is not true; so no ambiguity and call to sample(String args) is made.



In case of sample(String s) and sample(StringBuffer s):

There is no relationship between String and StringBuffer:
null can be substituted to the String and StringBuffer too, so the compiler is confused what to do, which constructor to call. And finally compiler complains that this is ambiguous call;




Regards,
cmbhatt
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuilder is a subclass of Object
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html

String is also a subclass of Object:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html

This means they are unrelated. When you pass in null, the complier does not know which one to call.

If there are two objects that are related (one is a subclass of the other), the most specific one will be called. This is the case with Object and String.

Hope this helps.

Cory
 
Pankaj Patel
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you very much Everybody. I finally Got it.
 
If you believe you can tell me what to think, I believe I can tell you where to go. Go read this tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic