One of the most important concepts in object-oriented programming is that of inheritance. Inheritance allows us to define a class in terms of another class which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and fast implementation time.

When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the baseclass, and the new class is referred to as the derived class.

The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal, hence dog IS-A animal as well and so on.

Base & Derived Classes:

Objective-C allows only multilevel inheritance, i.e., it can have only one base class but allows multilevel inheritance. All classes in Objective-C is derived from the superclass NSObject.

@interface derived-class: base-class

Consider a base class Person and its derived class Employee as follows:

#import <Foundation/Foundation.h>

@interface Person : NSObject
{
NSString *personName;
NSInteger personAge;
} - (id)initWithName:(NSString *)name andAge:(NSInteger)age;
- (void)print;
@end @implementation Person - (id)initWithName:(NSString *)name andAge:(NSInteger)age{
personName = name;
personAge = age;
return self;
} - (void)print{
NSLog(@"Name: %@", personName);
NSLog(@"Age: %ld", personAge);
} @end @interface Employee : Person
{
NSString *employeeEducation;
} - (id)initWithName:(NSString *)name andAge:(NSInteger)age
andEducation:(NSString *)education;
- (void)print; @end @implementation Employee - (id)initWithName:(NSString *)name andAge:(NSInteger)age
andEducation: (NSString *)education
{
personName = name;
personAge = age;
employeeEducation = education;
return self;
} - (void)print
{
NSLog(@"Name: %@", personName);
NSLog(@"Age: %ld", personAge);
NSLog(@"Education: %@", employeeEducation);
} @end int main(int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Base class Person Object");
Person *person = [[Person alloc]initWithName:@"Raj" andAge:];
[person print];
NSLog(@"Inherited Class Employee Object");
Employee *employee = [[Employee alloc]initWithName:@"Raj"
andAge: andEducation:@"MBA"];
[employee print];
[pool drain];
return ;
}

When the above code is compiled and executed, it produces the following result:

-- ::09.842 Inheritance[:] Base class Person Object
-- ::09.844 Inheritance[:] Name: Raj
-- ::09.844 Inheritance[:] Age:
-- ::09.845 Inheritance[:] Inherited Class Employee Object
-- ::09.845 Inheritance[:] Name: Raj
-- ::09.846 Inheritance[:] Age:
-- ::09.846 Inheritance[:] Education: MBA

Access Control and Inheritance:

A derived class can access all the private members of its base class if it's defined in the interface class, but it cannot access private members that are defined in the implementation file.

We can summarize the different access types according to who can access them in the following way:

A derived class inherits all base class methods and variables with the following exceptions:

  • Variables declared in implementation file with the help of extensions is not accessible.

  • Methods declared in implementation file with the help of extensions is not accessible.

  • In case the inherited class implements the method in base class, then the method in derived class is executed.

最新文章

  1. BZOJ2758 : [SCOI2012]Blinker的噩梦
  2. 禁用iPhone手机浏览器上给电话号码自动加上的link样式
  3. 大神的游戏(codevs 1353)
  4. PHP 错误与异常 笔记与总结(13 )自定义异常类
  5. HDU 1269:迷宫城堡(强连通)
  6. 职责链实现的apache.chain使用
  7. sublime配置python开发
  8. 安装zookeeper集群
  9. 团队作业4——第一次项目冲刺(Alpha版本)5th day
  10. Liunx小白须知
  11. BZOJ 1815: [Shoi2006]color 有色图(Polya定理)
  12. Win7共享文件夹简单?这个共享问题可以难倒90%的人
  13. vim 常用指令
  14. Maven替换为国内仓库
  15. taro之React Native 端开发研究
  16. 关于COM类工厂80070005和8000401a错误分析及解决办法
  17. What if you are involved in an automobile accident in the US
  18. 从零系列--node爬虫利用进程池写数据
  19. soj1090.Highways
  20. MyBatis—mybatis-config.xml模板

热门文章

  1. HTML特殊字符的html、js、css写法汇总 (转)
  2. Jasper-template
  3. .NETFramework-Timers:Timer
  4. docker 学习(八) docker file
  5. js中关于事件捕获与事件冒泡的小实验
  6. 2016年第七届蓝桥杯国赛试题(JavaA组)
  7. JAVA企业级开发-xml基础语法&amp;约束&amp;解析(04)
  8. 函数PARSENAME使用和截取字符串
  9. 文本情感分析(一):基于词袋模型(VSM、LSA、n-gram)的文本表示
  10. Swoole 多协议 多端口 的应用