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.

Objective-C Inheritance的更多相关文章

  1. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  2. C++ vs Objective C

    oc Short list of some of the major differences: C++ allows multiple inheritance, Objective-C doesn't ...

  3. 代码的坏味道(12)——平行继承体系(Parallel Inheritance Hierarchies)

    坏味道--平行继承体系(Parallel Inheritance Hierarchies) 平行继承体系(Parallel Inheritance Hierarchies) 其实是 霰弹式修改(Sho ...

  4. 5.Inheritance Strategy(继承策略)【EFcode-first系列】

    我们已经在code-first 约定一文中,已经知道了Code-First为每一个具体的类,创建数据表. 但是你可以自己利用继承设计领域类,面向对象的技术包含“has a”和“is a”的关系即,有什 ...

  5. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  6. single-table inheritance 单表继承

    type 字段在 Rails 中默认使用来做 STI(single-table inheritance),当 type 作为普通字段来使用时,可以把SIT的列设置成别的列名(比如不存在的某个列). 文 ...

  7. C++: virtual inheritance and Cross Delegation

    Link1: Give an example Note: I think the Storable::Write method should also be pure virtual. http:// ...

  8. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  9. React之Composition Vs inheritance 组合Vs继承

    React的组合   composition: props有个特殊属性,children,组件可以通过props.children拿到所有包含在内的子元素, 当组件内有子元素时,组件属性上的child ...

  10. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

随机推荐

  1. Spring如何引用properties文件里的…

    [zt]Spring如何引用properties文件里的配置 来自 http://blog.csdn.net/luobo525/archive/2006/11/06/1370258.aspx 1.Pr ...

  2. skb_store_bits() 和 skb_copy_bits()

    int skb_copy_bits(const struct sk_buff *skb, int offset, void *to, int len);    int skb_store_bits(c ...

  3. 2019计蒜之道初赛4 B. 腾讯益智小游戏—矩形面积交(简单)(矩形交集)

    B. 腾讯益智小游戏—矩形面积交(简单) 1000ms 262144K   腾讯游戏开发了一款全新的编程类益智小游戏,最新推出的一个小游戏题目是关于矩形面积交的.聪明的你能解出来吗?看下面的题目接招吧 ...

  4. js转义字符

    \"   \/   \" 例: span = "<span onclick=\"xadmin.open('编辑','\/junyi\/member\/up ...

  5. ue4 创建简易动画

    这个功能本来是在原动画基础上做调整用的,所以直接用来做动画并不是很合适,如果要做复杂动画,叠加的轨迹会非常多,不好用 (蒙皮好的)模型右键,创建一个动画合成 点开这个动画合成,创建动画序列(就是普通动 ...

  6. java基础笔记(三)——main方法

    1.解析public static void main(String[] args)方法 JVM在运行程序时,会首先查找main()方法作为入口,main是JVM识别的特殊方法名. public是权限 ...

  7. CF620E New Year Tree 状压+线段树(+dfs序?)

    借用学长的活:60种颜色是突破口(我咋不知道QAQ) 好像这几道都是线段树+dfs序??于是你可以把60种颜色压进一个long long 里,然后向上合并的时候与一下(太妙了~) 所以记得开long ...

  8. 浅谈最近公共祖先(LCA)

    LCA(Least Common Ancestors),即最近公共祖先,是指在有根树中,找出某两个结点u和v最近的公共祖先. (来自百度百科) 一.倍增求LCA 预处理出距点u距离为2^0,2^1,2 ...

  9. HDU-1068-GirlsandBoys(最大独立集,二分图匹配)

    链接:https://vjudge.net/problem/HDU-1068#author=0 题意: 学校对n个学生(男女都有)进行的调查了,发现了某些学生暗生情愫,现在需要你选出一个最大的集合,这 ...

  10. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...