Clases, Objects, Methods and Instances Explanation

Discussion in 'Mac Programming' started by CrazyIllini, Dec 21, 2009.

  1. CrazyIllini macrumors member

    Joined:
    Oct 8, 2009
    #1
    So I have no programming experience and am reading a book (not taking a class) on Objective-C. In one of the first chapters they try to explain the terms clases, objects, methods and instances. I feel like I don't fully understand what they are trying to say and I would like another explanation.

    And I DID check Mroogle.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Which book? Be specific.

    Which parts of the explanation didn't you understand? Be specific.

    When asking programming questions, it's usually better to be specific instead of general. For example, saying exactly which book, saying exactly what you didn't understand (even quoting the text instead of paraphrasing it), or posting actual code instead of posting a description of it.

    http://www.mikeash.com/getting_answers.html
     
  3. powerbuddy macrumors 6502

    Joined:
    Jun 20, 2006
    #3
    Check out the java versions of classes and objects. The concept of OOP should be very similar.
     
  4. Compile 'em all macrumors 601

    Compile 'em all

    Joined:
    Apr 6, 2005
    #4
    Get a software engineering book that talks about OO and things like encapsulation and data hiding.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    People seem hesitant to answer this, and I'm not quite sure why.

    A method is a set of code that is defined as part of a class. It performs some task, and optionally returns a value. It generally operates on class or instance variables.

    A class is a set of methods and variables. This is used to encapsulate related data and actions on that data. Often this is used to model a real-world item like a car, shape, or animal.

    An instance or Object refers to a concrete example of a class. These are the foundation of Object-Oriented Programming. Where a class is like a plan or idea, an object is a single item that is built from that plan. For example, a single German shepherd named Brüno.

    A method is most commonly used to act on an object/instance such as "walk" or "call" on a dog. This is called an instance method. There are methods that act on a class, such as "getPhylum" on the dog class. These are called class methods.

    Variables can also be tied to a class or instance. For example, the dog class might have a class variable to hold it's phylum, or return a list of all dog instances you've created. An instance variable would hold information about one single dog, like it's name, breed, etc.

    -Lee
     
  6. CrazyIllini thread starter macrumors member

    Joined:
    Oct 8, 2009
    #6

    Sorry the name of the book was Programming in Objective-C 2.0 by Stephan G. Kochan.




    Thank you. That was exactly what I was looking for.
     
  7. Tex-Twil macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
    #7
    If it can help:
    Objects: ... well those are "things" :) like in a real life. For example a car

    Classes: I would say that it is the same as Objects.

    Instance of an object: it is one object in particualar. For example your car. Another instance is the car of your neighbor. They are all cars but different instances.

    Methods: are actions you can perform on an Object:
    - start up the engine
    - open window
    - break
    - ...

    Instance variables: properties that define one car in particular (one instance of a car). For example the color is an instance variable. Your colors is red, the color of the neighbor s car is green. So instance variables differ from one instance to another.


    Hope it helps ;)
    Tex
     
  8. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #8
    My car object keeps receiving the -break method. Not sure how much longer I can afford to fix it.
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    You obviously didn't document your class clearly. Others are desperately sending the "-break" message hoping that it will result in the car stopping, but instead it results in your car being damaged.

    My recommendation would be to publish a method "-applyBrakes" that slows down/stops the car, and make a private (as private as you can make it in Obj-C, at least) method "-damageCar". This should cut down on the confusion and repair costs.

    -Lee
     
  10. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #10
    What I really need is +applyBrakes, but that one is rather difficult to implement.
     
  11. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Nice.
     
  12. Detrius macrumors 68000

    Joined:
    Sep 10, 2008
    Location:
    Asheville, NC
    #12
    I imagine it would heavily involve the use of +raiseGasPrices.
     
  13. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    I don't think a normal Car responds to that method, would probably throw an exception. Maybe a subclass though...
     

Share This Page