1

I want to throw a custom exception if override method does not call its super method explicitly. if any class extends class A and override say() method, should call super.say() forcefully to execute code written in base class method , if fails, I want to throw custom exception like super not exception

  class A {
    public void say() {
        System.out.println("From super");
    }
  }   

  class Demo extends A {
    @Override
    public void say() {
              //Check if user have called super.say() or not if not throw a custom  exception
       super.say();
       System.out.println("From Child");
    }

    public static void main(String[] args) {
      Demo d = new Demo();
      d.say();
    }

 }
Rajeev Ranjan
  • 4,152
  • 3
  • 28
  • 41
Jitendra Prajapati
  • 1,002
  • 1
  • 10
  • 33

2 Answers2

0

Here's the approach suggested by @DavidWallace, and that is better than what you would like, since it makes it impossible for a subclass to prevent say() from doing what it does in the base class:

class A {
    public final void say() {
        System.out.println("From super");
        saySomethingElse();
    }

    /**
     * Subclasses can override this method to make the say() method 
     * say something else in addition to "From super".
     */
    protected void saySomethingElse() {
    }
}   

class Demo extends A {
    @Override
    protected void saySomethingElse() {
        System.out.println("From Child");
    }

    public static void main(String[] args) {
        Demo d = new Demo();
        d.say();
    }
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for your answer & suggestion but in android if user fails to call super in override method of sub class of activity, it throws exception super not called exception. then why we can't do same implementation ? how this senario is implemented in android Activity class ? – Jitendra Prajapati Jan 10 '16 at 08:57
  • I'm not familiar with Android, but I guess it does that by calling another method, later, and checking that the super method has been called before (the super method could set a boolean flag, for example, that would be checked later). – JB Nizet Jan 10 '16 at 09:00
0

This is quite simply not possible to do reliably at runtime using plain Java. When a method is overridden the new version completely overrides the old version. So, when the method is called control goes to the subclass version and not the parent class version. It's totally at the discretion of the overriding method whether it calls the superclass method or not.

There are three main options for doing what you want to do

  • If you have compile-time access to the subclasses which which are overriding this method then you can use a static analysis tool such as Checkstyle or Findbugs to ensure that the method calls super(). Findbugs already has such a rule for the clone() method, for example. You could probably implement a similar rule for your own method.

  • Redesign your code, as suggested by @DavidWallace. The fundamental problem you're facing is that you're allowing control to flow to the subclass while you want to ensure something happens in your superclass. If you redesign your code so that the say() method is final in the superclass, then it can call an abstract method on the superclass (called e.g. sayInternal) which each subclass implements. Then you can be sure that when users call say() they first end up in the superclass method, and the superclass is fully in control of how the subclass method is used.

  • You can use a runtime agent to inspect classes as they're loaded. That could verify that the superclass method is called and refuse to load classes which don't call it.

sisyphus
  • 6,174
  • 1
  • 25
  • 35
  • Thanks for your answer & suggestion but in android if user fails to call super in override method of sub class of activity, it throws exception super not called exception. then why we can't do same implementation ? how this senario is implemented in android Activity class ? – Jitendra Prajapati Jan 10 '16 at 08:49
  • There are many things which are different in Android. Where Activity is concerned you've got a whole framework built around it - your subclass is being plugged into a framework which is ensuring that methods are called. I'm not familiar with how Activity is implemented but I imagine it's not the class which is ensuring that `super.onCreate()` and friends are called. – sisyphus Jan 10 '16 at 11:01