Overriding with constructors
by , 04-26-2012 at 05:53 PM (1841 Views)
- A constructor cant be overridden in a superclass, because they are non-inherited.
- A constructor cant be overridden in similar class because both of them would have similar signatures and compiler error would be there.
- When a subclass object is instantiated & a method is called by superclass constructor which in superclass gets overridden, superclass constructor will be calling the subclass methods.
Java Code: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()"); } }









Email Blog Entry
License4J 4.0
05-22-2013, 12:23 AM in Java Software