Bringing It All Together


Having the classes functional and implementing the proper interface are important, but we are still missing the demonstrational code that shows why the interface is different from a class with only abstract methods in it. The answer is presented here, and it requires us to write a function that takes an interface (not a class) as a parameter.

In Java, we would write this as follows :

 protected void ShowHTML( HTMLSource SomeObject ) 
{
System.out.println( SomeObject.GetHTML() );
}

The C# version would be nearly identical:

 protected void ShowHTML( IHTMLSource SomeObject ) 
{
System.Console.Out.WriteLine( SomeObject.GetHTML() );
}

Now, in both Java and C#, we have a ShowHTML method that accepts any object that implements the HTMLSource interface. With this knowledge, it is safe for the preceding ShowHTML function to call the GetHTML method on the received object.

Figure 11-3 shows what our diagramming now looks like, with multiple classes that implement an interface.

click to expand
Figure 11-3: A diagram showing multiple classes that implement an interface



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net