Hierarchies of Classes — Concepts

Classification

Organizing classes into hierarchies facilitates the reuse of code and, thereby, the reuse of solutions to design problems that have already been solved. You might think of class hierarchies as sets — supersets, (referred to as superclasses or base classesand subsets, referred to as subclasses or derived classes). For example, the following picture shows how you could represent an employee database with classes.

At the root of the hierarchy is the Employee class. It contains data and operations that apply to the set of all employees. Contained in the set of employees are subsets whose members, while still employees, are also members of sets that more specifically define the type of employee. These subsets are represented by subclasses like TestEngineer.

Developing the Abstraction

Classes are representations of real world concepts or things. When designing a class, you need to form an abstraction of what the class represents. Consider an abstraction of an employee and what are the essential aspects of employees for the intended use of the class. Name, address, and department might be what all employees have in common.

When designing classes, your abstraction should contain only those elements that are necessary. For example, the employee's hair color and shoe size certainly characterize the employee, but are probably not relevant to the design of this employee class. Their sales region might be relevant only to some employee so this characteristic belongs in a subclass.

Designing Class Hierarchies

As you design a system of classes, you should place common data and functionality in a superclass, which you can then use to derive subclasses classes. The subclasses inherit all the data and functionality of the superclass and contain only aspects that are unique to their particular purposes. This approach provides advantages:

Super and Subclass Behavior

Subclass objects behave like objects of the super class because they are specializations of the super class. This fact facilitates the development of related classes that behave similarly, but are implemented differently.

A Subclass Object Is A Superclass Object

You usually can describe the relationship between an object of a subclass and an object of its superclass with a statement like:

The subclass is a superclass . For example: An Engineer is an Employee.

This relationship implies that objects belonging to a subclass have the same properties, methods, and events of the superclass, as well as any new features defined by the subclass.

A Subclass Object Can be Treated Like a Superclass Object

You can pass a subclass object to a super class method, but you can access only those properties that are defined in the super class. This behavior enables you to modify the subclasses without affecting the super class.

Two points about super and subclass behavior to keep in mind are:

Therefore, you can treat an Engineer object like any other Employee object, but an Employee object cannot pass for an Engineer object.

Implementation and Interface Inheritance

MATLAB classes support both the inheritance of implemented methods from a superclass and the inheritance of interfaces defined by abstract methods in the superclass.

Implementation inheritance enables code reuse by subclasses. For example, an employee class might have a submitStatus method that can be used by all employee subclasses. Subclasses can extend an inherited method to provide specialized functionality, while reusing the common aspects. See Modifying Superclass Methods and Properties for more information on this process.

Interface inheritance is useful in cases where you want a group of classes to provide a common interface, but these classes need to create specialized implementations of methods and properties that define the interface. You create an interface using an abstract class as the superclass. This class defines the methods and properties that you must implement in the subclasses, but does not provide an implementation, in contrast to implementation inheritance.

The subclasses are require to provide their own implementation of the abstract members of the superclass. To create an interface, define methods and properties as abstract using their Abstract attribute.

See Abstract Classes and Interfaces for more information and an example.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS