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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:11:15+00:00 2026-05-16T16:11:15+00:00

what are the Advantages of using protocols in Objective-C, when we require them? I

  • 0

what are the Advantages of using protocols in Objective-C, when we require them? I have read the objective-C programming guide it shows mainly 3 advantages:

  1. To declare methods that others are expected to implement.

  2. To declare the interface to an object while concealing its class

  3. To capture similarities among classes that are not hierarchically related.

Can any one explain me (2) and (3), I am not able to get it, as I have read details of them in Obj-C programming Guide? Are there any other advantages of using protocol? How can you describe why we need protocols 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-16T16:11:16+00:00Added an answer on May 16, 2026 at 4:11 pm

    2) To declare the interface to an object while concealing its class

    @protocol DatabaseConnection
        - (QueryResult*) runQuery:(NSString*)query;
        - (void) close;
    @end
    
    
    @interface MySQLConnection : NSObject <DatabaseConnection> {
        ...
    }
    
    @interface OracleConnection : NSObject <DatabaseConnection> {
        ...
    }
    
    // imagine a method on some class that will give you a connection
    
    - (DatabaseConnection*) getConnection {
        // some logic to determine which connection to return
        if (something)
            return [[[OracleConnection alloc] init] autorelease];
        else
            return [[[MySQLConnection alloc] init] autorelease];
    }
    

    and you using it like so in client code …

    id <DatabaseConnection> con = [SomeClass getConnection];
    QueryResult* result = [con runQuery:@"select * from users"];
    [con close];
    

    Notice how we are abstracted away from the actual database implementation and can run our query regardless of which database we’re talking with.

    The advantage of defining DatabaseConnection as a protocol as opposed to a base class is that we don’t have to provide a default implementation. Because in this case, the default implementation would not make sense. A protocol is Objective-C’s way of doing a purely abstract base class in C++ or an interface in Java.


    3) To capture similarities among classes that are not hierarchically related.

    You might want the ability to ask any object about which class it implements.

    - (Class)class;
    

    You might also want to know what its superclass is.

    - (Class)superclass;
    

    Perhaps you want to ask if any two objects are equal.

    - (BOOL)isEqual:(id)object;
    

    All of these things and more are methods you may want on classes such as File, MySQLConnection, User, MonsterX, MiniGun. Though these classes are entirely different from an inheritance point of view, which is why they can’t share a base class, a protocol addresses this need.

    For example, Objective-C has a base protocol called NSObject.

    Note: There will be some who point out that most Objective-C classes inherit from the NSObject class and that they get a number of these methods from there.

    This statement is true. But NSObject the class, implements NSObject the protocol. Apple recognized that there will be cases when your objects do not inherit from NSObject the class, but may still want to provide services such as responding to the description method.

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

Sidebar

Related Questions

I understand that one of the advantages of using stdio.h FILE over direct read()
What are the advantages of using NoSQL databases? I've read a lot about them
Possible Duplicate: Advantages of using prototype, vs defining methods straight in the constructor? What
What are the advantages of using a SQLDataReader as opposed to a reader that
Possible Duplicate: Advantages of using prototype, vs defining methods straight in the constructor? I'm
Possible Duplicate: Initializing in constructors, best practice? Advantages of using initializer list? I have
After using them a while I can't help but feel that the hoops you're
What are the advantages of using Asynchronous controllers in my ASP.NET MVC website? Please
What are the advantages of using listview over gridview? I need pagination, editing rows,
Are there any advantages of using one over the other besides readability?

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.