Subclass constructors - Why must the default constructor exist for subclass constructors?

后端 未结 3 514
青春惊慌失措
青春惊慌失措 2020-12-18 09:05

Given a random class:

public class A {
    public T t;

    public A () {}  // <-- why is this constructor necessary for B?
    public A (T t) {
         


        
3条回答
  •  心在旅途
    2020-12-18 09:43

    You may use your own constructor in A, but you have to call it explicitly from the B's constructor, e.g.:

    public B(Integer i) {
      super(i);
      ...
    }
    

    If you don't do that, the compiler will attempt to instantiate A itself, by calling its default constructor.

提交回复
热议问题