Class, object, instance and attribute definition

  • Thread starter lavster
  • Start date
  • #1
217
0
Hi,

I have what i think must be a really basic question. What is meant by a class, object, instance and attribute in programming? In simple terms (i dont know computing at all). I thought a class was basically something that could be represented as a noun in everyday life, an object is an instance of a class (whatever that means...), and attribute is something that object does a method on...

but then take example of person making a call on a phone... person is a class? or an object? would it be an object if i made my person a specific person, for example Jim? and then the method is making the call. And then attribute is the phone? but would the phone not be an object. or even a class? Does it depend on the context that it is used in?

I am really confused...

Thanks
 

Answers and Replies

  • #2
760
69
Hi,

I have what i think must be a really basic question. What is meant by a class, object, instance and attribute in programming? In simple terms (i dont know computing at all). I thought a class was basically something that could be represented as a noun in everyday life, an object is an instance of a class (whatever that means...), and attribute is something that object does a method on...

but then take example of person making a call on a phone... person is a class? or an object? would it be an object if i made my person a specific person, for example Jim? and then the method is making the call. And then attribute is the phone? but would the phone not be an object. or even a class? Does it depend on the context that it is used in?

I am really confused...

Thanks

A class is a type of thing. An instance would be a specific thing of that type. If we have to use your analogy, "person" could be a class, and an instance of "person" could be "Jim".

"object" can mean the same thing as instance
 
Last edited:
  • #3
rcgldr
Homework Helper
8,708
534
object - a single entity, in a very generic sense. Just about any noun could be considered an object, such as a car or a tree in a racing game. Think of this as a noun.

class - in C, C++, Java, ... , defines a group of data variables and/or functions as part of a common structure. In the case of C or C++, internally it defines the offsets (relative to an actual instance of the class), data type, data size (or function type and parameters) for all of the defined members of that class.

instance - the actual existance in memory of some object, such as a simple data variable, a class, or the actual execution of function. In the case of a function, if the function is recursive and calls itself, then each call to the function is considered a separate instance of that function.

attribute - The type of data, or function. Think of this as an adjective.
 
  • #4
595
0
I would make a slight argument about "attribute" being a "type of data"....

In Object(dis)Oriented and Database context, I usually use "attribute" as the general description for data members in a class. For instance, the class (or table) "Person" could have the attributes "Name", "Rank", and "SerialNumber". Each of those is a specific data type and probably a class unto itself.

This usage seems to be supported by the Java "Attribute" type, but it's kinda hard to tell from the javadoc: http://download.oracle.com/javase/1.4.2/docs/api/javax/naming/directory/Attribute.html [Broken]
 
Last edited by a moderator:
  • #5
rcgldr
Homework Helper
8,708
534
I would make a slight argument about "attribute" being a "type of data"....
Not "a type of data", but instead "the type of data", more like an adjective than a noun. For example what are the attributes of a group of people in a database: their gender, age, ... .
 
  • #6
217
0
thanks :)
 
  • #7
703
13
Consider the following objects:
1) banana, apple, strawberry
2) Robert Millikan, Michael Faraday, Lise Meitner

What do the objects in 1) have in common?
They belong to the class Fruit.

What do the objects in 2) have in common?
They belong to the class Person.

--

Every class consists of two things:
1. Attributes
2. Methods


Attributes describe the object. Another word for attributes is properties.
Methods allow you to access or alter the attributes.

--

Example:

Code:
class Person:

      // Attributes
      name
      age
      weight

      // Methods
      setName(x)
      setAge(x)
      talk()
      eat()

Code:
class Fruit:

      // Attributes
      name
      color
      flavor

      // Methods
      setName(x)
      setColor(x)
      setFlavor(x)
Let's create an object myFruit of the class Fruit:
Code:
myFruit = new Fruit()
Let's define the attributes by applying the methods:
Code:
myFruit.setName("apple")
myFruit.setColor("green")
myFruit.setFlavor("sour")
Now, for some reason myFruit may change its color to "red" and its flavor to "sweet".
We use the methods again:
Code:
myFruit.setColor("red")
myFruit.setFlavor("sweet")
Sometimes, we say instance instead of object. Other objects (or instances) of the class Fruit are 'banana' and 'strawberry'.

---

Here are some links on classes and object oriented programming:
http://www.youtube.com/watch?v=c5kfCH50wl0"
http://www.youtube.com/watch?v=IBpZBI_8QAE&feature=youtu.be"
http://www.youtube.com/watch?v=tUu9V1lAaLM&feature=youtu.be"
http://learntofish.wordpress.com/2009/09/20/object-oriented-programming-an-introduction/" [Broken]
 
Last edited by a moderator:

Related Threads on Class, object, instance and attribute definition

Replies
9
Views
2K
Replies
8
Views
764
Replies
4
Views
714
Replies
7
Views
2K
Replies
5
Views
2K
Replies
2
Views
987
  • Last Post
Replies
8
Views
708
Replies
2
Views
881
  • Last Post
Replies
2
Views
4K
Top