Java lacks multiple inheritance of implementation because it’s not necessary?

spoon

Java itself isn’t ‘necessary’. But it is useful.

Is multiple inheritance (of implementation) dangerous? Well, even a spoon can be dangerous in the hands of a moron.

How about “Because there is a better way without multiple inheritance.” – spoken absolutely?

Lets look at this example of the observer pattern which I’ve roughly taken from an old Bob Martin document…
We have a Clock which is a self contained class which understands the way in which time is measured and represented. It ticks every second. We’d like to keep it self-contained and with a single responsibility.

We also want to implement an Observer pattern, whereby objects can register themselves as Observers of the clock so that they receive a notification whenever the clock updates it’s internal representation of the time (we don’t want to continuously poll the clock, wasting cpu cycles, when the time will only ever be updated once every second).

Given that Java doesn’t support multiple inheritance of implementations (ignoring some blurred lines introduced by Java 8 just recently), an implementation of the Observer pattern would have to look roughly like this:

public class Clock {

   public void tick() {
      // Update time each tick.
   }
}

interface Observer {
   public void update();
}

interface Subject {
   public void notifyObservers();
   public void registerObserver(Observer observer);
}

class SubjectImpl implements Subject {

   List<Observer> observers = new ArrayList<>();

   @Override
   public void notifyObservers() {
      for(Observer observer : observers) {
         observer.update();
      }
   }

   @Override
   public void registerObserver(Observer observer) {
      this.observers.add(observer);
   }
}

class ObservedClock extends Clock implements Subject {

   private Subject subjectImpl = new SubjectImpl();

   @Override
   public void tick() {
      super.tick();
      notifyObservers();
   }

   @Override
   public void notifyObservers() {
      subjectImpl.notifyObservers();
   }

   @Override
   public void registerObserver(Observer observer) {
      subjectImpl.registerObserver(observer);
   }
}

 

Now compare the above code with the implementation using a hypothetical Java which supported multiple inheritance of implementations:


class MIObservedClock extends Clock, Subject {

   @Override
   public void tick() {
      super.tick();
      notifyObservers();
   }
}

class Clock {

   public void tick() {
      // Update time each tick.
   }
}

interface Observer {
   public void update();
}

class Subject {

   List<Observer> observers = new ArrayList<>();

   public void notifyObservers() {
      for(Observer observer : observers) {
         observer.update();
      }
   }

   public void registerObserver(Observer observer) {
      this.observers.add(observer);
   }
}

 

Now… did writing that code unleash any zombie apocalypse? Which version is cleaner? Which is more elegant? Which is more decoupled? Which is ‘better’ ?

I’ve heard ‘Uncle’ Bob refer to Java interfaces as ‘a hack’, made for the sake of simplicity – but not for us. For the sake of the JVM implementation. I don’t know, but it’s probably true.

 

TL;DR?

Java interfaces are dumb.

This entry was posted in coding, java, software, Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s