Class, object, instance and attribute definition

In summary, a class is a type of thing, an instance is a specific thing of that type, and attribute is an adjective.
  • #1
lavster
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 don't 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
 
Technology news on Phys.org
  • #2
lavster said:
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 don't 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
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 existence 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
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
 
Last edited by a moderator:
  • #5
schip666! said:
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
thanks :)
 
  • #7
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/"
 
Last edited by a moderator:

What is a class?

A class is a blueprint or template for creating objects in object-oriented programming. It defines the characteristics and behaviors that an object of that class will have.

What is an object?

An object is an instance of a class. It is a specific, tangible representation of the concepts defined in the class. Objects have attributes and methods that allow them to interact with other objects and perform tasks.

What is an instance?

An instance is another term for an object. It refers to a specific occurrence or example of a class. Each instance of a class has its own unique set of attributes and can perform actions independently from other instances.

What are attributes?

Attributes, also known as properties or variables, are the characteristics or data associated with an object. They define the state of an object and can have different values for each instance of a class.

How are attributes defined?

Attributes are defined within the class and can be assigned a default value. They can also be modified or accessed using methods defined in the class. Attributes can be of different data types, such as strings, integers, or booleans.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
Replies
25
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
777
  • Programming and Computer Science
2
Replies
43
Views
2K
  • Programming and Computer Science
Replies
3
Views
4K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
48
Views
9K
Back
Top