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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:45:22+00:00 2026-06-14T04:45:22+00:00

Actually I am from java background and I am learning objective c.I am very

  • 0

Actually I am from java background and I am learning objective c.I am very confused about strange behaviour of objective C.”Please Read 3rd Question its important one.”

Questions are provided in sequence so please give answers in sequence as its understandable to me and others.

Question 1
I have two classes derived from NSObject: A and B:

@interface A : NSObject 
@end                               

@interface B : NSobject
-(void)display; // It displays "I am class B"
@end

Now if I do this:

A *a = [[B alloc]init]; // Show warning not error (it must be illegal)
[a display];            // prints "I am class B"

It calls the display method of class B. I don’t think that it should happen because:

  1. A doesn’t have the method display. By polymorphism.

  2. This could be a security threat as I am creating reference of any class and passing object of any another class and accessing data by it.

  3. There could be design issues as Dog class instance gets an object of Printer class and now i am calling print method on Dog instance.

  4. I have reference of NSArray and passed object of NSMutableArray and now i am calling NSMutableArray method on this instance.

    [nsarr addObject:@:abc]; //Looking very odd

Question 2
If I have Foo protocol and if any class is not confirming it. It should not be allowed to get object of that class in protocol reference.

@protocol Foo
@required
-(void)abc;
@end    

If i call:

id<Foo> obj= [[B alloc]init]; // Shows warning ignore it for now as it must be illegal also
[obj display];                // It will call display method which should be illegal  
  1. It should not happen, as B is not conforming to protocol Foo and obj is taking B object and calling B instance method. I think its very bad because of polymorphism and security

Question 3
If my class has a class method which returns an object of that class which is not autoreleased, the compiler shows warning. If I pass the object returned by that class (not conforming protocol) method to reference of protocol. (IT SHOULD BE AN ERROR).

id<Foo> obj = [Abc aClassMethodReturnsObjectWhichNotAutoreleased]; //show warning

It shows a warning which is good. Abc did not conform to the protocol Foo

BUT

id<Foo> obj = [NSArray arrayWithObjects:@"abc",@"def",nil]; // It does **not** show a warning as it will return autorelease object. NSArray doesn't conform protocol Foo

Why does the above assignment to the NSArray class not show a warning as it is showing in the previous example.

Thanks in advance.

EDIT

*Answer 3rd Question:*As NSArray returns id object which will allow to pass in “id obj” but in “aClassMethodReturnsObjectWhichNotAutoreleased” case the method returns “ABC *” pointer so that is why compiler giving warning in this case.

  • 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-14T04:45:23+00:00Added an answer on June 14, 2026 at 4:45 am

    Question 1:

    A *a = [[B alloc]init]; //Show warning not error (it must be illegal)
    [a display];  //prints "I am class B"
    

    Here you are using a static type A for the variable named a. You are then assigning a different type of object (B) to the variable.

    Unlike java, Objective-C does not enforce the static typing requirement, however it does warn you when it is being compiled since the compiler detected a difference between the declared type and the actual type of the object. It happily stuffs the B object into your variable though, so a is now pointing to the B object that you created. Once the program is compiled and running (at run-time), A *a is treated the same as id a.

    Another feature of Objective-C is that you can send any message to any object at any time. This is part of the dynamic nature of Objective-C. Obviously there are cases where sending the wrong message to an object can cause bad things ™ to happen so you need to ensure that you only send appropriate messages. There are various functions that can test the class of an object at run-time, or even test to see if it is able to handle a particular message before you send it in order to prevent the bad things. If you are using static typing (like in this example) then the compiler will issue warnings to tell you that you may have made a mistake and should review the code.

    Question 2:

    This is actually very similar to question 1. The compiler is warning you that you are assigning what appears to be an incorrect value to the variable, however at run-time you can send any message to any object, so it will work on the actual object instead of the “expected” object from the type declaration.

    Question 3:

    Good question. I would have thought that you would get a warning there too. Maybe someone else can help out on that. My first thought is that this is a bug and should be reported as such, but there may be a reason for it that I’m not aware of….

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

Sidebar

Related Questions

I'm from a very much C/Java type background. I'm used to text maybe being
I've always had a weird feeling in my gut about actually deleting rows from
Actually in one of my project i need to read images from remote server
I'm actually reposting a question from the iphone development boards. I didn't ask it,
I am new to java for android and come from an actionscript 3.0 background.
I come from a C# and Java background into C++ and I'm trying to
I’m from a .NET background and now dabbling in Java. Currently, I’m having big
I'm relatively new to C#, as my background is actually java. I'm creating a
I have been playing around with node.js, and coming from a Java background, I
I want to find out more about NoSQL databases/data-stores available for use from Java,

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.