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

  • Home
  • SEARCH
  • 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 8979169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:47:14+00:00 2026-06-15T19:47:14+00:00

I need your help. Following problem in Objective-C: // Robot.h @protocol RobotProtocol <NSObject> -(void)doWork;

  • 0

I need your help. Following problem in Objective-C:

// Robot.h
@protocol RobotProtocol <NSObject>
    -(void)doWork;
@end

@interface Robot : NSObject 


// Rob1 sublass of class Robot
// rob1.h
@interface Rob1 : Robot <RobotProtocol>

// rob1.m
@implementation
-(void)doWork
{
    // print 'robot 1'
}


// Rob2 sublass of class Robot
// rob2.h
@interface Rob2 : Robot <RobotProtocol>

// rob2.m
@implementation
-(void)doWork
{
    // print 'robot 2'
}



// Task.h
@interface Task : NSObject
{
    Robot *rob;
}


// Task.m
@implementation
- (id)init
{
    if ([super init]) {
        rob = [[Rob1 alloc] init]; // or Rob2 !!
    }
    return self;
}

-(void)doSomething
{
    [rob doWork]; // how to make sure, that this is implemented, depending on Rob1 or Rob2
}

How should Robot and its subclasses be implemented, that Robot *rob can be one of the subclasses of Robot rob1, rob2, ... and the method doWork:(BOOL)val; can be called? My first idea was to implement Robot as an abstract class, but unfortunately there are no abstract classes in Objective-C…

At the moment I am using a protocol, but I am not confident. Because it is not sure, that doWork is implemented, the compiler complains about

‘Robot’ may not respond to ‘doWork’

Thank you for your ideas.

  • 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-15T19:47:15+00:00Added an answer on June 15, 2026 at 7:47 pm

    Protocols should work.

    @protocol RobotProtocol <NSObject>
    @required
    - (void)doWork:(BOOL)flag;
    @end
    
    @interface Robot1 : NSObject <RobotProtocol>
    @end
    
    @implementation Robot1
    - (void)doWork:(BOOL)flag
    {
    }
    @end
    

    Unit Test for Robot1 called though id<RobotProtocol>

    - (void)testRobot
    {
        id<RobotProtocol> robot = [[Robot1 alloc] init];
        [robot doWork:YES];
    }
    

    Update

    After looking at your code, @interface Robot : NSObject should be @interface Robot : NSObject <RobotProtocol>. The thing is you don’t need @interface Robot : NSObject <RobotProtocol> at all. You can just use id<RobotProtocol>. This is the name of your abstract class.

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

Sidebar

Related Questions

I need your help in solving the following problem. Column1 shows a grouping by
I am a naive numpy user, need your help for the following problem: I
I need your help in solving the following problem: There're 3 girls (Ann,Susan,Alice) who
I'd really like your help with the following problem. I need to read N
Hello I need your help on the following problem. I am parsing the file
I really need your help since I have a frustrating problem. I'm downloading data
I need your help in solving the following 2 functions/problems: 1) I have to
I have a little problem and I need your help with it. I'm using
I need your help to sort out one problem with session timeout in my
I need your help with following matter. I want to read the parent tags

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.