Avoid casting in inherited java-classes

前端 未结 7 1958
执念已碎
执念已碎 2021-02-14 17:56

I have a class:

class MyClass {

  public MyClass getParent() { ... }

  public MyClass[] getChildren() { ... }

  ....

}

and a subclass

7条回答
  •  逝去的感伤
    2021-02-14 18:40

    Add the method getId to the super class as well ie. MyClass.

    Any subclasses can still override and have their own implementation. If you are scared of subclasses not overriding getId,then throw an exception from the super class from getId.

    Th erotically, getID is some thing that is applicable for All Class and hence can be added to super.

提交回复
热议问题