aspose file tools*
The moose likes Beginning Java and the fly likes about constructors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about constructors" Watch "about constructors" New topic
Author

about constructors

Bhuvana Dhruva
Greenhorn

Joined: Sep 11, 2000
Posts: 28
'To call a constructor in the superclass, you have to place the call as the first statement in the calling constructor' what does this mean? please illustrate with an example, it would be clearer.
thanks,

Bhuvana
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Bhuvana,
Here is an example:

Hope this helps.
Stephanie
Ling Wu
Ranch Hand

Joined: Jul 19, 2000
Posts: 184
Also, I think if the superclass constructor requires argument(s)you must provide them in this super() call.
Ruchi Gupta
Greenhorn

Joined: Sep 09, 2000
Posts: 11
Actually in the sub class constructer we need to declare super()(so as to call super class constructor)when we are passing arguments in the constructor.
consider this example:
class base
{
base()
{
System.out.println("from super");
}
}
class sub extends base
{
sub(int i)
{
System.out.println("from sub");
}
}
class testconstructor
{
public static void main(String asd[]){
sub s = new sub(23);}
}
here the output is:
from super
from sub
this shows that when we initalize the sub class then we don't explecitely make a call to the super class constructor then the default constructor is called by itself.
hence here i did not give super still it worked.
i hope this will help

ruchi


Ruchi<br />_ _ ___________________________ _ <br />Tata Consultancy Services,<br />New Delhi, India
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Bhuvana,
The reason why super is to be called first is as follows (I think)
When class B extends class A, the class B already all the elements of B. However B is free to change/add/modify these. So, any initialisation that is to be done should be done first, before it can be changed by class B. Hence the reason to call super()first.
Hope this helps
Good Luck.
Ambi
PS: Its always useful to try and wonder why things are done in a particular way. The how of it comes automatically after that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about constructors