Reputation: 15
I have started learning Objective-C, and I just wanted to verify that my understanding of the concepts "class" and "object" is correct. If this is the wrong forum, for these kind of über basic questions, please inform where I should be heading!
Anyway, I am studying Objective-C from a book on the subject, and I am now trying to making the knowledge "my own".
So far I have understood that "Class" refers to a description of the “objects” that can be “constructed” from this “Class”??? And that an “object” based on this “class” is understood by the specific “attributes” that this “object” can have and the specific “methods” this object can perform??? So “attributes” to me is similar to the physical description of the “object” and “methods” are the “actions” that this object can perform?
Upvotes: 1
Views: 112
Reputation: 16
Your class basically describes the attributes and methods you can base an object on.
Your understanding seems to be correct but you described a little confusing. Let me give you a quick example:
Think about a cat. This shall be your class. It has the following attributes: -Name -Colour -Gender -...
And some methods: -Scratch -Walk -Meow -...
Now you know what a cat is and what it can do. With this knowledge you can create objects based on your class, for example:
A cat object named "garfield" (an instance of the class "cat") with the attributes: Name: Garfield Colour: Orange Gender: Male ... Which can scratch, walk, meow and... with it's methods.
I hope that was quite understandable.
Greetz :)
Upvotes: 0
Reputation: 8134
Thinking or cars etc is a great way to visualise object-orientated concepts.
You can have a class 'Vehicle' which has subclasses 'Boat', 'Plane', 'Motor vehicle', 'Bike' etc.
The 'Vehicle' class has methods (things you can tell it to do) 'Move', 'Stop', 'Turn' and attibutes 'Colour', 'Speed', 'Weight' etc. All the subclasses have the same methods and attibutes, as they 'inherit' them from the superclass.
Whilst they all move, they might do it differently (fly, float, roll, drive etc), by 'overriding' their superclass' 'move' method.
The actual car that you drive is an 'Instance' of the 'car' class. Your wife's car is another instance of the car class. Each instance has it's own attributes (colour etc).
Upvotes: 1
Reputation: 56697
Think of class
as a blueprint for objects. An object is also called "instance" - an actual "working copy" of a class.
An attribute is actually what you'd expect - in some languages it is also called a "property". Something like a color for example or a location, something that can be set and/or retrieved. Basically, from the developer's point of view, an attribute is mostly a variable that is bound to the object in that it is not a "global variable".
A "method" is like a function you can call on an object to do something.
For example, take a class named Address
. It would have attributes like Street
or ZIP
or Country
. Also, it could have a method GetDistance
to calculate the distance from your current location to the address. Then you could create any number of objects: my address, your address, Chuck Norris's address, etc.
PS: Do I get a +1 for the Chuck Norris reference...? :-)
Upvotes: 0
Reputation: 33139
Yes, 'class' refers to the type, and 'object' refers to an instance of a class.
So you may have a class Bike which describes a bike, and 3 objects bike1, bike2 and bike3 which are each instances of the class Bike.
I hope this clears things up.
Upvotes: 0