Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Difference between inheritance and delegation in java [duplicate]

Tags:

java

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

   }
like image 357
Shilendra Sharma Avatar asked Nov 06 '12 06:11

Shilendra Sharma


People also ask

What is the difference between inheritance and delegation?

Delegation is an alternative to inheritance for reusing code among multiple classes. Inheritance uses the IS-A relationship for re-use; delegation uses the HAS-A reference relationship to do the same. Inheritance and delegation have the same kind of relationship that, say, Aspirin and Tylenol, have.

What is delegate in Java?

Delegation is simply passing a duty off to someone/something else. Delegation can be an alternative to inheritance. Delegation means that you use an object of another class as an instance variable, and forward messages to the instance.

What is delegation in oops?

In object-oriented programming, delegation refers to evaluating a member (property or method) of one object (the receiver) in the context of another original object (the sender).

Does delegation promote code reuse?

Delegation is a technique that promotes code reuse by allowing runtime function invocation in the context of a specific instance – regardless of the hierarchical lineage of instance and function.


1 Answers

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();
    }
}
like image 98
npinti Avatar answered Sep 16 '22 17:09

npinti