Programming |
Simple Inheritance
A class that inherits attributes from a single parent class, and adds new attributes of its own, uses simple inheritance. Inheritance implies that objects belonging to the child class have the same fields as the parent class, as well as additional fields. Therefore, methods associated with the parent class can operate on objects belonging to the child class. The methods associated with the child class, however, cannot operate on objects belonging to the parent class. You cannot access the parent's fields directly from the child class; you must use access methods defined for the parent.
The constructor function for a class that inherits the behavior of another has two special characteristics:
class
function is slightly different, reflecting both the child class and the parent class.
The general syntax for establishing a simple inheritance relationship using the class
function is
Simple inheritance can span more than one generation. If a parent class is itself an inherited class, the child object will automatically inherit from the grandparent class.
Visibility of Class Properties and Methods
The parent class does not have knowledge of the child properties or methods. The child class cannot access the parent properties directly, but must use parent access methods (e.g., get
or subsref
method) to access the parent properties. From the child class methods, this access is accomplished via the parent field in the child structure. For example, when a constructor creates a child object c
,
MATLAB automatically creates a field, c.
parentClassname
, in the object's structure that contains the parent object. You could then have a statement in the child's display method that calls the parent's display method.
See Designing the Stock Class for examples that use simple inheritance.
Building on Other Classes | Multiple Inheritance |
© 1994-2005 The MathWorks, Inc.