How to create instance of subclass with constructor from super class

后端 未结 4 2158
时光取名叫无心
时光取名叫无心 2021-02-19 00:51

I\'d like to create a registry for classes which are subclasses of a super class. The classes are stored in a map which acts as registry. A class is picked from the registry dep

4条回答
  •  情歌与酒
    2021-02-19 01:23

    Constructors need to be defined explicitly in the subclass. If a constructor is defined in superclass that doesn't mean that constructor can be used to create an instance of subclass whether you are using reflection or not.

    Since your SubClass2 doesn't have constructor with one argument, so when you try to create an instance of it with one argument, it's throwing NoSuchMethodException.

提交回复
热议问题