Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Inheritance in Objective C

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.59k
    Comment on it

    Inheritance in Objective C can be done by Subclassing. Here is an example of inheritance that works in objective-c.

     
    **In Rectangle.h**
    @interface Rectangle: NSObject
    
    @property (nonatomic) int width;
    @property (nonatomic) int height;
    
    -(Rectangle*) initWithWidth: (int) w height: (int) h;
    -(void) setWidth: (int) w height: (int) h;
    -(void) print;
    
    @end
    
    **In Rectangle.m**
    
    #import "Rectangle.h"
    #import <`stdio.h`>
    
    @implementation Rectangle
    
    @synthesize width = _width;
    @synthesize height = _height;
    
    -(Rectangle*) initWithWidth: (int) w height: (int) h {
        self = [super init];
        if ( self ) {
            [self setWidth: w height: h];
        }
        return self;
    }
    -(void) setWidth: (int) w height: (int) h {
        self.width = w;
        self.height = h;
    }
    -(void) print {
        printf( "width = %i, height = %i", self.width, self.height );
    }
    
    @end
    
    

    Inherit from Parent

    **In Square.h**
    
    @interface Square: Rectangle
    
    
    Square.h
    
    #import "Rectangle.h"
    
    @interface Square: Rectangle
    -(Square*) initWithSize: (int) s;
    -(void) setSize: (int) s;
    -(int) size;
    @end
    
    **In Square.m**
    
    #import "Square.h"
    
    @implementation Square
    -(Square*) initWithSize: (int) s {
        self = [super init];
    
        if ( self ) {
            [self setSize: s];
        }
    
        return self;
    }
    
    -(void) setSize: (int) s {
        self.width = s;
        self.height = s;
    }
    
    -(int) size {
        return self.width;
    }
    
    @end
    

    Import both files i.e, Rectangle.h and Square.h on top and write the below mentioned function *In your implementation .m file * where you want to use inheritance.

    - (void)inheritanceExample
    {
        Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
        Square *sq = [[Square alloc] initWithSize: 15];
    
        // print em
        printf( "Rectangle: " );
        [rec print];
        printf( "\n" );
    
        printf( "Square: " );
        [sq print];
        printf( "\n" );
    
        // update square
        [sq setWidth: 20];
        printf( "Square after change: " );
        [sq print];
        printf( "\n" );
    }
    
    @end
    

    *Happy Coding :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: