Classes, Objects, and Methods


Long-Term Learning

Learn efficiently and remember over time.

Start Long-Term Learning

Get personalized study reminders at intervals optimized for better retention.
Track your progress on this set by creating a folder
Or add to an existing folder

Add this set to a folder

  • Object

    A thing.

    An instance of a car

    Your particular car

    The name of the class


    Each time a new car is manufactured, a new instance from the class of car is created, and each instance of the car is referred to as an object.

    object oriented programming example


    A unique occurrence of a class


    An action that is performed on an instance of a class or to the class itself


    Applying a method to an object will affect the state of that object

    Applying methods to classes or instances

    [ ClassOrInstance method ];
    [ receiver message ];

    How to get a new car

    yourCar = [ Car new ];

    You send a NEW message to the car class (the receiver of the message) asking it to give you a new car. The resulting object (which represents your unique car) is then stored in the variable yourCar. From now on, yourCar can be used to refer to your instance of the car.

    Factory or class methods

    The method new is an example of a class method because you have to go to the factory to get a new Car.

    instance methods

    Methods that apply only to your car

    Applying the same methods to different objects is one of the key concepts of object-oriented programming


    A method that takes an argument

    [ yourCar setSpeed: 55 ];

    set the speed to 55 mph

    The @interface section

    Describes the class and its methods

    The @implementation section

    Describes the data (the instance variables that objects from a class will store) and contains the actual code that implements the methods declared in the interface section.

    The program section

    Contains the program code to carry out the intended purpose of the program.

    You can also declare the instance variables for a class in the interface section. This is considered a better way to define a class.


    The @ interface section details

    1. You have to define where the class came from when you define a new class. That is, you have to name it's parent class.

    2. You need to define the type of operations, or methods, that can be used when working with objects from this class.

    3. You also need to list items known as properties

    General format for the @interface section

    @interface NewClassName: ParentClassName

    Class names should begin with a capital letter


    Rules for forming names

    They must begin with a letter or underscore (_) and they can be followed by any combination of letters (uppercase or lowercase), underscores, or the digits 0 through 9.

    When deciding on a name, don't be lazy. Pick names that reflect the intended use of the object or variable.


    leading (-) sign

    indicates an instance method

    leading (+) sign

    indicates a class method

    Return Values

    - (int) returns an integer
    - (void) returns no value
    - (double) returns a decimal

    Tells the program what type of value the method returns

    A value is returned from a method using the return statement.


    return 0;

    Specifying an integer argument

    - (void) setNumerator: (int) n;

    Integer arguments are indicated by the (int) in front of the argument name.

    When a method name ends with a colon, it means that it is expecting to see an argument.

    - (void) setNumerator: (int) n;

    The @implementation section in detail

    You have to describe the data that members of the class will contain. These members are called instance variables. You declare the methods in the @interface section and then you define them in the @implementation section.

    The @implementation section general format

    @implementation NewClassName

    NewClassName is the same name that was used for the class in the @interface section.

    The memberDeclarations section specifies what types of data are stored in your instance of the class. It is enclosed inside its own curly braces

    int numerator;
    int denominator;

    The members declared in this section are known as instance variables. Each time you create a new object, a new and unique set of instance variables are also created.

    The methodDefinition part of the @implementation section contains the code for each method specified in the @interface section.

    Each method starts by identifying the type of the method (class or instance; + or -), its return type, and its arguments and their types. Instead of the line ending in a semicolon, the code for the method follows, enclosed in a set of curly braces.

    -(void) print


    automatically generates methods for you

    The program section in detail

    This section contains the code to solve your particular problem, which can be spread out across many files. Somewhere you must have a routine called main. That's where your program begins execution.

    Create an object to store a fraction

    Fraction *myFraction;

    Create the object (ask the factory to build a car). This zeros out all of the object's instance variables.

    myFraction = [Fraction alloc];

    Initialize the object

    myFaction = [myFraction init];

    Note that you are not sending the init method to a class, you are sending it to the instance of the class

    The init method also returns a value - namely, the initialized object. You store the return value in your variable myFraction


    Two-line sequence of allocating a new instance of a class and initializing it

    myFraction = [[Fraction alloc] init]

    Three-line sequence (final shorthand)

    Fraction *myFraction = [[Fraction alloc] init]

    Set the value of your fraction

    // Set fraction to 1/3

    [myFraction setNumerator: 1];
    [myFraction setDenominator: 3];

    The first message statement sends the setNumerator: message to myFraction. The argument that is supplied is the value 1.

    Inside the setNumerator: method, the passed value of 1 is stored inside the variable n. The single program line in that method effectively stores that value in the instance variable numerator.

    Display the value of myFraction

    NSLog (@"The value of myFraction is: ");
    [myFraction print]

    invokes the print method

    In the past, iOS programmers were responsible for telling the system when they were done using an object that they allocated by sending the object a release message. That was done in accordance to the memory management system known as manual reference counting. As of Xcode 4.2, programmers no longer have to worry about this and can rely on the system to take care of releasing memory as necessary. This is done through a mechanism known as Automatic Reference Counting (ARC). ARC is enabled by default.


    The ultimate goal for objects

    Make your programs easier to write, code, and maintain.

    Fraction *myFraction

    The asterisk represents a reference (or pointer) to a object. The variable myFraction doesn't actually store the fraction's data. Instead it stores a reference - which is a memory address - indicating where the object's data is located in memory. You can think of it as an empty box.

    Allocating a new object (using alloc)

    enough space is reserved in memory to store the object's data, which includes space for its instance variables, and a little more. The location where the data is stored is returned by the alloc routine, and assigned to myFraction.

    More data is stored with the object than just that indicated, but you don't need to worry about that here. You'll note that the instance variables are shown as being set to 0.


    Please allow access to your computer’s microphone to use Voice Recording.

    Having trouble? Click here for help.

    We can’t access your microphone!

    Click the icon above to update your browser permissions above and try again


    Reload the page to try again!


    Press Cmd-0 to reset your zoom

    Press Ctrl-0 to reset your zoom

    It looks like your browser might be zoomed in or out. Your browser needs to be zoomed to a normal size to record audio.

    Please upgrade Flash or install Chrome
    to use Voice Recording.

    For more help, see our troubleshooting page.

    Your microphone is muted

    For help fixing this issue, see this FAQ.

    Star this term

    You can study starred terms together

    NEW! Voice Recording

    This is a Plus feature