jQuery in Action, 3rd edition
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


Raef Kandeel
Ranch Hand

Joined: Aug 05, 2007
Posts: 87
In Kathy Siera's book, second chapter, page 135, it says:-

One last point on the whole default constructor thing (and it's probably very obvious, but we have to say it or we'll feel guilty for years), constructors are never inherited. They aren't methods. They can't be overridden (because they aren't methods and only instance methods can be overridden). So the type of constructor(s) your superclass has in no way determines the type of default constructor you'll get. Some folks mistakenly believe that the default constructor somehow matches the super constructor, either by the arguments the default constructor will have (remember, the default constructor is always a no-arg), or by the arguments used in the compiler-supplied call to super().

Call me stupid. But I don't understand this point at all.

marc weber

Joined: Aug 31, 2004
Posts: 11343

If -- and only if -- you do not write any constructors for your class, then the the compiler will provide a default constructor. A default constructor will always be a no-args constructor with an implicit call to no-args super(). This is true regardless of what constructors the superclass might have.

For example, suppose your superclass has a single constructor that takes a String, and you do not provide a constructor for your subclass...

In this situation, you will have a problem. Why? Because the subclass will get a default no-args constructor, and this default constructor will try to call super() with no args, but no such constructor exists for the superclass.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Maxi Malahleha

Joined: Dec 11, 2006
Posts: 28
which points don't you understand. please be specific.

default contructor is called implicitly when no other constructor is contructed. you can define you own contructor which can have no-args or have args.

example 1
class Bake{
Bake(){}// if you dont define this it will not be called implicitly by JVM
Bake(int x){}// this is defined constructor made explicitly.
Bake(int...x){}// this is defined constructor made explicitly.


example 2
class Cook{
Cook(){}// this contructor is called every time, wheather you see or you dont see it and on condition that you dont define you our contructor with args.
Raef Kandeel
Ranch Hand

Joined: Aug 05, 2007
Posts: 87
Oh, so all they are saying that the implicit call to "super()" is without arguments and so if you don't define your own no-arg constructor and define a new "arg-constructor" (in which case there would be no default constructor), it would give a compiler error if super() was called. Isn't that redundant? I thought they were making a new point.

. Thank you for clearing that up.
I agree. Here's the link: http://aspose.com/file-tools
subject: Constructors
It's not a secret anymore!