4

See below example:

class A {
   A() { this(1); }
   A(int i) { System.out.println("A" );  }
}

class B extends A {
    B() {}
    B(int i) {  System.out.println("B" );  }
}

public class Test
{
    public static void main(String[] args)   {        
       A o =  new B();
    }
}

The output:

A

Q1:Seems java does not perform late binding for "this(1)". It has been decided at compile-time.Please confirm.

Q2:Java does not perform late binding on any constructors. Please confirm.

Q3:Does this mean constructors are implicitly final?

Don Li
  • 1,095
  • 2
  • 12
  • 17
  • Related: [Can we override a constructor in Java and can a constructor be private?](http://stackoverflow.com/questions/5430569/can-we-override-a-constructor-in-java-and-can-a-constructor-be-private) – Jesper Jun 07 '12 at 07:51

6 Answers6

7

You cannot override constructors. They don't follow inheritance rules at all. They can't follow inhertitance rule because you need a simple order for constructing your object.

e.g. in your example, if you could override the A(int) constructor A() would call B(int), but B(int) implicitly calls super() which is A() and you have infinite recursion.

It is often considered bad practice for a constructor to call an overrable method. So having constructors do this automatically would be a very bad idea.

If the constructors were final, like static final methods you wouldn't be able to hide them either, but you can, so I would say they are final either.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

Java does not override the constructor.

A o = new B(); It will call B() wherein it will call super().

A() will be called and here you are calling this(1) which means it will call A(1) so nothing strange .Everything is decided at compiletime when you talk about constructors.

Answers:

Q1:Seems java does not perform late binding for "this(1)". It has been decided at compile-time.Please confirm.

Yes,Only at compile time constructors are decided.

Q2:Java does not perform late binding on any constructors. Please confirm.

Since doesn't override so no late binding.

Q3:Does this mean constructors are implicitly final?

No they are not final but you cannot override them.

EDIT: A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. Refer Java Docs

amicngh
  • 7,831
  • 3
  • 35
  • 54
1

Q1: true

Q2: that is not about the constructor, it is more about this. this always refer to a method/field present in the class or the super class, not in the children. That makes sense since the parent does not know the implementation of the child

Q3: Kind of, Constructors are special method and it does not make sense to override them since they are chained

tibo
  • 5,326
  • 4
  • 37
  • 53
0

When you create an instance of a subclass, you must invoke a constructor of the superclass.

If you don't specify a constructor, the default (no-args) constructor is called for you.

In other words, the no-args constructor in B, is actually executed as:

B() {
    super();
}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • This doesn't address the question. He knows that `A::A()` is being implicitly called. He's confused why the `this(1)` in `A::A()` calling `A::A(1)` instead of `B::B(1)`. – Corbin Jun 07 '12 at 07:55
0

When you instantiate a subclass constructor, it first invokes its superclass' default constructor before performing its own task. This is done implicitly or via super() reference.

So a call to B() will call constructor A() which will call A(1)

Similarly if you call B(2) the output will be ( It will call default constructor A() and then A(1)

A
B
dejavu
  • 3,236
  • 7
  • 35
  • 60
0

'"this()" is not overridden' doesn't mean anything, and neither do any of your other questions.

All that is happening here is that the constructors for B are both implicitly calling super(), because you didn't code anything else, and super() is A(), which calls this(1).

user207421
  • 305,947
  • 44
  • 307
  • 483