Every constructor must have a call to super() or this() in it as the first statement. If you don't put it there the compiler will. If the super class doesn't have a no-args constructor you have to explicitly call one of the other constructors.
Its really not necessary to introduce a no-arg constructor in your super class. Just make a call to one of the constructors of the superclass from your subclass constructor or make a call to the other constructor through "this(argList...)" in the subclass that has a "super(argList...)"
i'm reopenning this discussion as i'm still not convinced here the code is
so we can see here that we get a compilation error when call the single argument cnstrtr of derived class by EBH103(4. now it goes to the corresponding cnstctr and from there will it call super() or super(x)as it finds a cnstrctr call there??? now if if it calls super() first and after execution of it comes back to its own body and then again finds super(x) there and goes to super class again then again come back......i think it doesn't work like this....it should, finding super(x) there, call single arg constrtr of the super class.....but it's like this than it means default no-arg cnstrctr is noway getting aclled here so why does it not compile because of the very same reason.......???
What first happens when a class's constructor is called? If the constructor does not specifically invoke the superclass's constructor, the JVM calls the superclass's no-argument constructor, whether that constructor is the default constructor or a no-argument constructor. (There's a difference.)
In other words, the JVM treats a constructor with no call to super() as though the first line of that constructor invokes super(). That's why you need to have a no-argument constructor or explicitly invoke another form of the superclass's constructor.
Your code snippet has:
This is where the call to super() is happening. The compiler knows that you're implicitly invoking super(), but it doesn't see a constructor with that no-arg signature, so it generates a compiler error. Does that help?
Give a man a fish, he'll eat for one day. <br />Teach a man to fish, he'll drink all your beer.<br /> <br />Cheers,<br /> <br />Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Joined: Mar 05, 2005
sorry Jeff i didn't get ur point here .... you say that the constructor with 2 args has a call to super() that's fine but we are not calling it from the derived class anyway... ammmm....if i try to understand what you mean is that there might be a call to that cnstrtr and then it will throw error so compiler dosen't allow the possibility of going to that point ...it's saying that it has tobe proper irrespective of whether you call that cnstrtr or not!!!
Plz corect me if wrong................
Joined: Jul 30, 2003
I was responding to your original post, where EB... was a subclass of D. If you don't invoke a constructor of a class's superclass, the JVM will automatically, implicitly invoke the superclass's no-arg constructor, whether you want it to or not.
The code you posted has an error. Both the classes in your example are extending from Object. You are trying to call Object's constructor which takes an int. There is no cons in Object class that takes an int. Hence the program won't compile.
Code causing the error.
[ March 25, 2005: Message edited by: Anupam Sinha ]
Joined: Mar 05, 2005
to demonstrate what i said i have a code (this time its okay )
if u run this wont give any compilation error as now there is no possibility of super() getting called so okay....