I am greatly confused with Overriding Constructors. Constructor can not be overridden is the result what i get when i searched it in google my question is
public class constructorOverridden {
public static void main(String args[]) {
Sub sub = new Sub();
sub.test();
}
}
class Super {
Super() {
System.out.println("In Super constructor");
test();
}
void test() {
System.out.println("In Super.test()");
}
}
class Sub extends Super {
Sub() {
System.out.println("In Sub constructor");
}
void test() { // overrides test() in Super
System.out.println("In Sub.test()");
}
}
when i run this i got the result as
In Super constructor
In Sub.test()
In Sub constructor
In Sub.test()
pls note the test method in subclass is executed. Is it shows that Superclass constructor is overridden. Whether is it correct ?
The first implicit line of a constructor is a call to
Super()
. That's why you get those results.Constructors aren't polymorphic - you don't override them at all. You create new constructors in the subclass, and each subclass constructor must chain (possibly indirectly) to a superclass constructor. If you don't explicitly chain to a constructor, an implicit call to the parameterless superclass constructor is inserted at the start of the subclass constructor body.
Now in terms of overriding methods - an object is of its "final type" right from the start, including when executing a superclass constructor. So if you print
getClass()
in theSuper
constructor code, you'll still seeSub
in the output. The upshot of that is the overridden method (i.e.Sub.test
) is called, even though theSub
constructor hasn't yet executed.This is fundamentally a bad idea, and you should almost always avoid calling potentially-overridden methods in constructors - or document very clearly that it's going to be the case (so that the subclass code is aware that it can't rely on variables having been initialized appropriately etc).
You cannot override the constructor as the constructor is invoked by the name of the class it constructs. How can you create some different class with the same constructor? Also, a child class does not inherit constructors from its parent.
If the parent constructor does some important initialization, it can be called from the child constructor using super:
The parent class constructor is also always called. If you yourself do not call any, a constructor without parameters is called automatically before entering a constructor of the derived class. If the parent has no parameterless constructor and you do not call any, a compile time error is reported.
That's correct. When instantiating a subclass, the superclass constructor is called first, and then each successive constructor in the hierarchy.
In your example above, the superclass constructor calls an overridden method (
test()
). This works, but is potentially dangerous since the subclass constructor has not been called, and your subclass will not have been fully initialised. For this reason calling overridden (or overriddable) methods in a constructor is not good practise.it is not overriding super class constructor and constructor can not be overridden it can be overloaded.
When you create child class object super class will be instantiated first then sub class will be instantiated. Its like Child can not be existed without parent.
Compiler will automatically call super class constructor from sub class constructor .