• 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

confusing

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<PRE>
class Child extends Parent
{
{
System.out.println("init bloc in Child");
}

Child()
{
this(7);
System.out.println("Inside Child()");
}

Child(int i)
{
super(i);
System.out.println("Inside Child(int i)");
}

public static void main( String args[] )
{
Child c = new Child();
}

static
{
System.out.println("static init in Child");
}
}
class Parent
{
{
System.out.println("init bloc in Parent");
}
Parent()
{
System.out.println("Inside Parent()");
}
Parent(int i)
{
System.out.println("Inside Parent(int i)");
}

static
{
System.out.println("static init in Parent");
}
}
</PRE>
Result:
static init in Parent
static init in Child
init bloc in Parent
Inside Parent(int i)
init bloc in Child
Inside Child(int i)
Inside Child()

I've been reading the archives and I saw a program similar to this. I got confused because "this" and "super" are inside the Child constructors. Does this mean that if "this" will eventually lead you to a parent constructor it will be executed first. And when the constructors inside the Child class are called they will still execute "this" then "super" except they will no longer lead to the parent class. I hope you understand what I'm saying. Can you please give me a better explanation.
Thanks in advance.
panday

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your text was a bit confusing, so let's start at the basics.
Contructors are always called when creating an object, but you knew that. In case of inheritance, the constructor of the baseclass is always called before subclasses, in top-down order.
That is the basics. However with the use of this and super, we can gain a bit of control over what is called. You know about overloading of methods. It's the ssame with constructors. all this and super does is call a constructor that mathes what ever parameters we send with it, like a normal methodcall, in the same class (this) or in the superclass emidiatly above it (super).
If there is no call to super in a childclass, it will automaticly call the super-constructor with no parameters.
Calling this() is just saying that some other constructor in the same class should be runned before the one you're in right now.
Does this answer what you where wondering about?
/Mike
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Panday
In a constructor the first line must be either a call to another constructor in the same class or a call to the superclass constructor. The superclass must always be constructed before the subclass is. The 'this' keyword has a special use in constructors to refer to another constructor in the same class. While the 'super' keyword is used to call the constructor of the superclass. You can use 'this' in your constructors because it is just calling another constructor in the same class a superclass constructor will still be executed before one of the subclasses constructors is run to completion. I added comments to your code to help you follow:

Originally posted by Panday Manako:
<PRE>
class Child extends Parent
{
{
System.out.println("init bloc in Child");
}

Child()
{
this(7); // calls the Child constructor that takes an it as the argument
System.out.println("Inside Child()");
}

Child(int i)
{
super(i); //** calls the Parent constructor that takes an int as an argument
System.out.println("Inside Child(int i)");
}

public static void main( String args[] )
{
Child c = new Child();
}

static
{
System.out.println("static init in Child");
}
}
</PRE>



If you dont specifically call a superclass constructor the compiler will puit in call to the default constructor of the super class. So in your code if you got rid of the line with the ** it would call the Parent constructor with no arguments.
hope that helps

Dave
 
Mikael Jonasson
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to avoid confu�sion:
It isn't the default constructor that get's called, it's the constructor without parameters (wich if you have no othe constructors is what the default constructor looks like) if you leave out a call to super.
The default constructor is the name of the constructor that the compiler creates if you have no other constructors. This is good to know, since if you have other constructors, and none is the one without parameters, you will get a compiler error if you don't use super() in alla child constructors, since the compiler have no constructor to call.
/Mike
 
Panday Manako
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it now. Thank you Mike. Thanks Dave.
 
The harder I work, the luckier I get. -Sam Goldwyn So tiny. - this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic