title: What is the Difference between inheritance and delegation in java
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance">Prefer composition over inheritance?</a>  </p>
  </blockquote>
  
  
  
  <p>What is the difference between inheritance and delegation in java?</p>
  
  <p>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?</p>
  
  <pre><code> 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();
       }
  
     }
  </code></pre>