23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Building on Other Classes<br />

baseclass. Youcannotaccessthebaseclass object’s fields directly from the<br />

derived class; you must use access methods defined for the base class.<br />

Derived Class Constructor<br />

The constructor function for a derived class has two special characteristics:<br />

• It calls the constructor function for the base class to create the inherited<br />

fields.<br />

• It requires a special calling syntax for the class function, specifying both<br />

the derived class and the base class.<br />

The syntax for establishing simple inheritance using the class function is:<br />

derivedObj = class(derivedObj, 'derivedClass', baseObj);<br />

Simple inheritance can span more than one generation. If a base class is<br />

itself an inherited class, the derived class object automatically inherits from<br />

theoriginalbaseclass.<br />

Visibility of Class Properties and Methods<br />

Thebaseclassdoesnothaveaccesstothe derived class properties. The<br />

derived class cannot access the base class properties directly, but must use<br />

base class access methods (e.g., get or subsref method) to access the base<br />

class properties. From the derived class methods, this access is accomplished<br />

via the base class field in the derived class structure. For example, when a<br />

constructor creates a derived class object c,<br />

c = class(c, 'derivedClassname', baseObject);<br />

<strong>MATLAB</strong> automatically creates a field, c.baseClassname, intheobject’s<br />

structure that contains the base object. You could then have a statement in<br />

the derived class display method that calls the base class display method.<br />

display(c.baseClassname)<br />

See “Designing the Stock Class” on page 9-49 for examples that use simple<br />

inheritance.<br />

9-39

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!