OOP Demystified
In order for you to understand interfaces, we ll start out with a conceptual example. We have been working with a school registration system throughout the book, so we ll stick with that example. In our registration system, so far we have seen similar (or derived) classes such as Student and Instructor ( potentially derived from a Person class) as well as dissimilar classes such as Enrollment and Course .
Because Course , Person , and Enrollment are dissimilar, they probably won t share the same base class (we ll discuss super base classes later). But, as it turns out, we may want to have a standard method in each of these classes to permit us to display the data of an object.
Because we have already discussed a Display method common to each of the classes, let s make this example a bit more interesting. Our goal is to introduce a standard method, called GetHTML , for objects to generate their own HTML source for display in a web page.
Now you might be jumping ahead here. Perhaps you are thinking, Why do I need this ˜interface thingy? Why can t I just add the GetHTML method to the desired classes? Well, the fact is, we will be doing this, but you will see how interfaces permit us to treat classes such as Student and Course as generic providers of HTML data, rather than as two distinct classes.
A Quick Introduction to HTML
If you ve been on the Internet, you have seen HTML in action. The Hypertext Markup Language defines the appearance and content of a web page. When you visit a website, typically your browser asks the web server for an HTML file, which the server serves up to you. Your browser then follows the HTML commands (or tags ) and renders the proper appearance intended by the author of the HTML file.
Although some web pages can be fairly complicated, you can normally visit any web page and view the HTML source for that page. In Internet Explorer, you would select View Source from the main menu to view the HTML source for the current page.
HTML is defined as a set of tags that control the formatting of a page. These tags normally are used in pairs, where the text or content of the web page is wrapped by the tags. Table 11-1 gives a brief summary of the common HTML tags, some of which we will be using in our example:
Start tag | End tag | Description |
---|---|---|
<BR> | None | Indicates a line break. |
<B> | </B> | The text between the tags is displayed in a bold font. Example: <B>Bold</B> text. |
<P> | </P> | Denotes a paragraph. Example: <P> Paragraph1</P><P>Paragraph2</P> |
<TABLE> | </TABLE> | Denotes a tabular or columnar table. |
<TR> | </TR> | Denotes a row within a table. |
<TD> | </TD> | Denotes a cell or column within a table. Example: <TR><TD>Jones</TD><TD>Tom</TD></TR> |
Категории