The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing with Interfaces

Advertisement

Advertisement

This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

C++ mixin v/s java interfaces

Posted by Salil on January 11, 2001 at 7:00 PM

Interfaces are similar to Mixin's in C++. Mixin is a class that does not implement any method but are in fact has only pure virtual methods. The implementor decides the logic for these methods.

Also, The article mentions advantage of interface over single inheritence but The Animal class could always be mixed in with a Talkative class in which case the result would have been the same as interace. e.g.

In C++ (or Java)...

class Talkative {
public:
virtual void talk() = 0;
};

class Animal : public Talkative {
public:
//some other methods
];

class Dog : public Animal {
public:
void talk() { cout << "Woof!" << endl; }
};

class CuckooClock : public Talkative {
public:
void talk() { cout << "Cuckoo, Cuckoo!" << endl; }
};

class Interrogator {
public:
void makeItTalk(const Talkative& subject) { subject.talk(); }
};

Also, I don't understand how interfaces are any better than doing multiple inheritance since interfaces could cause code repeatability. e.g.

In Java...

public class Context {
public void write(String str) {
data.append(str);
}
public String getData() {
return data.toString();
}
private StringBuffer buff = new StringBuffer();
}

public class Board {
public void write(String str) {
context.write(str);
}
protected Context context;
}

public abstract class ColorBoard extends Board {
// Some other methods
public abstract String getColor();
}

public class WhiteBoard extends ColorBoard {
public String getColor() {
return new String("White");
}
}

public class PrintableBoard extends Board (
public void print(Context context) {
Syatem.out.println(context.getData());
}
}

What if I now want to write a class for PrintableWhiteBoard ?

public class PrintableWhiteBoard extends WhiteBoard implements Printable {
// This method was provided by PrintableBoard but ALAS!
public void print() {
System.out.println(context.getData());
}
}

I agree thar an inheritence involving a diamond is complicated but it works fine if you have a good insight into the language.

I'm a huge java fan but I guess that both languages have their up sides and down sides.

Thanks,





Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us