• 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

Constructor Behaviour

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


o/p:

A's default constructor
C's default constructor

A's default constructor
C's argument (int) constructor

A's default constructor
C's argument (String) constructor

Why always A's default constructor is called, even though A is having matching argument constructor with C? When the case the "A's argument (int) constructor" will be printted?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Since there is no default constructor in class B, won't the compiler complain when we say C c = new C();??
 
Micheal John
Ranch Hand
Posts: 344
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the class don't have default constructor, then the compiler will provide a default constructor..

any answer to my question???
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the class don't have a default constructor, the compiler will provide one...that's absolutely true in the sase where you even don't write one. But in class B, you are already writing one constructor that takes a String as an arg which means the compiler won't provide you a default constructor....
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think there is no default constructor in class B since B(String msg) is treated as a function because it is declared with return type as boolean.
So, the compiler will insert an empty constructor.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oopss...I was thinking that there is a constructor in class B, but I did not notice the boolean method.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

So here's the flow, when you say C c = new C(); it invokes the B's no-arg constructor. But since there's none, the compiler puts that in place and that no-arg constructor has super() as it's first statement which in turn calls A's no-arg constructor which prints out A's default constructor and then runs the B's no-arg and then C's no-arg which prints C's default constructor.

Next when you say C c1 = new C(2);, it invokes C's constructor which that takes int as argument which has super() as it's first line, inserted by the compiler. Now this calls the no-arg versions respectively of A and B in that order.

The same applies when you use a String as argument. In order to print the arg versions, you have to insert the call to super that takes the corresponding versions in it's superclass.

Hope this helps.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Micheal John asked December 02, 2006 01:27 AM

Why always A's default constructor is called, even though A is having matching argument constructor with C?



in a constructor, the first line will always be a call to super or a call to this.

If you don't type it yourself, the compiler will include a call to super() automatically.

But it always will call super without parameters.
You may think, it will call super with the same parameter(s) your constructor has. But that is not the case. The automatic call to super is always
super(); // no params

So





of course, only one of them.


Yours,
Bu.
[ December 02, 2006: Message edited by: Burkhard Hassel ]
 
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic