5

I know Constructors are not inheritable in java, we need to use super() - super must be the first statement in Constructors.

But why cant i use @Override annotation?

In example:

public class Foo extends Point2D.Double {
    @Override // The annotation @Override is disallowed for this location
    public Foo(){}
}

If i have a instance of Foo, i never ever can call Point2D.Double.Double() directly! This behave is compleatly like Overriding!

8
  • possible duplicate of Is using the Override annotation in constructors considered a good practice?
    – Joe
    Commented Sep 11, 2013 at 9:36
  • Hm, not a duplicate, but is already answered there.
    – Grim
    Commented Sep 11, 2013 at 9:42
  • Hm, i guess public Foo(int i){super();} wont be able to be have an @Override and Oracle-Developers are too foul to check if the arguments match!
    – Grim
    Commented Sep 11, 2013 at 10:00
  • Calling super() is the same as calling Point2D.Double.Double() directly. Even if you don't write super(); in your constructor it is called anyways. And calling super() is not overriding - it is simply using a constructor of another class. Using is not overriding.
    – Celebes
    Commented Sep 11, 2013 at 10:11
  • @Celebes I did a Breakpoint in Point2D.Double.Double(), removed super(); and debug the creation of a Foo. Unfortunately my Breakpoint didnt debug! Are you sure that i dont need to call super();?
    – Grim
    Commented Sep 11, 2013 at 10:18

8 Answers 8

7

@Override is used when you are overriding a method (not a constructor!), which means that you are creating a method using the same name and parameters as one of the methods from superclass.

There is no constructor called Foo() in your superclass (obviously), because constructors aren't inherited from parent classes, so this is not overriding.

Overriding can be only applied to inherited methods which are not constructors and are not defined as final.

0
3

You cannot override a constructor.

Constructors are not inherited.

Yous subclasses constructor is completely different and independent from super class's constructor (language semantically, due to initializations etc it may depend. ).

While you can call super() to call the super class's constructor it's called chaining not overriding.

4
  • super() is not my point of interrest. You have a full working example without the super(); line.
    – Grim
    Commented Sep 11, 2013 at 10:06
  • I thought that's what contributed to your confusion. Why else would you think that you can override a constructer?
    – Thihara
    Commented Sep 11, 2013 at 10:18
  • Maybe i should have had to remove the super(); line. See ... Foo has only one default-constructor right? It looks like overriding for me.
    – Grim
    Commented Sep 11, 2013 at 10:21
  • You mean it looks to you that Foo() is overriding the superclass constructer? But even the superclass constructer name is different, I'm sorry I don't follow. But I hope my answer helped.
    – Thihara
    Commented Sep 11, 2013 at 10:31
0

Because there is no previous implementation of Foo of which will be overridden.

0

@Override is denoted the place where override supper class(Parent class) method in child class. Constructor is NOT a method and can't override in that way. So you can't put @Override there.

0

Compiler checks overriding rules while compiling the java class. Constructors cannot be overridden so compiler generates an error if somebody tries to use @Override annotation for the constructor.

0

Because you do not ovverride a constructor. As you state constructors are not inheritable. Defining a constructor in a Subclass is not the same process as ovverriding a method. Therefore the JVM invokes automatically super(), or you have to do it yourself, when you define a constructor in a Subclass, which is not required when ovverriding a method.

0

You could override only when you have a method in your parent class. Since parent class does not have Foo() method.. hence no override is allowed.

I believe you are not asking about overriding a constructor like

public class Foo extends Point2D.Double {
    public Double(){
    }
}

this is simply not allowed. However, each subclass constructor has to chain either to another constructor within the subclass or to a constructor in the superclass. So for example:

public class Superclass
{
    public Superclass(int x) {}

    public Superclass(String y) {}
}

public class Subclass extends Superclass
{
    public Subclass()
    {
        super(5); // chain to Superclass(int) constructor
    }
}

The implication of constructors not being inherited is that you can't do this:

// Invalid
Subclass x = new Subclass("hello");
0

The case is that you do not perform override at all. That is why you can not use that annotation. When you declare the a new constructor you are assured that super() will be called by Java. You do not have to force it. What more you can not apply any additional operation before the super(). This lead to the conclusion that if you need to call default constructor from parent class you do not have to do anything special.

The case change when you enforce to use some parameterized constructor, then you have to add that call in child class. But if your parent class has no default constructor then you will always need to call that parameterized.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.