Red > Green > Refactor > Red

cycle is based on desire

Java constructor

src
 |
 + -- jp.ymatsukawa
           |
           + -- constructor
           |         |
           |         + -- Super.java
           |         + -- Sub.java
           + -- Main.java           

サブクラスをインスタンス化 > 親クラスのコンストラクタが呼ばれる

Main.java

package jp.ymatsukawa;

import jp.ymatsukawa.constructor.Sub;

public class Main {
  public static void main(String[] args) {
    Sub sub = new Sub();
  }
}

System out

Super constructor
Sub constructor

construcotr/Super.java

package jp.ymatsukawa.constructor;

public class Super {
  Super() {
    System.out.println("Super constructor");
  }
}

constructor/Sub.java

package jp.ymatsukawa.constructor;

public class Sub extends Super {
  public Sub() {
    System.out.println("Sub constructor");
  }
}

サブクラスのインスタンス生成を抑制

constructor/Super.java

package jp.ymatsukawa.constructor;

public class Super {
  private Super() {
    System.out.println("Super constructor");
  }
}
package jp.ymatsukawa.constructor;

public class Sub extends Super {
  public Sub() {
    System.out.println("Sub constructor");
  }
}

Compile Error

There is no default constructor available in `jp.ymatsukawa.constructor.Super`