aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructors ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructors ??" Watch "Constructors ??" New topic
Author

Constructors ??

Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78


In the above example why is it necessary to introduce a no-arg constructor (line 1)in the D class, failure to do so results in complier error.

pl help.


SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
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.
Balasubramani Dharmalingam
Ranch Hand

Joined: Dec 06, 2004
Posts: 116
whenever u define any constructor , it will call its super class default construtor (ie no args construtor ).

if super class did nt have no args contructor it will cause compile error


class A{
A(int i){
// here it will implicitly call the super class no arg constructor
}

}


S.D.Balasubramani
SCJP 1.4


Balasubramani SD,<br />SCJP 1.4,SCWCD 1.4,SCJP 5.0<br /><a href="http://sd.balasubramani.googlepages.com" target="_blank" rel="nofollow">www.sd.balasubramani.googlepages.com</a>
Rahul Bhosale
Ranch Hand

Joined: Mar 10, 2005
Posts: 77
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...)"


RB
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
thanks a lot..
I understood the concept ..
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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.......???

thanx amit
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
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. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
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................
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
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.

Did that help?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Amit

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 ]
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
yeah...typo!!!my apologies....

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....

thanx
amit
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructors ??