blue-sky
blue-sky

Reputation: 53786

Using composition over inheritance when overriding

If I have a class that I would like to customise by overriding one if its methods the only I can do this is by using inheritance (sub-classing and overriding method) ? Is it possible to use composition in some way to achieve same goal ?

Upvotes: 1

Views: 1093

Answers (3)

AlexR
AlexR

Reputation: 115328

Sure. You can use the following patterns.

Simple overriding of method method

Template method pattern

class Base {
    public void foo() {
        // do something
        bar();
        // do something
    }
    protected abstract void bar();
}

class Child {
    protected void bar() {
        // do something.
    }
}

Delegation

class Base {
    private Runnable r;
    protected Base(Runnable r) {
        this.r = r;
    }

    public void foo() {
        r.run();
    }
}

class Child extends Base {
    Child() {
        super(new Runnable() { /* implementation */})
    }
}

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691625

Using composition instead of inheritance is a design choice.

Either your class has been designed with inheritance in mind (i.e. it provides non-final public and protected methods, intended to be overridden by subclasses), or it has been designed with composition or delegation in mind (by using the strategy pattern, for example).

If the class has not been designed for customization at all, you might be able to customize it using inheritance, but not using a composition/delegation mechanism.

Upvotes: 3

Jeff Foster
Jeff Foster

Reputation: 44696

Yes, you can use delegation. Instead of deriving from Foo in the example below, Bar contains a Foo and delegates to it where it chooses.

interface SomeMethods {
  void doSomething();
  void doSomethingElse();
}

class Foo implements SomeMethod {
  public void doSomething() { // implementation }
  public void doSomethingElse() { // implementation }
}

class Bar implements SomeMethod {
   private final Foo foo = new Foo();

   public void doSomething() { foo.doSomething(); }
   public void doSomethingElse() { // do something else! }
}

Upvotes: 4

Related Questions