java force an extending class

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6028526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 14:02:16  来源:igfitidea点击:

java force an extending class

javaconstructorabstract-class

提问by Marthin

In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter?

In Java, can I somehow force a class that extends an abstract class to implement its constructor with a Object as a parameter?

Something like

Something like

public abstract class Points {

    //add some abstract method to force constructor to have object.
}

public class ExtendPoints extends Points {

    /**
     * I want the abstract class to force this implementation to have
     *  a constructor with an object in it?
     * @param o
     */
    public ExtendPoints(Object o){

    }
}

回答by Sean Patrick Floyd

You can use a constructor with a parameter in your abstract class (make it protected if you want to dis-allow anonymous subclasses).

You can use a constructor with a parameter in your abstract class (make it protected if you want to dis-allow anonymous subclasses).

public abstract class Points{
    protected Points(Something parameter){
        // do something with parameter
    }
}

Doing that, you force the implementing class to have an explicit constructor, as it must call the super constructor with one parameter.

Doing that, you force the implementing class to have an explicit constructor, as it must call the super constructor with one parameter.

However, you cannot force the overriding class to have a constructor with parameters. It can always fake the parameter like this:

However, you cannot force the overriding class to have a constructor with parameters. It can always fake the parameter like this:

public class ExtendPoints extends Points{
    public ExtendPoints(){
        super(something);
    }
}

回答by BertNase

As said by others before, the signatue of Constructors cvannot be enforced, but you could enforce a particular set of arguments by using the AbstractFactorypattern instead. Then you can define the create methods of your factory interface to have a particular signature.

As said by others before, the signatue of Constructors cvannot be enforced, but you could enforce a particular set of arguments by using the AbstractFactorypattern instead. Then you can define the create methods of your factory interface to have a particular signature.

回答by planetjones

No Constructors aren't inherited, so each Class needs to provide its own, unless you don't specify a constructor and get the default no args constructor.

No Constructors aren't inherited, so each Class needs to provide its own, unless you don't specify a constructor and get the default no args constructor.

回答by user85421

Probably there it's not possible at compile time, but you can use reflection to check at run time if the desired constructor was declared:

Probably there it's not possible at compile time, but you can use reflection to check at run time if the desired constructor was declared:

public abstract class Points {

    protected Points() {
        try {

            Constructor<? extends Points> constructor = 
                getClass().getDeclaredConstructor(Object.class);
            if (!Modifier.isPublic(constructor.getModifiers()))
                throw new NoSuchMethodError("constructor not public");

        } catch (SecurityException ex) {
            throw new RuntimeException(ex);
        } catch (NoSuchMethodException ex) {
            throw (NoSuchMethodError) new NoSuchMethodError().initCause(ex);
        }
    }
}

回答by Tom Jefferys

If you add a public Points(Object o) {}constructor to Points, you force any subclass constructors to call that super constructor. However I don't think there's no way of ensuring that subclasses use that exact constructor signature.

If you add a public Points(Object o) {}constructor to Points, you force any subclass constructors to call that super constructor. However I don't think there's no way of ensuring that subclasses use that exact constructor signature.

回答by morja

EDIT

EDIT

Well, no, its not possible to force the implementation of a constructor with argument.

Well, no, its not possible to force the implementation of a constructor with argument.