Java – how to enforce a method in a subclass without using abstraction?

I want to force subclasses to implement an implementation method of my mother class

public class myMotherClass { 

   myMethod {

      ...some code ..

   }

}

public class myClass extends myMotherClass {

   myMethod {

      ... other code ...
   }

}

So, in this example, I want to force MyClass to implement mymethod

Sorry, my English is not good

Solution

You cannot force a subclass to override a method You can only force it to implement a method through abstraction

So if you can't abstract mymotherclass, you can only introduce another superclass to extend mymotherclass and delegate it to the methods that must be implemented:

public abstract class EnforceImplementation extends myMotherClass {

        public final void myMethod(){
             implementMyMethod();
        }

        public abstract void implementMyMethod();
}

edit

I've found that another integration approach to solving problems in the hemrest API is, for example Used by mockito

public interface Matcher<T> extends SelfDescribing {

    /**
     * Evaluates the matcher for argument <var>item</var>.
     * <p/>
     * This method matches against Object,instead of the generic type T. This is
     * because the caller of the Matcher does not kNow at runtime what the type is
     * (because of type erasure with Java generics). It is down to the implementations
     * to check the correct type. 
     *
     * @param item the object against which the matcher is evaluated.
     * @return <code>true</code> if <var>item</var> matches,otherwise <code>false</code>.
     *
     * @see BaseMatcher
     */
    boolean matches(Object item);

    /**
     * This method simply acts a friendly reminder not to implement Matcher directly and
     * instead extend BaseMatcher. It's easy to ignore JavaDoc,but a bit harder to ignore
     * compile errors .
     *
     * @see Matcher for reasons why.
     * @see BaseMatcher
     */
    void _dont_implement_Matcher___instead_extend_BaseMatcher_();
}

This interface specifies a method_ dont_ implement_ Matcher___ instead_ extend_ BaseMatcher_. Of course, this does not prevent others from implementing the matcher interface, but it will guide developers in the right direction

The basematcher class implements_ dont_ implement_ Matcher___ instead_ extend_ BaseMatcher_ The method is final

public final void _dont_implement_Matcher___instead_extend_BaseMatcher_() {
    // See Matcher interface for an explanation of this method.
}

Finally, I think this is a design problem, because basematcher can implement the logic that each matcher should implement Therefore, it is better to make matcher an abstract class and use template method

But I guess they do, because it's the best compromise between bytecode compatibility and new features

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>