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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:54:51+00:00 2026-05-27T01:54:51+00:00

Class Child extends Parent. Parent implements protocol C which has optional methods, including -(void)d

  • 0

Class Child extends Parent. Parent implements protocol C which has optional methods, including -(void)d. Child has an implementation of -d; should it invoke [super d]?

In other words, what code do I write to invoke [super d] if and only if something will respond to it? Assume that I do not control the implementation of Parent; it may change at any time.

Here are all the ways I have thought of. I am currently using number 4.

Apparently sensible answer 1:

[super d]; // Delete this line if a runtime exception occurs when you try it

This does not work because Parent might implement -d dynamically so this works when you test it and not in the field. Or the implementation of Parent could change so that the result of this test is no longer correct.

Apparently sensible answer 2:

if ([super respondsToSelector:_cmd])
    [super d];

This does not work, because NSObject’s implementation of -respondsToSelector will find the implementation in Child and return YES in all cases.

Apparently sensible answer 3:

if ([[self superclass] instancesRespondToSelector:_cmd])
    [super d];

This works if and only if the superclass knows it always implements -d; if instances dynamically determine whether this method is present this technique will not work. Better than 1 in that it will pick up static changes to the implementation of Parent at runtime.

Apparently sensible answer 4:

@try
{
    [super d];
}
@catch (NSException *exception)
{
    NSString *templateReason = [NSString stringWithFormat:
                                @"-[%@ %@]: unrecognized selector sent to instance %p"
                                ,NSStringFromClass([self superclass])
                                ,NSStringFromSelector(_cmd)
                                ,self];
    if (![exception.reason isEqualToString:templateReason])
        @throw exception;
}

Performance of this is poor if the method in the superclass does not exist because computing templateReason and then comparing it to the exception reason is expensive.

This mechanism is fragile because the format of the exception reason string in this case could be altered in a future SDK or runtime release.

  • 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-27T01:54:51+00:00Added an answer on May 27, 2026 at 1:54 am

    None of these things are necessary.

    If you are subclassing some class or other, you already need to know if you are replacing or supplementing behavior.

    In other words, if the implementation exists and you want it done differently, you don’t call super.

    If the implementation doesn’t exist, you don’t call super.

    If the implementation does exist, but you want to supplement it, you call super.

    Addendum:

    Wether the implementation can change at any time isn’t relevant for your question; what is important is if the interface changes.

    If the interface is constantly changing, odds are good the class is an exceptionally poor candidate for subclassing, or even use.

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

Sidebar

Related Questions

If I have child class,the child class inherits all methods from the parent,but how
Assume i have: @Inheritance(strategy = InheritanceType.JOINED) public class Child extends Parent{ } How can
I've a code: class Parent{ int x=10; void show(){ System.out.println(x); } } class Child
I have two classes Parent and Child . class Child extends Parent { private
class Test { import scala.collection._ class Parent class Child extends Parent implicit val children
Consider these classes: class Parent { int a; } class Child extends Parent {
Example models: class Parent(models.Model): name = models.CharField() def __unicode__(self): return self.name class Child(models.Model): parent
I have a parent and child class that both need to implement IDisposable .
I have a parent class and child class (inherited from parent). In the child
I jave the following mapped superclass that provides a basic implementation for a parent/child

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.