1 Replies - 1954 Views - Last Post: 24 December 2010 - 01:51 PM

#1 Ninjonxb  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 02-December 10

new to objective c, from c++ and java

Posted 24 December 2010 - 12:00 PM

I have a couple questions about objecive c comming from experience with c++ and java objects. I am trying to learn from a book and certain things seem backwards to me and I just want to make sure I understand this correctly.

First is with subclasses. It seems that in this language you make a new object of the most subclass you want to use instead of the parent class? In java you make an object of the parent class than that class calls the subclasses when needed. Maybe I have just not gotten to this point in objective c and it is just possible to do it both ways?

Also, putting the import the book is having me import the .h file (interface) in the main file of the program instead of the .m file for the class. the .m file imports the .h file, not the other way around. Why does this work? Is it just how the language works that i don't necessarily have to specifically import certain files?


Is This A Good Question/Topic? 0
  • +

Replies To: new to objective c, from c++ and java

#2 GWatt  Icon User is offline

  • member icon

Reputation: 307
  • View blog
  • Posts: 3,105
  • Joined: 01-December 05

Re: new to objective c, from c++ and java

Posted 24 December 2010 - 01:51 PM

With any OO language (like Objective C or Java) you should declare an instance as the most specific applicable type in the class hierarchy. For example, if you want a GUI object, like a button, you don't declare the object to of type NSObject (or Object in Java.) You declare an instance of the NSButton class (or JButton if you're using Java swing.) You can declare everything to be NSObject instances and initialize them as whatever, but I can't think of a good reason to do that.

Header/Source files:
Class declarations are put in separate files from their source code. The .h file contains the declarations and the .m file contains the definition/implementation. #import <filename> tells the compiler to use the declarations in a given file. It's similar to #include <filename>, except that #import only looks in the file once, whereas #include will look in that file as many time as the #include <filename> is used. #import is effectively the same as using #pragma once or using the#ifdef header guards in C/C++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1