13

Possible Duplicate:
Prefer composition over inheritance?

What is the difference between inheritance and delegation in java?

How to use the following example in my project? Please can you guide me with delegation. I know about inheritance but do not have much knowledge about delegation. So, please give a proper reason. Why should I use this?

 package com.m;

 class RealPrinter { // the "delegate"
     void print() { 
      System.out.println("something"); 
    }
 }

 class Printer { // the "delegator"
     RealPrinter p = new RealPrinter(); // create the delegate 
     void print() { 
     p.print(); // delegation
     } 
 }

 public class Tester {

// to the outside world it looks like Printer actually prints.
     public static void main(String[] args) {
        Printer printer = new Printer();
        printer.print();
     }

   }
Community
  • 1
  • 1
Shilendra Sharma
  • 173
  • 2
  • 2
  • 7
  • See e.g. http://stackoverflow.com/questions/832536/when-to-use-delegation-instead-of-inheritance or search the internet for 'inheritence delegation'... – home Nov 06 '12 at 06:33
  • 2
    Or better this: http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance – home Nov 06 '12 at 06:34

2 Answers2

26

When you delegate, you are simply calling up some class which knows what must be done. You do not really care how it does it, all you care about is that the class you are calling knows what needs doing.

If I were you though I would make an interface and name it IPrinter (or something along those lines) which has one method named print. I would then make RealPrinter implement this interface. Finally, I would change this line: RealPrinter p = new RealPrinter(); to this: IPrinter p = new RealPrinter().

Since RealPrinter implements IPrinter, then I know for sure that it has a print method. I can then use this method to change the printing behaviour of my application by delegating it to the appropriate class.

This usually allows for more flexibility since you do not embed the behaviour in your specific class, but rather leave it to another class.

In this case, to change the behaviour of your application with regards to printing, you just need to create another class which implements IPrinter and then change this line: IPrinter p = new RealPrinter(); to IPrinter p = new MyOtherPrinter();.

public interface IPrinter {
    void print();
}

public class RealPrinter implements IPrinter {

    @Override
    public void print() {
        System.out.println("This is RealPrinter");
    }
}

public class RealPrinter2 implements IPrinter {

    @Override
    public void print() {
        System.out.println("This is RealPrinter2");
    }
}

public class Main {

    public static void main(String...arg){
        IPrinter printer  = new RealPrinter();
        printer.print();

        printer = new RealPrinter2();
        printer.print();
    }
}
AZ_
  • 21,688
  • 25
  • 143
  • 191
npinti
  • 51,780
  • 5
  • 72
  • 96
6

Inheritence uses the java compiler's type system to implicitly include code from elsewhere, with delegation you explicitly implement the callout.

Inheritance is limited to a single parent class (and ancestors) but it can sometimes be non-obvious where your code is going; delegation is less elegant, but you can bring in functionality from multiple other classes and the flow is clearly visible.

As far as invoking external functions is concerned, delegation is generally preferred for just this reason, inheritance should be reserved for cases where you want to implement a class whose behaviour specifically is like the base-class, so it can essentially be treated as an instance of the baseclass by other code (ie. it is polymorphic).

robert
  • 4,612
  • 2
  • 29
  • 39