Object-Oriented Thought Process
Authors: Weisfeld M.
Published year: 2003
Pages: 42-44/164
Buy this book on amazon.com >>
 <  Day Day Up  >  

Chapter 4. The Anatomy of a Class

We have already discussed in great detail object-oriented (OO) concepts and the difference between the interface and the implementation. No matter how well you think out the problem of what should be an interface and what should be part of the implementation, the bottom line always comes down to how useful the class is and how it interacts with other classes. A class should never be designed in a vacuum , for as might be said, no class is an island. When objects are instantiated , they almost always interact with other objects. An object can also be used within another object, or be inherited. The following section is a bit of a dissection of a class, and the rest of the chapter offers some guidelines that you should consider when designing classes.

In this chapter we'll examine a simple class and then take it apart piece by piece. We will continue using the cabbie example presented in Chapter 2, "How to Think in Terms of Objects."

In this chapter, we'll discuss the following parts of a class:

  • Class name ” How the class name is identified

  • Comments ” How to create comments to document your code

  • Attributes ” How to define attributes for use in the class

  • Constructors ” Special methods used to properly initialize a class

  • Accessors ” Methods that are used to control access to private attributes

  • Public interface methods ” How to define public interface methods

  • Private implementation methods ” How to define private implementation methods

Only a Template

This class is meant for illustration purposes only. Some of the methods are not fleshed out (meaning that there is no implementation) and simply present the interface.


 <  Day Day Up  >  
 <  Day Day Up  >  

The Name of the Class

Figure 4.1 shows the class that will be dissected. Plain and simple, the name of the class in our example, Cabbie , is the name located after the keyword class :


public class Cabbie {



}
Figure 4.1. Our sample class.

graphics/04fig01.gif

Using Java Syntax

Remember that the convention for this book is to use Java syntax. The syntax might be somewhat different in C# or C++, and totally different in other OO languages such as Smalltalk.


The class Cabbie name is used whenever this class is instantiated .

 <  Day Day Up  >  
 <  Day Day Up  >  

Comments

Regardless of the syntax of the comments used, they are vital to understanding the function of a class. In Java, C#, and C++, there are two kinds of comments.

The Extra Java and C# Comment Style

In Java and C#, there are actually three types of comments. In Java, the third comment type ( /** */ ) relates to a form of documentation that Java provides. We will not cover this type of comment in this book. C# provides similar syntax to create XML documents.


The first comment is the old C-style comment, which uses /* (slash-asterisk) to open the comment and */ (asterisk-slash) to close the comment. This type of comment can span more than one line, and it's important not to forget to use the pair of open and close comment symbols for each comment. If you miss the closing comment ( */ ), some of your code might be tagged as a comment and overlooked by the compiler. Here is an example of this type of comment used with the Cabbie class:


/*



  This class defines a cabbie and assigns a cab



*/

The second type of comment is the // (slash-slash), which renders everything after it, to the end of the line, a comment. This type of comment spans only one line, so you don't need to remember to use a close comment symbol, but you do need to remember to confine the comment to just one line and not include any live code after the comment. Here is an example of this type of comment used with the Cabbie class:


// Name of the cabbie
 <  Day Day Up  >  
Object-Oriented Thought Process
Authors: Weisfeld M.
Published year: 2003
Pages: 42-44/164
Buy this book on amazon.com >>

Similar books on Amazon