“Could not resolve matching constructor” error when passing in constructor-arg for child class

后端 未结 3 1953
别跟我提以往
别跟我提以往 2021-01-17 02:33

I have the following classes:

public abstract class ParentClass
{
    public ParentClass()
    {
        throw new RuntimeException(\"An ID must be specified         


        
3条回答
  •  走了就别回头了
    2021-01-17 03:23

    Short answer: Constructors are not inherited in Java.

    From the JLS:

    Constructor declarations are not members. They are never inherited and therefore are not subject to hiding or overriding. 
    

    This means you have to declare the constructors needed for each subclass and call the corresponding super constructor. Even if it has the same signature it doesn't count as overriding.

提交回复
热议问题