Object-Oriented Concepts and Constructs
Class, object, method, message, instance variable, and inheritance are the basic concepts of the OO technology. OO metrics are mainly measures of how these constructs are used in the design and development process. Therefore, a short review of definitions is in order.
- A class is a template from which objects can be created. It defines the structure and capabilities of an object instance. The class definition includes the state data and the behaviors (methods) for the instances of that class. The class can be thought of as a factory that creates instances as needed. For example, an Account class may have methods to allow deposits and withdrawals, using a balance instance variable to hold the current balance. This definition defines how an Account works, but it is not an actual account.
- An abstract class is a class that has no instances, created to facilitate sharing of state data and services among similar, more specialized subclasses.
- A concrete class is a class that has instances. For example, there might be a Savings Account class with a number of instances in a bank application.
- An object is an instantiation of a class. It is anything that models things in the real world. These things can be physical entities such as cars , or events such as a concert, or abstractions such as a general-purpose account. An object has state (data) and behavior (methods or services), as defined for the class of objects it belongs to.
- A method is a class service behavior. It operates on data in response to a message and is defined as part of the declaration of a class. Methods reflect how a problem is broken into segments and the capabilities other classes expect of a given class.
- Message: Objects communicate via messages. To request a service from another object, an object sends it a message. This is the only means to get information from an object, because its data is not directly accessible (this is called encapsulation).
- Instance variable is a place to store and refer to an object's state data. In traditional programming, this would be a data variable. In OO paradigm, data is made up of instance variables of an object.
- Inheritance: Similar classes of objects can be organized into categories called class hierarchies. The lower-level classes (called subclasses) can use the services of all the higher classes in their hierarchy. This is called inheritance. Inheritance is simply a way of reusing services and data. As an example, Savings accounts are types of general Account, and IRA accounts are types of Savings accounts. The Savings account inherits the capability to handle deposits from the Account class. The number of subclasses in the class hierarchy is called hierarchy nesting or depth of inheritance tree (DIT).
Figure 12.1 provides a pictorial description of the OO structures and key concepts. For example, "Account", "SavingsCheckingAccount", "HomeEquity", and "CertificateAccount" are all classes. "Account" is also an abstract class; the other classes are its subclasses, which are concrete classes. "Ken Brown'sSavingsAccount" and "Ken Brown's HomeEquity Account" are objects. The "Account" class has three subclasses or children. "AccountNumber" is an instance variable, also called an attribute, and getBalance() is a method of the "Account" class. All instance variables and methods for the "Account" class are also the instance variables and methods of its subclasses through inheritance. The object "Ken Brown'sSavingsAccount" sends a message to the object "Ken Brown's HomeEquity Account", via the "transfer()" method and thereby invokes the "setMonthlyPayment()" method. Therefore, the class "SavingsCheckingAccount" is coupled to the class "HomeEquity" through the message.
Figure 12.1. An Object-Oriented Class Hierarchy