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 877149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T11:33:30+00:00 2026-05-15T11:33:30+00:00

This is probably a common Objective-C question reported by Java coders, but I don’t

  • 0

This is probably a common Objective-C question reported by Java coders, but I don’t know what to call it or how to search for the answer. Let’s say I have a class and another class which extends it:

AbstractModel

@interface AbstractModel {
}

ModelImpl

@interface ModelImpl : AbstractModel {
}

Separate from these, I have two more classes, again one extending the other:

ControllerA

@interface ControllerA {
    AbstractModel *foo;
}

@property (nonatomic, retain) AbstractModel *foo;

ControllerB

@interface ControllerB : ControllerA {
}

I want to be able to say that foo in ControllerA can contain an AbstractModel or any of its subtypes. However, the compiler gives me a warning if I attempt to store anything other than an AbstractModel in it. (Of course I understand that classes can’t really be abstract in ObjC, but have mercy on me.)

I would also like to be able to “lock down” the foo property in specific subclasses. I would like to say that foo in ControllerB can contain only a ModelImpl4 for example. Is this possible?

What is the conventional Objective-C best practice for solving this type of problem? Is using inheritance in this way — or to achieve this goal — just not a good idea in Objective-C?

  • 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-05-15T11:33:31+00:00Added an answer on May 15, 2026 at 11:33 am

    First, I want to understand this:

    However, the compiler gives me a
    warning if I attempt to store anything
    other than an AbstractModel in it.

    This doesn’t make sense. You should be able to assign sub-classes of AbstractModel to foo without trouble. What problem are you seeing?

    Next, what you’re describing is not overriding, it’s overloading. You’re trying to change the return type of the method, and you cannot do that in ObjC. There are very good solutions to this problem, but it somewhat depends on what your real goal is.

    • First, you can get rid of -foo in ControllerA. If ControllerA is actually abstract, then it perhaps is better not to have one. If ControllerA is abstract, I definitely recommend that you get rid of the foo ivar at that layer. You should put the ivars in the subclasses.

    • Alternately, you can add typed methods to the subclasses. For instance, ControllerB would have a -modelBFoo method in addition to -foo that it inherits. These methods would be identical; they would just have different return types, allowing good typing in all callers.

    Do not ignore warnings. They’re there to protect you (and in ObjC, they’re about all you have to protect you). Limit your typecasting as much as you can. They move compiler errors (good) to run-time exceptions (bad).

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

Sidebar

Ask A Question

Stats

  • Questions 441k
  • Answers 441k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer gettimeofday is thread safe. The (posix) functions listed here might… May 15, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer perl is not a valid command inside a Perl script.… May 15, 2026 at 5:38 pm
  • Editorial Team
    Editorial Team added an answer The DragMove function do not allow you to drag a… May 15, 2026 at 5:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.