Is there a way to make a method that is not abstract but needs to be redefined?

Is there a way to force child classes to override a non-abstract superclass method?

I need to be able to instantiate the parent class, but if the class extends this class, it must give its definition of some methods.

+52
java override inheritance abstract-class abstract-methods
Oct 20 '11 at 7:21
source share
12 answers

This, as I know, is not a direct method capable of executing the compiler.

You can work around this without creating a parent class, but instead providing a factory method that creates an instance of some (possible private) subclass that has a default implementation:

public abstract class Base { public static Base create() { return new DefaultBase(); } public abstract void frobnicate(); static class DefaultBase extends Base { public void frobnicate() { // default frobnication implementation } } } 

Now you cannot write new Base() , but you can do Base.create() to get the default implementation.

+37
Oct 20 '11 at 7:25
source share

As others have pointed out, you cannot do this directly.

But one way to do this is to use the Strategy template, for example:

 public class Base { private final Strategy impl; // Public factory method uses DefaultStrategy // You could also use a public constructor here, but then subclasses would // be able to use that public constructor instead of the protected one public static Base newInstance() { return new Base(new DefaultStrategy()); } // Subclasses must provide a Strategy implementation protected Base(Strategy impl) { this.impl = impl; } // Method is final: subclasses can "override" by providing a different // implementation of the Strategy interface public final void foo() { impl.foo(); } // A subclass must provide an object that implements this interface public interface Strategy { void foo(); } // This implementation is private, so subclasses cannot access it // It could also be made protected if you prefer private static DefaultStrategy implements Strategy { @Override public void foo() { // Default foo() implementation goes here } } } 
+23
Oct 20 2018-11-11T00:
source share

Consider creating an interface with this method. The descendants of the class will have to implement it.

+6
Oct 20 '11 at 7:26 a.m.
source share

I think the easiest way is to create an abstract class that inherits from the base class:

 public class Base { public void foo() { // original method } } abstract class BaseToInheritFrom extends Base { @Override public abstract void foo(); } class RealBaseImpl extends BaseToInheritFrom { @Override public void foo() { // real impl } } 
+5
Feb 04 '12 at 16:20
source share

No, that is the whole point of the abstract method. What is your use case? Perhaps we can think about it based on basic needs.

+3
Oct 20 '11 at 7:25
source share

How about this: inside the default implementation of the method, use reflection to get the exact class of the object. If the class does not match your base class, throw a RuntimeException or its equivalent.

 public class Parent { public void defaultImpl(){ if(this.getClass() != Parent.class){ throw new RuntimeException(); } } } 
+3
Oct 20 2018-11-11T00:
source share

There a reason is not possible!

A derived class can simply invoke an implementation of the base class when overriding a method.

So, what makes a class override your method? I do not think there is any benefit.

+3
Oct 20 '11 at 17:50
source share

The answer will be no. You can reverse engineer a template template template. It might help you.

Alternatively, you can make your child classes an implemented interface. An interface may or may not be implemented by a superclass.

+2
Oct 20 '11 at 7:27
source share

You can always make the base class a method that throws an exception.

Technically, the base class defined this method, but it cannot be used without overriding the method. In this case, I prefer a run-time exception, because it does not require an explicit throw command. Example follows

 public class Parent { public void doStuff() { throw new RuntimeException("doStuff() must be overridden"); } } public class Child extends Parent { @Override public void doStuff() { ... all is well here ... } } 

The disadvantage is that this does not interfere with the creation of Base objects; however, anyone who tries to use one of the "must be overridden" methods will soon find that they should override the class.

While this solution satisfies the description of the request, your application is likely to benefit from a non-requiring solution like this. This is much better to avoid run-time crashes with compiler checks, which provides an abstract keyword.

+1
Oct. 20 2018-11-11T00:
source share

I will reflect other answers and say that there is no forced way in which derived classes can override a non-abstract method. The whole point of creating an abstract method is to determine that a method with this signature must exist, but cannot be specified at a basic level and therefore must be specified at a derived level. If there is a working, non-trivial implementation (since it is not empty and does not just throw an exception or show a message) of the method at the basic level, then this is not strictly necessary in order to call the method from the consumer of the derived class for success. Thus, the compiler does not need to forcefully cancel a method that can successfully execute at the base or derived levels.

In situations where you want a derived class to override your working implementation, it should be pretty obvious that the base implementation does not do what the consumers of the derived class want; the base class either does not have sufficient implementation or is incorrect. In such cases, you must trust that the programmer receiving your class will know what it is doing, and thereby know that the method needs to be redefined, because it does not give the correct answer in the context of using its new object.

I can think of one thing you could do. This will require an abstract base with a sealed (final for Javaheads) default implementation. Thus, there is a basic implementation of the method that is easily accessible for use as if it were a “base” class, but in order to define another class for the new script, you must return to the abstract class and therefore have to redefine the method. This method may be the only abstract thing in the class, thereby allowing you to use the basic implementations of other methods:

 public abstract class BaseClass { public abstract void MethodYouMustAlwaysOverride(); public virtual void MethodWithBasicImplementation() { ... } } public final class DefaultClass:BaseClass { public override void MethodYouMustAlwaysOverride() { ... } //the base implementation of MethodWithBasicImplementation //doesn't have to be overridden } ... public class DerivedClass:BaseClass { //Because DefaultClass is final, we must go back to BaseClass, //which means we must reimplement the abstract method public override void MethodYouMustAlwaysOverride() { ... } //again, we can still use MethodWithBasicImplementation, //or we can extend/override it public override void MethodWithBasicImplementation() { ... } } 

However, this has two drawbacks. First, since you do not have access to the DefaultClass implementation through inheritance, you cannot extend the DefaultClass implementation, which means that to accomplish what DefaultClass does, plus a little more, you must rewrite the code from DefaultClass, violating DRY. Secondly, this only works for one level of inheritance, because you cannot force override if you allow inheritance from DerivedClass.

+1
Oct 20 2018-11-11T00:
source share

perhaps this helps:

 class SuperClass { void doStuff(){ if(!this.getClass().equals(SuperClass.class)){ throw new RuntimeException("Child class must implement doStuff Method"); }else{ //ok //default implementation } } } class Child extends SuperClass{ @Override void doStuff() { //ok } } class Child2 extends SuperClass{ } new SuperClass().doStuff(); //ok new Child().doStuff(); //ok new Child2().doStuff(); //error 
+1
Oct. 20 '11 at 16:02
source share

OK, let's learn it like this. I adhere to java style rules and use Java syntax. Thus, it is assumed that multiple inheritances and C ++ templates are not available.

creating an abstract method of the parent class method is optional; in OOP you use the concept of polymorphism. You can use the same method in two or more ways. This is called method overriding.

take an example.

  public class Animal{ public void makeSound(){ System.out.println("Animal doesn't know how to make sound"); } } public class PussyCat extends Animal{ public void makeSound(){ System.out.println("meowwww !!!"); } public static void main(String args[]){ PussyCat aCat=new PussyCat(); aCat.makeSound(); } } 

it will print "meowww !!!" on the screen.

but this does not mean that the makeSound method must be overridden in a child class.

If you need a child class to be forced to override methods, it is better to implement an interface for this class.

  public Interface audible{ public void makeSound(); } public class pussyCat implements audible{ // now you must implement the body of makeSound method here public void makeSound(){ System.out.println("meowwww !!!"); } public static void main(String args[]){ PussyCat aCat=new PussyCat(); aCat.makeSound(); } } 

it will also print "meowwww !!!" on the screen

0
Oct 20 '11 at 9:20
source share



All Articles