Special

Introducing the “Welcome to Xojo” Bundle!

New to Xojo and looking for guidance? We've put together a terrific bundle to welcome you! Xojo Bundle

This bundle includes six back issues of the magazine -- all of year 21 in printed book and digital formats -- plus a one-year subscription (beginning with 22.1) so you'll be learning all about Xojo for the next year. It's the perfect way to get started programming with Xojo. And you save as much as $35 over the non-bundle price!

This offer is only available for a limited time as supplies are limited, so hurry today and order this special bundle before the offer goes away!

Article Preview


Buy Now

Issue 4.2

COLUMN

Pondering Polymorphism

Issue: 4.2 (November/December 2005)
Author: Charles Yeomans
Author Bio: Charles is the author of I Declare: Calling External Functions in REALbasic, available online at http://www.declareSub.com/
Article Description: No description available.
Article Length (in bytes): 5,827
Starting Page Number: 33
Article Number: 4215
Related Web Link(s):

http://www.declareSub.com/

Excerpt of article text...

Polymorphism is perhaps the most elusive of the fundamental concepts of object-oriented programming. Perhaps the reason is that it's often poorly explained. What I think of as the standard mistake of exposition is to explain polymorphism in terms of the ability to override methods in subclasses. This is not polymorphism; this is a way to implement polymorphism. In many previous columns I have demonstrated polymorphism in action. Here I take my crack at putting forth my understanding of polymorphism.

Object Types and Polymorphism

In REALbasic, an object type is a description of what an object can do. In particular, a type specification is a list of the public methods and properties of an object. An object comes with a default type -- the set of all public methods and properties of its class. However, an object can have more than one type.

If the object's class inherits from another class, the object also inherits the associated type of the superclass. Thus an EditField also has types RectControl and Control. Also, all classes other than Object inherit from Object, so every object in REALbasic has type Object.

Class interfaces provide the other means by which an object can have more than one type. Recall that a class interface is essentially a list of methods that an object must implement. For example, the BinaryStream class implements the Readable class interface, which consists of the methods EOF, Read, ReadAll, and ReadError. Thus an object created as an instance of BinaryStream is also of type Readable.

The types supported by an object are known at compile-time, and the compiler uses this information to control the assignment of object references. You can only assign an object reference to one of a type that the assigned object supports. Thus, for instance, the following assignments are valid.

...End of Excerpt. Please purchase the magazine to read the full article.