• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Question related to Constructors

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is a question from javacertificate.com


Question: which of the three constructors will be called.
Answer : public PowerSupply(String) will be called.

Can anyone please explain the answer.
Shyam Ramineni
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The constructer which takes a string object will be chosen because it is more specific. If you receive it as an Object type and try to use it you will get a NullPointerException.

Bottom line: A null String object has more meaning than a null Object object.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class GrandParent
{
}

class Parent extends GrandParent
{
}

class Child extends Parent
{
}

public class NullCheck
{
public void isOn ( GrandParent g )
{
System.out.println ( " GrandParent " ) ;
}

public void isOn ( Parent g )
{
System.out.println ( " Parent " ) ;
}

public void isOn ( Child g ) // ( 1 )
{
System.out.println ( " Child " ) ;
}

public static void main ( String args[] )
{
NullCheck nc = new NullCheck ( ) ;
GrandParent g = new GrandParent ( ) ;
Parent p = new Parent ( ) ;
Child c = new Child ( ) ;

nc.isOn ( g ) ;
nc.isOn ( p ) ;
nc.isOn ( c ) ;
nc.isOn ( null ) ; // ( 2 )
}
}
------------------------------------------------------------------

when this code is executed the line at ( 2 ) will call the method at ( 1 )
since the Child class is the smallest in the inheritance heirarchy , as a result of which it is chossen for execution hwen the method isOn ( ) is called with "null" as the formal parameter

since object --> string ( ie. the string class is derived form the object class ) , so when the method is called with the null parameter the method corresponding to the string is executed

iam including one more code whichh illustartes this issue more

regards,
bhavesh
 
shyam ramineni
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bhavnesh,

Thanks for sample code.

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

Originally posted by Marzo Kaahn:
If you receive it as an Object type and try to use it you will get a NullPointerException.



And what will happend if you receive it as an String type and try to use it? As you can guess, you will get a NullPointerException.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

That's what I've found in JLS

Widening Reference Conversions:
  • From any class type S to any class type T, provided that S is a subclass of T. (An important special case is that there is a widening conversion to the class type Object from any other class type.)
  • From the null type to any class type, interface type, or array type.


  • So, then a compiler see this code:


    In Line 1 there are two methods which can be called. foo (A a) and foo (B b). The compiler will choose nearest widening reference conversion and foo (B b) will be called.

    The same situation appears in Line 2. null can be "widened" to any class type. Once again, the compiler will choose nearest widening reference conversion and foo (B b) will be called.

    As you can guess, this code will cause a compile time error (in Line 1):


    That's all, folks.
     
    shyam ramineni
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi All,

    This is a similar kind of question from Dan's Website.
    Answer is it fails to compile.
    I couldnot understand the reason stated in the website.
    Can anyone explain the result.



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

    Originally posted by shyam ramineni:
    Hi All,

    This is a similar kind of question from Dan's Website.
    Answer is it fails to compile.
    I couldnot understand the reason stated in the website.
    Can anyone explain the result.



    Thanks
    Shyam Ramineni




    Could you please provide the answer given on web site?
     
    shyam ramineni
    Ranch Hand
    Posts: 43
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    Answer(gives in website): Fails to Compile.

    Shyam Ramineni
     
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Mr George Bolyuba: that was a nice explanation.

    Could any of you please explain the following:

    class bas{}

    class der extends bas{}

    public class Main {
    public void f1(String o){
    System.out.println("String");
    }
    public void f1(der o){
    System.out.println("der");
    }
    public static void main(String[] args){
    new Main().f1(null); //----> AMBIGUOUS
    }
    }

    Why is it ambiguous now?
    Object->String
    Object->bas->der

    So (Mr George Bolyuba - beg your attention), it should not be ambiguous.

    You can claim der also extends from object. OK .. but a given class has only one super class and der has bas as its super class bas.
     
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It's ambiguous because String and the other class are in different hierarchy so how the compile will know wich one is the most specific class? It's differet if you have one parameter as an OBject and another as a String. As we know String extends object, so String is more specific. If you pass null, the compiler will run the method that has a String as an argument.
     
    Prasanta Chinara
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Marcos. Thats neat :-)
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic