Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a class to inherit properties and methods from another class. This promotes code reuse and establishes a natural hierarchy between classes.

Key Concepts

  1. Base Class (Superclass): The class whose properties and methods are inherited.
  2. Derived Class (Subclass): The class that inherits from the base class.
  3. Inheritance Syntax: In Objective-C, inheritance is denoted using a colon (:) in the class declaration.

Why Use Inheritance?

  • Code Reusability: Avoids redundancy by reusing existing code.
  • Hierarchical Classification: Establishes a natural hierarchy between classes.
  • Extensibility: Allows for extending the functionality of existing classes without modifying them.

Syntax

In Objective-C, you define a subclass by specifying the superclass after a colon in the interface declaration.

@interface SubclassName : SuperclassName
// Subclass properties and methods
@end

Example

Let's create a simple example to illustrate inheritance. We'll define a base class Person and a derived class Student.

Base Class: Person

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property NSString *name;
@property NSInteger age;

- (void)displayInfo;

@end

@implementation Person

- (void)displayInfo {
    NSLog(@"Name: %@, Age: %ld", self.name, (long)self.age);
}

@end

Derived Class: Student

#import "Person.h"

@interface Student : Person

@property NSString *studentID;

- (void)displayStudentInfo;

@end

@implementation Student

- (void)displayStudentInfo {
    [self displayInfo]; // Call the method from the superclass
    NSLog(@"Student ID: %@", self.studentID);
}

@end

Using the Classes

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Student *student = [[Student alloc] init];
        student.name = @"John Doe";
        student.age = 20;
        student.studentID = @"S12345";
        
        [student displayStudentInfo];
    }
    return 0;
}

Output

Name: John Doe, Age: 20
Student ID: S12345

Practical Exercise

Exercise

  1. Create a base class Vehicle with properties make and model, and a method displayInfo.
  2. Create a derived class Car that inherits from Vehicle and adds a property numberOfDoors.
  3. Implement a method displayCarInfo in the Car class that calls displayInfo from the Vehicle class and also displays the number of doors.

Solution

Base Class: Vehicle

#import <Foundation/Foundation.h>

@interface Vehicle : NSObject

@property NSString *make;
@property NSString *model;

- (void)displayInfo;

@end

@implementation Vehicle

- (void)displayInfo {
    NSLog(@"Make: %@, Model: %@", self.make, self.model);
}

@end

Derived Class: Car

#import "Vehicle.h"

@interface Car : Vehicle

@property NSInteger numberOfDoors;

- (void)displayCarInfo;

@end

@implementation Car

- (void)displayCarInfo {
    [self displayInfo]; // Call the method from the superclass
    NSLog(@"Number of Doors: %ld", (long)self.numberOfDoors);
}

@end

Using the Classes

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Car *car = [[Car alloc] init];
        car.make = @"Toyota";
        car.model = @"Corolla";
        car.numberOfDoors = 4;
        
        [car displayCarInfo];
    }
    return 0;
}

Output

Make: Toyota, Model: Corolla
Number of Doors: 4

Common Mistakes

  • Forgetting to Call Superclass Methods: Always ensure that you call the superclass methods when overriding them if you want to retain the base functionality.
  • Incorrect Property Access: Ensure that properties are correctly accessed and modified using self.propertyName.

Summary

Inheritance in Objective-C allows you to create a new class based on an existing class, promoting code reuse and a hierarchical structure. By understanding and utilizing inheritance, you can create more organized and maintainable code. In the next section, we will explore polymorphism, another key concept in OOP.

© Copyright 2024. All rights reserved