Если MotherClass
имеет видимость пакета, а ChildClass1
и ChildClass2
являются общедоступными и находятся в одном пакете, вы можете создать подкласс этих двух, но не MotherClass.
Редактировать:
Другойвозможность:
interface Marker {} //note that this is package private
public abstract class Mother<T extends Marker > {}
public class ChildA extends Mother<ChildA> implements Marker {}
public class ChildB extends Mother<ChildB> implements Marker {}
Метод:
doSomething(Mother<?> mother() {}
Теперь вы не можете сделать
class GrandChild extends Mother<GrandChild> {}
Это скомпилируется, но вы получите как минимум предупреждение:
class GrandChild extends Mother {} //warning like "Mother is a raw type"
Способы без предупреждения:
class GrandChild extends ChildA {}
class GrandChild extends ChildB {}