Inheritance confusion


#1

Hello I am reading the chapter 18 and I have a confusion because i don’t understand the relation between the object and the parent class , we know all that the object is something real and exists in the memory after allocation and the class is a plan how can the object communicate with the parent class while it’s running ? comunicate with the class that you didn’t make an object of it ! I wanna understand what’s happens in the memory and the subclass contain the methods of the parent physicly in the memory after allocation or not , how the inheritance works , the subclass include the methods and instance variables ? for example let’s take the example of the person and the employee , when we make [[employee alloc]init] what happens ? did that create a person object or a employee object or both of them and make a relation between them ? and if it’s a employee object how it knows the information about the instance variable of the person while we don’t have an object person allocated in memory ? , so I need to know the communication and the position of the class Person with the object employee hope you understand what I am asking for ! thank you in advance for your answer

Annonces en Tunisie


#2

When an object (an instance of a class) is created, an instance of its super class is also created. But this happens in reverse: the instance of super class is created first then the instance of the sub class.

Use the notation Foo: Bar to designate that Bar is the super class of Foo or Foo is a sub class of Bar.

Now assume that you have the class hierarchy:
B: A
C: B
D: C

If you create an instance of D, then:
First an instance of A is created;
then instance of B is created;
then instance of C is created;
finally instance of D is created;

This is because D needs C which needs B which needs A.

When the instance of D is destroyed, destruction takes place in the opposite order: instance of D first, …, instance of A last.


#3

really, thank you very much , it’s helpful , know I can understand what’s happening while working with inheritance :slight_smile:

Annonces en Tunisie


#4

In an attempt to understand inheritance as well as how dealloc & description fit into the picture from Chapter 19, I wrote the following example.

I created a Circle class that inherits from NSObeject and a Wheel class that inherits from Circle
NSObject > Circle > Wheel

Circle.h

#import <Foundation/Foundation.h>

@interface Circle : NSObject
{
    float radius;
    float diameter;
    
    float circumference;
    float area;
}

@property float radius;

- (float)diameter;
- (float)circumference;
- (float)area;

@end

Circle.m

#import "Circle.h"

@implementation Circle

@synthesize radius;

- (float)diameter
{
    diameter = radius * 2;
    return diameter;
}

- (float)circumference
{
    circumference = M_PI * diameter;
    return circumference;
}

- (float)area
{
    area = M_PI * radius * radius;
    return area;
}

// Override the description method of NSObject
- (NSString *)description
{
    return [NSString stringWithFormat:@"Circle (%.2f, %.2f, %.2f, %.2f)\n\n", [self radius], [self diameter], [self circumference], [self area]];
}
// Override the dealloc method of NSObject
- (void)dealloc
{
    NSLog(@"\n\n\n--- REMOVING FROM MEMORY ---\n%@", self);
}

@end

Wheel.h

#import <Foundation/Foundation.h>
#import "Circle.h"

@interface Wheel : Circle
{
    NSString *type;
    NSString *brand;
    int airPressure;
}

@property NSString *type;
@property NSString *brand;
@property int airPressure;

@end

Wheel.m

#import "Wheel.h"

@implementation Wheel

@synthesize type, brand, airPressure;

//--- Overrite the description method from the Circle class
- (NSString *)description
{
    return [NSString stringWithFormat:@"Wheel (%@, %@, %d, | %f, %f, %f, %f)\n\n", [self type], [self brand], [self airPressure], [self radius], [self diameter], [self circumference], [self area]];
}

// Override the dealloc method from the Circle class

- (void)dealloc
{
    NSLog(@"\n\n\n### REMOVING FROM MEMORY ###\n%@", self);
}

@end

main.m

#import <Foundation/Foundation.h>
//#import "Circle.h"
#import "Wheel.h"


int main(int argc, const char * argv[])
{
    @autoreleasepool {     
  
        Wheel *myTire = [[Wheel alloc] init];
        [myTire setRadius:(9)];
        [myTire setType:(@"Bike Tire")];
        [myTire setBrand:(@"Schwin")];
        [myTire setAirPressure:(3.4)];
        
        NSLog(@"\nMy Tire is: %@", myTire);
        
        
        Wheel *myOtherTire = [[Wheel alloc] init];

        [myOtherTire setRadius:(14)];
        [myOtherTire setType:(@"Truck Tire")];
        [myOtherTire setBrand:(@"Good Year")];
        [myOtherTire setAirPressure:(5.2)];
        
        NSLog(@"\nMy other tire is: %@", myOtherTire);
    }
    return 0;
}

OUTPUT

2012-11-02 11:43:36.782 Playing[21727:303] 
My Tire is: Wheel (Bike Tire, Schwin, 3, | 9.000000, 18.000000, 56.548668, 254.469009)

2012-11-02 11:43:36.782 Playing[21727:303] 
My other tire is: Wheel (Truck Tire, Good Year, 5, | 14.000000, 28.000000, 87.964592, 615.752136)

2012-11-02 11:43:36.783 Playing[21727:303] 


### REMOVING FROM MEMORY ###
Wheel (Truck Tire, Good Year, 5, | 14.000000, 28.000000, 87.964592, 615.752136)

2012-11-02 11:43:36.783 Playing[21727:303] 


--- REMOVING FROM MEMORY ---
Wheel (Truck Tire, Good Year, 5, | 14.000000, 28.000000, 87.964592, 615.752136)

2012-11-02 11:43:36.783 Playing[21727:303] 


### REMOVING FROM MEMORY ###
Wheel (Bike Tire, Schwin, 3, | 9.000000, 18.000000, 56.548668, 254.469009)

2012-11-02 11:43:36.784 Playing[21727:303] 


--- REMOVING FROM MEMORY ---
Wheel (Bike Tire, Schwin, 3, | 9.000000, 18.000000, 56.548668, 254.469009)

2012-11-02 11:43:36.784 Playing[21727:303] 


--- REMOVING FROM MEMORY ---
Circle (24.00, 48.00, 150.80, 1809.56)

2012-11-02 11:43:36.785 Playing[21727:303] 


--- REMOVING FROM MEMORY ---
Circle (4.00, 8.00, 25.13, 50.27)

As you can see, my dealloc method is firing when my Wheel object is removed from memory. This causes the description for that class to be displayed. Then because Wheel is based on Circle, the Circle dealloc method is called, but some how then, the description for wheel is fired again? I would assume it would be the description for Circle that would get fired, not the description for Wheel.

Can someone explain why I’m receiving this output?

Thank you.


#5

self always refers to the object created from a class, regardless of where self appears in the methods in the class hierarch.

Assume B inherits from A, and C from B:
A, B : A, C : B

Create a C:
C *object = [C create];

Occurrences of self in A and B always refer to the instance of C, the object.


#6

Thank you for the response.

It seems odd that A and B’s self would refer to the instance of C.

Is there a way to have A and B’s self refer to A and B respectively? Can we use super self?