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();
     }
   }
 
     
     
     
    