View Full Version : Clases, Objects, Methods and Instances Explanation
CrazyIllini
Dec 21, 2009, 06:08 PM
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.
chown33
Dec 21, 2009, 06:23 PM
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
powerbuddy
Dec 21, 2009, 06:24 PM
Check out the java versions of classes and objects. The concept of OOP should be very similar.
Compile 'em all
Dec 21, 2009, 06:29 PM
Get a software engineering book that talks about OO and things like encapsulation and data hiding.
lee1210
Dec 21, 2009, 07:06 PM
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
CrazyIllini
Dec 21, 2009, 07:31 PM
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
Sorry the name of the book was Programming in Objective-C 2.0 by Stephan G. Kochan.
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
Thank you. That was exactly what I was looking for.
Tex-Twil
Dec 22, 2009, 12:59 AM
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
Sydde
Dec 22, 2009, 11:05 AM
My car object keeps receiving the -break method. Not sure how much longer I can afford to fix it.
lee1210
Dec 22, 2009, 11:12 AM
My car object keeps receiving the -break method. Not sure how much longer I can afford to fix it.
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
Sydde
Dec 22, 2009, 11:39 AM
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
What I really need is +applyBrakes, but that one is rather difficult to implement.
jared_kipe
Dec 22, 2009, 11:55 AM
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
Nice.
Detrius
Dec 22, 2009, 01:33 PM
What I really need is +applyBrakes, but that one is rather difficult to implement.
I imagine it would heavily involve the use of +raiseGasPrices.
jared_kipe
Dec 22, 2009, 03:16 PM
I imagine it would heavily involve the use of +raiseGasPrices.
I don't think a normal Car responds to that method, would probably throw an exception. Maybe a subclass though...
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.