| [ directory ] |
|
6.2 Multiple Interfaces and Explicit Interface DeclarationA class can implement multiple interfaces in C# and Java. For example, the Person class can also implement an IEmotion interface (Listing 6.5). Listing 6.5 The IEmotion Interface (C#)
namespace Interfaces {
interface IEmotion {
void Happy();
void Sad();
void Angry();
void Love();
}
}
Let's create another class similar to the Person class (call it Man) and let it implement the two interfaces. Note that both IEmotion and ILife have a Happy() and a Sad() method. One way to implement the class is with C#, as in Listing 6.6. Listing 6.6 Implementing Multiple Interfaces with Method Name Conflicts (C#)
using System;
namespace Interfaces {
class Man : ILife, IEmotion {
string name;
public Man(string name) {
this.name = name;
}
public void Birth() {
Console.WriteLine("Birth of {0}",this.name);
}
public void Death() {
Console.WriteLine("Death of {0}",this.name);
}
public void Happy() {
Console.WriteLine("{0} is happy",this.name);
}
public void Sad() {
Console.WriteLine("{0} is sad",this.name);
}
public void Angry() {
Console.WriteLine("{0} is angry",this.name);
}
static void Main(string[] args) {
Man per = new Man("John");
per.Happy();
ILife life = (ILife)per;
life.Happy();
IEmotion emo = (IEmotion)per;
emo.Happy();
}
}
}
Listing 6.6 prints John is happy three times on the console. This is fine as long as you want the same implementation for the Happy method of the ILife interface and the IEmotion interface. But if you want different implementations for the Happy and Sad methods of these two interfaces, you must explicitly specify the interface in the Man class (Listing 6.7). Listing 6.7 Explicit Interface Implementation (C#)
using System;
namespace Interfaces {
class Man : ILife, IEmotion {
protected string name;
public Man(string name) {
this.name = name;
}
public Man() {
}
public void Birth() {
Console.WriteLine("Birth of {0}",this.name);
}
public void Death() {
Console.WriteLine("Death of {0}",this.name);
}
void ILife.Happy() {
Console.WriteLine("{0} is happy",this.name);
}
void ILife.Sad() {
Console.WriteLine("{0} is sad",this.name);
}
void IEmotion.Happy() {
Console.WriteLine("{0} has happy emotions",this.name);
}
void IEmotion.Sad() {
Console.WriteLine("{0} has sad emotions",this.name);
}
public void Angry() {
Console.WriteLine("{0} is angry",this.name);
}
static void Main(string[] args) {
Man per = new Man("John");
ILife life = (ILife)per;
life.Happy();
IEmotion emo = (IEmotion)per;
emo.Happy();
}
}
}
This technique of stating the interface name before the method name is called explicit interface declaration. There are no access modifiers on a method declared with explicit interfaces. Also, in the Main method there is no calling of the Happy method from the class itself (per.Happy() is missing). This is because of the explicit interface implementation; calling the Happy() or Sad() method on the object without casting the object to the relevant interfaces is no longer valid. For the per.Happy() call to be valid, there would have to be a separate public void Happy() method on the Man class, which is different from the one defined in the two interfaces. The fact that the two Happy and Sad methods are prefixed by their interface names indicates that these methods are obtained by implementing the interfaces. If we remove these prefixes and remove the interfaces, the Happy() and Sad() methods would be treated like any other method of the Man class (that is, specific to the Man class). The output of Listing 6.7 is as follows: John is happy John has happy emotions Java does not support explicit interface declaration. Therefore, calling the happy() method returns the same result whether you call it on the class, the Life interface, or the Emotion interface (Listing 6.8). And it is not possible to have different implementations of the three method calls. In contrast, with C#, calling the Happy() method on the class, the ILife interface, and the IEmotion interface returns three different results. Listing 6.7 has two Happy() methods: one defined for the ILife interface, and another for the IEmotion interface. No method is defined on the class itself. Such a method would be easily defined by declaring it as public void Happy(). Then calls to the three Happy() methods would all give a different result. Whether this flexibility is confusing, we leave to you to decide. Listing 6.8 Java Equivalent of the Multiple Interface Implementation
interface Life {
void birth();
void death();
void happy();
void sad();
}
interface Emotion {
void happy();
void sad();
}
public class Man implements Life, Emotion{
String name;
public Man(String name) {
this.name = name;
}
public Man() {}
public void birth() {
System.out.println("Birth of " ame);
}
public void death() {
System.out.println("Death of " ame);
}
public void happy() {
System.out.println(name+ " is happy");
}
public void sad() {
System.out.println(name+ " is sad");
}
static final void main(String[] args) {
Man per = new Man ("John");
per.happy();
Life life = (Life)per;
life.happy();
Emotion emo = (Emotion)per;
emo.happy();
}
}
|
| [ directory ] |
|