OOAD

Object-Oriented Design Basics: Objects, Classes and Inheritance

posted in: OOAD, Software Engineering | 0

Objects

In general terms, objects are units of reusable software functionality. Different objects can be assembled together to become parts of software applications.

Objects expose their functionality through properties and methods. Properties define, store and expose attributes, and methods define and expose behaviors and actions. The collection of values that the properties store is called state. The entire collection of properties and methods that an object exposes is called its interface.

Objects in JavaScript

In formal JavaScript terms, an object interface is expressed as a collection of key/value pairs. All such key/value pairs are considered to be properties. Methods are properties that have a value of type function, in other words properties whose value is a function.

Syntactically, object properties and methods are accessed with dot notation: objectReference.propertyName or objectReference.methodName(argList). The object reference is called the receiver, because it receives the request for the property or method and acts on it, either setting or returning the value of the property or calling (invoking) the method function. So, another way of looking at methods is that they are functions that have a receiver.

Object properties and methods may alternatively be accessed using square-bracket notation, for example objectReference['propertyName'] or objectReference['methodName'](argList). This syntax must be used when using a property name that is stored in a variable.

Classes

A class is a definition of an object. The relationship between object and class is analogous to the relationship between a house and the blueprint from which the house is built.

An object is said to be an instance of a class, and the process of creating an object from a class is called instantiation. Just as you can build any number of houses from the same blueprint, you can instantiate any number of objects from the same class.

The architecture of objects, then, is expressed in terms of classes. So classes are what designers use to design objects.

Here is a simple example in JavaScript of a class, with the code to create an instance of it (using the new keyword) and to call its bark method:

Inheritance

Inheritance is the ability of one class (a subclass) to use the state and behaviors of another class (a superclass or base class, from which it inherits). For example, dogs, cats and birds all have specialized behaviors of their own. But they also have behavior that is common to all animals.

This generalized behavior can be modeled in a superclass from which all the animals inherit, so they don’t have to provide repetitive implementations of the behavior. For example, an Animal class might have a sleep method that all subclasses of Animal will share. Then, those subclasses will have specialized behaviors of their own.

Each instance has specialized behavior of its own, while sharing Animal‘s methods as well. When Dog, Cat or Bird instances call the sleep method, the call will be passed to an instance of their Animal superclass, which will then execute its sleep method.

Conceptually, inheritance relationships are derived either by extracting common behavior in several classes into a common superclass (class generalization) or by doing the opposite: creating subclasses that add in specialized behavior of their own, while reusing the common behavior of the superclass (class specialization).

Semantically, inheritance defines an “is a” relationship: a subclass is an example of the superclass from which it inherits. For example, if a Dog class inherits from an Animal class, the dog “is an” animal.

For more about class design, check out Object-Oriented Design: Using Cohesion and Coupling to Create Well-Formed Objects.