简体   繁体   中英

Require override of specific methods of a non-abstract class

Is it possible to have a class defined like

public class MyClass
{
    public void methodA(){} // Inherit
    public void methodB(){} // Inherit
    public void methodC(){} // Require override
}

and then have all classes which extend from MyClass to be required to override methodC() but just simply inherit methodA() and methodB() ?

If it is possible, how does one do it? If it's not possible, can you propose an alternative solution to achieve a similar result?

EDIT:

I need a non-abstract class because I want to be able to instantiate this class too.

You would have to make your base class abstract.

public abstract class MyClass
{
    public void methodA(){} // Inherit
    public void methodB(){} // Inherit
    public abstract void methodC(); // Require override
}

You cannot require an override of a non-abstract method.

Maybe you can do something similar to the template method pattern :

 public final void methodC() { methodC1(); someMoreLogic(); methodC2();}

 protected abstract void methodC1();

 protected abstract void methodC2();

Here methodC encapsulates a fixed algorithm that calls into pieces that have to be supplied by the subclasses.

I don't think you do exactly what you want. Alternatively, create MyBaseClass as an abstract class with methodC() abstract implementations for methodA() and methodB(). Derive MyClass from it, adding an implementation for methodC() . Any classes that you do not want to have inherit that implementation should directly subclass MyBaseClass rather than MyClass.

If you want a method to be just inherited use final keyword. To force overriding make it abstract . However, only non-abstract child classes will have to override it.

AFAIK there is no way to force override a method in Java with out abstract.

You can achive with abstract class by making the method as abstract method.

one way to do it may be to use virtual keyword instead of abstract but it doesnt require to override, but you can do override. though so, you can instantiate.

another way and more recommended is to create an interface where you can indicate what the class requirements. it is something like abstract, you indicate what to require, but keep in mind interface is not a class. it's more like pointer. for more for interface click: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html

So, java.lang.Thread is a concrete class that implements the Runnable interface. Therefore, it must be that Thread implements a public void run() method. Yet, when you extend Thread, like MyClass extends Thread { }, you are required to implement the run() method.

It stands to reason that there is a way to force overriding of a method without making the containing class abstract.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM