September 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

The Components of a Class

  • March 10, 2005
  • By Matt Weisfeld
  • Send Email »
  • More Articles »

This series, The Object-Oriented Thought Process, is intended for someone just learning an object-oriented language and wants to understand the basic concepts before jumping into the code or someone who wants to understand the infrastructure behind an OOP language they are already using. Click here to start at the beginning of the series.

In keeping with the code examples used in the previous articles, Java will be the language used to implement the concepts in code. One of the reasons that I like to use Java is because you can download the Java compiler for personal use at the Sun Microsystems Web site http://java.sun.com/. You can download the J2SE 1.4.2 SDK (software development kit) to compile and execute these applications and will provide the code listings for all examples in this article. I have the SDK 1.4.0 loaded on my machine. I will provide figures and the output (when appropriate) for these examples. See the previous articles in this series for detailed descriptions for compiling and running all the code examples in this series.

The last column continued the discussion on insuring that an object is created in a safe state. When designed properly, an object must be initialized properly and in no cases should the state of the object contribute to a program crash or the dissemination of incorrect data.

This article will explore the actual components of a class. In other words, what the class is really made of.

Class Internals

I have already covered the difference between the interface and the implementation of a class. 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 also can be used within another object, or be inherited. This article can be thought of as a dissection of a class and offers some guidelines that you should consider when designing classes. You'll examine a simple class and then take it apart piece by piece—using the cabbie example presented in an earlier column.

Class components to discuss include:

  • 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

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.

The Name of the Class

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

public class Cabbie {

}

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

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

Comments

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. I will not cover this type of comment in this column. 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



Click here for a larger image.

Figure 1: The sample class.

Attributes

Attributes represent the state of the object because they store the information about the object. For your example, the Cabbie class has attributes that store the name of the company, the name of the cabbie, and the cab assigned to the cabbie. For example, the first attribute stores the name of the company:

private static String companyName = "Blue Cab Company";

Note here the two keywords, private and static. The keyword private signifies that a method or variable can be accessed only within the declaring object.

Hiding as Much Data as Possible

All the attributes in this example are private. This is in keeping with the design principle of keeping the interface design as minimal as possible. The only way to access these attributes is through the method interfaces provided (which we will explore later in this article).

The static keyword signifies that there will be only one copy of this attribute for all the objects instantiated by this class. Basically, this is a class attribute. Thus, even if 500 objects are instantiated from the Cabbie class, there will be only one copy in memory of the companyName attribute (see Figure 2).

Figure 2: Object memory allocation.

The second attribute, name, is a string that holds the name of the cabbie:

private String name;

This attribute is also private so that other objects cannot access it directly. They must use the interface methods.

The myCab attribute is a reference to another object. The class, called Cab, holds information about the cab, such as its serial number and maintenance records:

private Cab myCab;

Passing a Reference

It is likely that the Cab object was created by another object. Thus, the object reference would be passed to the Cabbie object. However, for the sake of this example, the Cab is created within the Cabbie object. Likewise, for the purposes of this example, you are not really interested in the internals of the Cab object.

Note that at this point, only a reference to a Cab object is created; there is no memory allocated by this definition.

Constructors

This Cabbie class contains two constructors. You know they are constructors because they have the same name as the class: Cabbie. The first constructor is the default constructor:

public Cabbie() {
   name = null;
   myCab = null;
}

Technically, this is not a default constructor. The compiler will provide a default constructor if you do not specify a constructor for this class. By definition, the reason it is called a default constructor here is because it is a constructor with no arguments. If you provide a constructor with arguments, the system will not provide a default constructor. The rule is that the default constructor is only provided if you provide no constructors.

In this constructor, the attributes Name and myCab are set to null:

name = null;
myCab = null;




Page 1 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel