If you declare any constructor, what happens to the default no-argument constructor?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

If you declare any constructor, what happens to the default no-argument constructor?

Explanation:
When you declare any constructor, the language stops automatically providing a default no-argument constructor. The compiler only adds that default when you haven’t defined any constructors at all. If you want to create objects without arguments, you must define a no-argument constructor yourself. For example: class Widget { Widget(int size) { /* ... */ } } Without a no-argument constructor, new Widget() won’t compile. If you add one: class Widget { Widget() { /* default init */ } Widget(int size) { /* ... */ } } then new Widget() is allowed. So the default no-argument constructor is not generated once you declare any constructor; you must define it yourself if you need it.

When you declare any constructor, the language stops automatically providing a default no-argument constructor. The compiler only adds that default when you haven’t defined any constructors at all. If you want to create objects without arguments, you must define a no-argument constructor yourself.

For example:

class Widget {

Widget(int size) { /* ... */ }

}

Without a no-argument constructor, new Widget() won’t compile. If you add one:

class Widget {

Widget() { /* default init */ }

Widget(int size) { /* ... */ }

}

then new Widget() is allowed.

So the default no-argument constructor is not generated once you declare any constructor; you must define it yourself if you need it.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy