OOP Demystified
We want to create our own interface, which we will call HTMLSource . The feature this interface will provide is the ability to format its data as HTML.
Now, before we go much further, you should be aware that providing user interface functionality in a class that also implements some sort of logic or rule functionality is typically considered bad design. Ideally, logical classes are separated from user interface classes. A more realistic example would be to provide an XMLSource interface to provide XML-formatted data. However, XML and its usage are more complicated than we have space to cover.
The HTMLSource interface we ll create will have a single method, GetHTML() , that returns the object s data in an HTML-formatted string.
The interface in Java would look like the following:
package Registration; public interface HTMLSource { public String GetHTML(); }
Note the use of the interface keyword to declare HTMLSource as an interface, not a class. Inside you can see fairly typical method signature:
public String GetHTML();
This means that GetHTML is a method that accepts no parameters and returns a string. Note, however, that unlike a normal Java class, the body of the function is not defined as or marked abstract. By being in an interface, it is implicitly abstract.
The same interface in a C# program would look pretty similar:
namespace Registration { public interface IHTMLSource { string GetHTML(); } }
Again, C# uses the same interface keyword, which is followed by the interface name, and inside you find the return data type, method name , and argument list but not the body of the method. Note that by popular convention (but not by requirement), interfaces in C# normally start with the letter I.
Категории