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. sql web Admin 源码.net 升级

    http://www.cnblogs.com/foundation/archive/2008/10/07/1305297.html

  2. POJ-3069

    Saruman's Army Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10994   Accepted: 5555 D ...

  3. 如何增加新的PointT类型

    博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=286 为了增加新的point类型,首先需要进行定义,例如: struct M ...

  4. day1 java基础回顾-泛型

    2.泛型(Generic) 当集合中存储的对象类型不同时,那么会导致程序在运行的时候的转型异常 1 import java.util.ArrayList; 2 import java.util.Ite ...

  5. 洛谷 - P3952 - 时间复杂度 - 模拟

    https://www.luogu.org/problemnew/show/P3952 这个模拟,注意每次进入循环的时候把新状态全部入栈,退出循环的时候就退栈. 第一次就错在发现ERR退出太及时,把剩 ...

  6. [openjudge] 1455:An Easy Problem 贪心

    描述As we known, data stored in the computers is in binary form. The problem we discuss now is about t ...

  7. 洛谷P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower

    P2687 [USACO4.3]逢低吸纳Buy Low, Buy Lower 题目描述 “逢低吸纳”是炒股的一条成功秘诀.如果你想成为一个成功的投资者,就要遵守这条秘诀: "逢低吸纳,越低越 ...

  8. IT兄弟连 JavaWeb教程 使用AJAX发送GET请求并获取响应

    GET请求用于获取数据,有时候我们需要获取的数据需要通过"查询参数"进行定位,在这种情况下,我们会将查询参数追加到URL的末尾,令服务器解析. 使用Ajax发送GET请求非常简单, ...

  9. (三)siege的使用

    学习: ELK——http://dockone.io/article/3655 docker——http://www.testclass.net/docker/ Android Monkey压力测试— ...

  10. ajax上传文件及nodeJS接收

    ajax文件上传需要用到FormData 官方介绍 FormData对象用以将数据编译成键值对,以便用XMLHttpRequest来发送数据.其主要用于发送表单数据,但亦可用于发送带键数据(keyed ...