Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call super class method automatically

Consider the following class

class A{
    public void init(){
        //do this first;
    }
    public void atEnd(){
        //do this after init of base class ends
    }
}

class B1 extends A{

    @Override
    public void init()
    {
        super.init();
        //do new stuff.
        //I do not want to call atEnd() method here...
    }
}

I have several B1, B2,... Bn child classes which are already developed. All of them extend class A. If I want to add a new functionality in all of them, the best place to do so is define that in a method within class A. But the condition is that the method should always get called automatically just before the init() method of child class ends. One basic way to do so is to again add atEnd() method call at end of init() method of child classes. But is there any other way to do this smartly ??

like image 924
V_M Avatar asked May 17 '12 11:05

V_M


People also ask

How to call a method on a superclass in Java?

This concept is used when we want to call super class method. So whenever a base and subclass have same named methods then to resolve ambiguity we use super keyword to call base class method. The keyword “super” came into this with the concept of Inheritance. Below is the example of call a method on a superclass.

How to call base class from derived class using super() method?

The following example demonstrates how the derived class can call base class using the super () method. In the above example, super ().__init__ (firstname, lastname) in the init method of the student class call's the base class person's init method and pass parameters. In the same way, super ().fullname () calls person.fullname () method.

What are the rules for calling the superclass constructor C++?

What are the rules for calling the superclass constructor C++? In C++, we can derive some classes. Sometimes we need to call the super class (Base class) constructor when calling the constructor of the derived class. Unlike Java there is no reference variable for super class.

Why call Super() method at the end of the constructor?

Note, that super is a reference to the parent class, but super () is its constructor. Some call super method at the end or some at the start why? In constructor, you need to call super () as a first statement (if you call it explicitly). On regular methods you call it wherever you want depending on your app logic needs.


3 Answers

One way to do this is by making init() final and delegating its operation to a second, overridable, method:

abstract class A {
  public final void init() {
    // insert prologue here
    initImpl();
    // insert epilogue here
  }
  protected abstract void initImpl();
}

class B extends A {
  protected void initImpl() {
    // ...
  }
}

Whenever anyone calls init(), the prologue and epilogue are executed automatically, and the derived classes don't have to do a thing.

like image 190
NPE Avatar answered Oct 10 '22 00:10

NPE


Another thought would be to weave in an aspect. Add before and after advice to a pointcut.

like image 31
duffymo Avatar answered Oct 10 '22 02:10

duffymo


Make init() final, and provide a separate method for people to override that init() calls in the middle:

class A{
    public final void init(){
        //do this first;
    }

    protected void initCore() { }

    public void atEnd(){
        //do this after init of base class ends
    }
}

class B1 extends A{

    @Override
    protected void initCore()
    {
        //do new stuff.
    }
}
like image 25
SLaks Avatar answered Oct 10 '22 02:10

SLaks