Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding Constructors

Tags:

java

oop

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 ?

like image 908
jackyesind Avatar asked Feb 01 '13 09:02

jackyesind


People also ask

Can you override a constructor?

It does not have a return type and its name is same as the class name. But, a constructor cannot be overridden. If you try to write a super class's constructor in the sub class compiler treats it as a method and expects a return type and generates a compile time error.

What is constructor Overriding in C++?

In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments. This concept is known as Constructor Overloading and is quite similar to function overloading.

What is constructor overloading and constructor Overriding?

Constructors can be overloaded in a similar way as function overloading. Overloaded constructors have the same name (name of the class) but the different number of arguments. Depending upon the number and type of arguments passed, the corresponding constructor is called.

What is constructor Overriding in Java with example?

In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.


2 Answers

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 the Super constructor code, you'll still see Sub in the output. The upshot of that is the overridden method (i.e. Sub.test) is called, even though the Sub 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).

like image 105
Jon Skeet Avatar answered Oct 10 '22 02:10

Jon Skeet


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:

class Err extends Throwable {
   Err(String message) {
       super(message); // Call the constructor of Throwable.
        ..

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.

like image 20
Audrius Meškauskas Avatar answered Oct 10 '22 02:10

Audrius Meškauskas