-1

Sometimes there is no doubt, but sometimes it can be a very hard decision. Let me show you a couple of examples:

Inheritance:

public class Employee{
    public void goToWork(){
        //...
    }
}
public class Lawyer extends Employee{
    //private Employee body;
    /*public void goToWork(){
        body.goToWork(); // Does that make any sense?
    }*/
}

Delegation:

public class Lights{
    public void turnOn(){
        //...
    }
}
public class Car{ //public class Car extends Lights? Nice car then...
    private Lights light;
    public void turnOnLights(){
        light.turnOn();
    }
}

Which one?

public class Field{
public void grow(Seeds s){
        //...
    }
}
public class Plantation{ //extends Field{ makes sense too
    private Field field;
    private Worker[] workers;
    private Vehicle[] tractors;

    public void grow(Seeds s){
        //...
        field.grow(s);
    }
}

So, are there any tips that can help to choose between inheritance and delegation when both are acceptable and each one has their own advantages and disadvantages? Is the choice random and totally based on programmer's mood?

Aakash
  • 1,860
  • 19
  • 30
TomatoMato
  • 703
  • 2
  • 8
  • 19

3 Answers3

2

A plantation wouldn't extend Field, because (a) plantations have more on them than just a field, and (b) plantations would likely have multiple fields.

In this case a Plantation is not a field, because a field doesn't have its own tractors.

You might temporarily associate a field with a tractor, e.g., for scheduling purposes, but that tractor belongs to the over-arching container, the plantation.

Inheritance is generally pretty limited in scope, and only works when behavioral traits are shared fairly precisely. In this case the traits aren't inherently shared, and scheduling would be handled by a completely different mechanism.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
1

They do have different advantages and disadvantages. For example, you can only inherit one thing. What if you decide on inheritance (public class Plantation extends Field) and then decide that you really want your Plantation to have multiple Fields? What if you decide that multiple Plantations can share a single Field?

Composition (or delegation as you call it) is typically more future-safe, but often times inheritance can be very convenient or technically correct. "X inherits Y" implies that "X is a Y", so it can be useful in cases where that statement is technically true (for example, "a square is a rectangle").

Ultimately, it is a design decision that is left up to the person who is implementing the code. There is no "right" answer here.

Jon Newmuis
  • 25,722
  • 2
  • 45
  • 57
  • Funny you make that example... a square is not a rectangle in OOP. This even has its on wikipedia entry: http://en.wikipedia.org/wiki/Circle-ellipse_problem – Flavio Oct 23 '13 at 21:07
0

It depends really on decision making which you already know and it is : "does child have and require all (almost all) functionality as its parent?"

Compared to delegation : "Does the object include another object inside of its body."

You can also think this way - if I have list of fields, does it make sense to have fields and plantation in this list? If it does not, extend is not possible.

About this example - I would prefer Delegation, cause plantation is not just extended field, it is something using field and people and so on to make profit.

If you require to have same or similar methods (like grow) which works a little different, you should have interface that both classes implements.

libik
  • 22,239
  • 9
  • 44
  • 87