Changes

Jump to: navigation, search

Objective-C Inheritance

5 bytes added, 19:12, 26 October 2009
An Objective-C Inheritance Example
</pre>
As we can see from the declaration, this class is a subclass of the NSObject base class and contains a number of instance variables and instance methods. Though this is a somewhat basic class it does everything necessary if all you need it to do is store an account number and account balance. Suppose, however, that in addition to the BankAccount class you also needed a class to be used for savings accounts. A savings account will still need to hold an account number and a current balance and methods will still be needed to access that data. One option would be to create an entirely new class the , one that duplicates much all of the functionality of the BankAccount class together with the new features required by a savings account. A more efficient approach would be to create a new class that is a ''subclass'' fo the BankAccount class. The class will then inherit all the features of the BankAccount class but can then be extended to add the additional functionality required by a savings account.
To create a subclass of BankAccount that we will call SavingsAccount we simply write the appropriate interface and implementation structure, this time specifying BankAccount instead of NSObject as the parent class (inheritance passes down through all levels of the class hierarchy so SavingsAccount will still inherit from NSObject via the BankAccount parent class):

Navigation menu