'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
posted
0
Bhuvana, Here is an example:
Hope this helps. Stephanie
Ling Wu
Ranch Hand
Joined: Jul 19, 2000
Posts: 184
posted
0
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
posted
0
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
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.