D - Interface NameClash
- Luke D (62/62) Mar 20 2004 Sorry about mentioning this twice, but no one but Matthew seemed to resp...
Sorry about mentioning this twice, but no one but Matthew seemed to respond to it, and I never really got a very definitive response. The problem is basicly that when you have 2 interfaces with methods with the same name, you can only provide a single implementation even if the methods are meant to have different from 2 interfaces called IAmericanMeasurement and IMetricMeasurement that both have methods called getLength(). The class provided implementations for both of the interface's methods so you could get either the length in inches or in centimeters simply by specifying the interface you want to use. It's not really the best example, but it at least demonstrates a use of it. Anyway, not being able to do this in D seems like a pretty large restriction. When you're working with 2 interfaces that you didn't make, you have no way of preventing nameclashes, and I think it's better to be forced to use a special syntax to implement and call the 2 methods than not allow the class to be made at all. C++: class A { void someMethod() = 0; // do something that's important to this interface } class B { void someMethod() = 0; // do something that's important to this interface, but has a completely different use than the method for A } class C: public A, public B { void A::someMethod() { // implementation for A's someMethod() } void B::someMethod() { // implementation for B's someMethod(), different than implementation for A's, have completely different uses, but since you didn't design the interfaces, they happen to have the same name } } called by casting the reference of an instance of C to to A* or B* same for definition of A and B (except using interfaces instead) definition of C class C: A, B { void someMethod() // default implementation of someMethod if base class isn't specified or other implementation not provided { // implementation for A's someMethod, really shouldnt be default, but I just wanted to demonstrate this because it can be useful } void B.someMethod() { // implementation for B's someMethod } } C intsanceOfC; A's called with ((A)instanceOfC).someMethod(); // calls default someMethod() B's called with ((B)instanceOfC).someMethod(); // calls B.someMethod() if no cast is used it uses the default: instanceOfC.someMethod(); D and Java: Can't be done, only 1 implementation of someMethod() can be written.
Mar 20 2004