views:

674

answers:

6

Today I got my book "Head First Design Patterns" in the mail. Pretty interesting stuff so far, however I do have a question about it's contents.

I have no Java/C# background nor do I wish to jump into those languages right now (I'm trying to focus on C++ first). In the book is said that java does not have an implementation for interfaces... This would mean that for every change to that interface, you would have to modify all subclasses that implement the interface.

How is this done in C++? What am I missing?

+8  A: 

What the author of the book meant, if you change the signatures of the members of the interface or add new ones, you will need to make those changes in the implementing classes as well so that they keep implementing the interface.

You can change the implementing classes whatever way you want as long you have the members of the interface implemented with exactly the same signatures (that is, with the same name, return type, and the order and type of parameters).

I have the impression that you don't quite understand how interfaces work, so I suggest reading up the C# interface specification on MSDN which is quite clear on the subject I think (and it's pretty much the same in Java except that in Java you use the "implements" keyword instead of a colon (:) to declare that a class implements a specific interface).

DrJokepu
+1  A: 

The C++ equivalent of a Java interface would be a class with only pure virtual methods.

You mentioned that you've "seen plenty of these Runnable/Threadable interfaces that can be modified without touching those subclasses." It's hard to say for sure from your description, but what you might have seen before is something like this:

Runnable r = new Runnable() {
    public void run() {
        // do something here
    }
};

This is an anonymous class implementing the Runnable interface; it is not modifying the interface in any way.

Michael Myers
+2  A: 

... if you want to change the signature of a method in an interface, a good IDE (eclipse, netbeans...) can help you with refactoring all the classes implementing this interface.

Pierre
+1  A: 

It's the same situation in C++.

C++ doesn't have an interface keyword, but a pure virtual class, with all pure virtual methods, is the same idea.

If you changed method signatures in the pure virtual class, all its subclasses must follow suit in order to compile.

duffymo
A: 

An interface defines a contract that the class implementing the interface is expected to follow. This is akin to an abstract base class - which basically delegates the implementation of the methods to the deriving class.

Yes, if you modify the interface - you would have to modify all the classes that implement that interface. My guess is that the instances of Runnable that you would have seen in Java code are anonymous classes which actually implement Runnable rather than modify the interface itself.

Runnable myOwnThread = new Runnable() {
  public void run() {
   // Do something here
  }
};

In Java, you are not allowed to modify the interfaces (and classes) provided in the standard library. The equivalent in C++ could be the pure virtual class.

Anirudh
A: 

Ok I'll askt he obvious question:

If you want to do design patterns in C++ why would you read anything other than the original Gang of Four Design Patterns book?

And yes a C++ class with pure virtual methods is the equivalent to a Java or C# interface.

cletus
Because the GoF book is sometimes needlessly academic and dense. Head First is a very accessible entry point into thinking in patterns.
Kylar