Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9311449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:22:05+00:00 2026-06-19T01:22:05+00:00

I had an exercise to override init method, so I need to create an

  • 0

I had an exercise to override init method, so I need to create an init method that will set some attributes as well.

My question is: why do I need to define the original init method as well? in case that the new init method won’t work?

This is my .h file:

#import <Foundation/Foundation.h>

#import "XYPoint.h"

@interface Rectangle: NSObject

@property float width, height, tx, ty;

-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
-(void) translate: (XYPoint *) point;
-(id) initWithWidth:(int) w andHeight:(int) h;
-(id) init;

@end

And .m (only the init methods):

-(id) initWithWidth:(int)w andHeight:(int)h
{
    self = [super init];

    if (self)
    {
        [self setWidth:w andHeight:h];
    }

    return self;
}

-(id) init
{
    return [self initWithWidth:0 andHeight:0];
}

I know that it is good this way, but if someone can explain me why is that I would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T01:22:06+00:00Added an answer on June 19, 2026 at 1:22 am

    The idea is to have a central point of initialization for your object instead of sprinkling the initialization of variables throughout each init method.

    Your particular example doesn’t do much justice to this pattern because you’re initializing a Rectangle with 0 width and 0 height, and the default NSObject implementation resets the memory for all instance variables to zero by default, and your initWithWidth:andHeight: method does the same. However, suppose that, you were allocating unit rectangles (width 1, height 1) by default when a Rectangle object was created using,

    [[Rectangle alloc] init]
    

    then instead of doing this,

    - (id)initWithWidth:(int)width andHeight:(int)height {
        self = [super init];
        if (self) {
            [self setWidth:width andHeight:height];
        }
        return self;
    }
    
    - (id)init {
        self = [super init];
        if (self) {
            [self setWidth:1 andHeight:1];
        }
        return self.
    }
    

    you’re centralizing the point of initialization by just doing,

    - (id)initWithWidth:(int)width andHeight:(int)height {
        self = [super init];
        if (self) {
            [self setWidth:w andHeight:h];
        }
        return self;
    }
    
    - (id)init {
        return [self initWithWidth:1 andHeight:1];
    }
    

    This also goes hand in hand with the principle of DRY a.k.a. Don’t Repeat Yourself.

    This is a trivial example, however, in most real-world objects, you might have a much more complicated setup with notification registration, KVO registration, etc., and then it becomes absolutely crucial that your centralize all that initialization logic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I had a debate about macros and their readability. I think that in some
This is an academical exercise (disclaimer). I'm building an application that will profit from
I'm reading CLRS and had some problem with the exercise 6.5-8. Give an O(n
As an exercise for my Java course in Uni this morning, I had to
Had a page that was working fine. Only change I made was to add
Had a good search here but can't see anything that gets my mind in
I had a calculator app that can draw a function. I'm stuck in an
I'm working through a book exercise to generate some random serial number, and here's
Currently I'm learning C++ in my book and they had an exercise on using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.