java - Why can you not inherit from a class whose constructor is private? -
why java disallow inheritance class constructor private?
java doesn't prevent sub-classing of class private constructors.
public class main { static class { private a() { system.out.println("subclassed in "+getclass().getname()); } } static class b extends { public b() { } } public static void main(string... ignored) { new b(); } }
prints
subclassed in main$b
what prevents sub-classes cannot access constructors of super class. means private constructor cannot used in class file, , package local constructor cannot used in package.
in situation, option have delegation. need call factory method create instance of "super" class , wrap it.
Comments
Post a Comment