• 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

need explanation

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

output:
base default constructor
derived constructor:9
derived default constructor
could someone explain the output in terms of this() used in the code?


(made not so loud)
[ June 21, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here "this(9)" is used to invoke one argument construtor from default constructor.
[ June 21, 2005: Message edited by: rajan singh ]
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Both your base and derived class have two constructors one defualt and other which takes integer as an argument.

Now have a look at this line
Derived obj = new Derived();


Since you are creating a derived class object without giving any arguments and in your dervied defualt constructor you have not called base class defualt constructor so while intializing your base class, your base class's defualt constructor gets called now you have this line:

this((int) 9);

this refers to the current object and in this program our current object is derived object, which consist of base object + derived object and since we have not called super class's constructor Base(int) explictly so defualt constructor gets called, now base part has been initialized

this((int) 9); statement causes Derived(int j) to be invoked and not the base(int) bcoz "this" will invoke the method of the object being created and in our case Dervied class's ojbect is being created thats why Dervied(int) gets called.

Regards,
 
Rachana Sharma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the explanation but how do you explain this output
class Base{
public Base() {
System.out.println("base default constructor");
}
public Base(int i) {
System.out.println("base constructor:"+i);
}
}

class Derived extends Base{
Derived()
{
this((int)9);
System.out.println("derived default constructor");
}
Derived(int j)
{
super(j);
System.out.println("derived constructor"+j);
}

}

public class test
{
public static void main(String args[])
{
Derived obj=new Derived();
}
}
output:
base constructor:9
derived constructor9
derived default constructor
shouldn't the base class default constructor be called here again in the default derived class constructor??
then ideally the oupt should have been :
base default constructor
base constructor:9
derived constructor9
dervived default constructor
 
Piyush Sam
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your derived class defualt constructor you have this((int) 9),
that will invoke Derived(int j) and since inside this method you are calling super(j) it causes base class integer arugment constructor base(int) to be invoked, so is your o/p

System.out.println("base constructor:" + i);

then your

System.out.println("derived constructor" + j);

and lastly

System.out.println("derived default constructor");

and in your first code u didnt call any super class constructor explictly so base class defualt contructor got called but here you are calling explictly super(int) it will invoke the appropriate base class constructor.

Regards,
[ June 21, 2005: Message edited by: Piyush Sam ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use tags to make your formatted code more readable.
Thanks
 
Rachana Sharma
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the answer it seems to be clear for me now.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic