Difference between revisions of "Objective-C Inheritance"
(New page: In the chapter entitled An Overview of Objective-C Object Oriented Programming we covered the basic concepts of object-oriented programming and worked through an example of creating a ...) |
(→Inheritance, Classes and Subclasses) |
||
Line 8: | Line 8: | ||
Classes need not only be derived from a root class. For example, a subclass can also inherit from another subclass with the potential to create large and complex class hierarchies. | Classes need not only be derived from a root class. For example, a subclass can also inherit from another subclass with the potential to create large and complex class hierarchies. | ||
+ | |||
+ | == An Objective-C Inheritance Example == | ||
+ | |||
+ | As with most programming concepts the subject of inheritance in Objective-C is perhaps best illustrated with an example. In the we created a class called BankAccount. The declaration of this class is reproduced below: | ||
+ | |||
+ | <pre> |
Revision as of 20:05, 21 October 2009
In the chapter entitled An Overview of Objective-C Object Oriented Programming we covered the basic concepts of object-oriented programming and worked through an example of creating a new class using Objective-C. In that example, our new class was derived from the NSObject base class and as such inherited a number of traits from that parent class. In this chapter we will explore the subject of inheritance in greater detail.
Inheritance, Classes and Subclasses
The concept of inheritance brings something of a real-world view to programming. It allows a class to be defined which has a number of characteristics and then other classes to be created which are derived from that class. The derived class inherits all of the features of the parent class and typically then adds some features of its own.
By deriving classes we create what is often referred to as a class hierarchy. The class at the top of the hierarchy is known as the base class or root class and the derived classes as subclasses or child classes. Any number of subclasses may be derived from a class. The class from which a subclass is derived is called the parent class.
Classes need not only be derived from a root class. For example, a subclass can also inherit from another subclass with the potential to create large and complex class hierarchies.
An Objective-C Inheritance Example
As with most programming concepts the subject of inheritance in Objective-C is perhaps best illustrated with an example. In the we created a class called BankAccount. The declaration of this class is reproduced below: