Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Please anyone explain me the working of below proram

 
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Please anyone explain me the working of below proram.How it works?

Both class have two constructors and we have not make a call to super constructot having argument in the sub-class constructor explicitly even super class has a constructor with arguments.Why?






class boo
{
boo(String s){}
boo(){}


}


class bar extends boo
{ static String s;
bar(){}
bar(String s)
{
// super(s);

}


public static void main(String...args)
{
bar b=new bar(s);
}

}

 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure if I got your question right.
Why we are not forced to put in the line that you have commented out?

Because then the call for the parameterless constructor will be made automatically. Because it must be sure that at least one constructor of the super class will be called.
Without the call super(s), super() will be called.

Yours,
Bu.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all,

This is my first post ,

I'm a complete beginner, just finished my BE in mech and studying Java to write SCJP, so guys please go easy on me .

Regarding the above question, if we dont call super(s) would'nt the default constructor of super be called due to super()? If so then an instance of the superclass cannot be called(here its "s") is that right?

Thanks,

Akshay
 
Burkhard Hassel
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 Akshay,

welcome to the ranch!




yes you are right. Without "manually" providing a call to any of the constructors of the super class, the parameterless constructor would be called with super().
Running it with parameter s is ok, because the subclass has this variable and it hands it over to the superclass via this reference (that it is null does not matter here):

As it is it prints:
Boo parameterless

If you remove the // then it prints:
Boo, String: null



Yours,
Bu.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome Akshay This is the friendliest java forum in the entire universe

bar b=new bar(s);
This invokes the String constructor of bar class. bar(String s) {// super(s);}
Note: Every child class constructor must explicitly or implicitly invoke the parent class constructor. In the child class bar(String s) {} will implicitly have a call to super() or you can explicitly call class bar(String s) { super();} In this case there must be a boo(){} defined in parent class or class bar(String s) { super(s);} in this case there must be boo(String s) {} defined in boo class.
Note when you define a parameter constructor in your class Java compiler will not place its default no argument constructor, its your job to define to explicitly as explained above.
Hope this clears
 
Akshay Dashrath
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Burkhard and Deepak. Tried it out on the comp too. Thanks again.

Regards,

Akshay
 
pradeep singh
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Thanks to you all .i am well understood now.
 
Dinner will be steamed monkey heads with a side of tiny ads.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic