Say I extend a class and override a method in this class. Why is it bad practice to then call the overridden method from the constructor of my new class?
-
1why do you say it's bad practice? It probably depends on the circumstances... – jk. Jun 28 '11 at 20:05
-
2Netbeans underlines it and gives me a warning – user489041 Jun 28 '11 at 20:06
-
OK then, what's the warning say? – gatkin Jun 28 '11 at 20:08
-
Similar problem with C# http://stackoverflow.com/questions/119506/virtual-member-call-in-a-constructor – Bala R Jun 28 '11 at 20:09
-
It just underlines it and days, "Overridable method call in constructor" as if it is warning me something bad is about to happen – user489041 Jun 28 '11 at 20:10
-
@user489041, Sorry if I have mentioned this before, but you have 34 questions without an accepted answer. – Peter Lawrey Jun 28 '11 at 20:15
5 Answers
The main reason not to call overridable methods from constructors is that it allows subclasses to see the class in a half-constructed state. This may or may not be security risk, but it's a bug waiting to happen. Read more here.

- 3,445
- 26
- 26
It is generally a bad practice to do work in the constructor - just get and assign the object dependencies.

- 588,226
- 146
- 1,060
- 1,140
Someone can inherit from your class and change the behavior that you are relying upon in your constructor.
This means that you do not know what you that function is going to do.

- 4,362
- 3
- 32
- 44
Because the resulting instance of the class could be in an inconsistent state. Here's a concrete example.
public class Foo {
private int number;
public Foo() {
number = 42;
multiplyNumber();
}
public void multiplyNumber() {
number = number * 2;
}
public int getNumber() {
return number;
}
}
public class Bar extends Foo {
private int number;
public Bar() {
multiplyNumber();
}
@Override
public void multiplyNumber() {
number = number * 3;
}
}
public class FooBar {
public static void main(String[] args) {
Foo foo = new Foo();
Foo bar = new Bar();
System.out.println("Foo number 1 = " + foo.getNumber()); // Returns 84
System.out.println("Foo number 2 = " + bar.getNumber()); // Returns 42;
}
}
When running through a debug on my machine, bar
never actually calls the multiplyNumber()
method in the constructor; it just gets skipped. As a result, the object doesn't have the expected value in number
.
Constructors should be simple creatures; best not to put anything very complicated in there.

- 2,715
- 4
- 29
- 43
Imagine that you call an overridable method in your constructor. you subclass again and override again. The second subclass can interrupt the work that your first subclass depends on to be deemed fully intialized thus leaving it in a corrupted state.
As for work in the constructor. You can do work, it just generally should be the type of work that is required to initialize your object.
For a best practice, avoid inheritance all together and if you decide to divide constructor work up into methods use visibility modifiers to ensure that all that work remains local to the class being constructed.

- 14,875
- 8
- 50
- 77