javaskeleton-code

What is the proper style for implementing a skeleton class?


What is the proper style for implementing a skeleton class in Java?

By skeleton I mean a class that provides a skeleton utility code, but needs a subclass to implement some of the parts.

In C++ I would just add pure virtual methods into the class, but Java has a strict distinction between interfaces and classes, so this is not an option.

Basically the patter should be something like this:

class Skel { 
  void body() {
    this.action1();
    this.action2();
  }
};

class UserImpl : extends A {
   void action1() {
      impl;
   }

   void action2() {
      impl;
   }
}

/* ... snip ... */

Skel inst = new UserImpl();

/* ... snip ... */

inst.body();

Solution

  • Abstract methods in Java are similar to virtual methods in C++.

    You need to declare an abstract class Skel with abstract methods, and implement them in the subclass:

    public abstract class Skel {
    
        public void body() {
            this.action1();
            this.action2();
        }
    
        abstract void action1();
        abstract void action2();
    
    }