Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Class, object, instance and attribute definition

  1. Sep 30, 2011 #1
    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
     
  2. jcsd
  3. Sep 30, 2011 #2

    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: Sep 30, 2011
  4. Sep 30, 2011 #3

    rcgldr

    User Avatar
    Homework Helper

    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.
     
  5. Oct 3, 2011 #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 [Broken]
     
    Last edited by a moderator: May 5, 2017
  6. Oct 3, 2011 #5

    rcgldr

    User Avatar
    Homework Helper

    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, ... .
     
  7. Oct 3, 2011 #6
    thanks :)
     
  8. Oct 3, 2011 #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 (Text):
    class Person:

          // Attributes
          name
          age
          weight

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

    Code (Text):
    class Fruit:

          // Attributes
          name
          color
          flavor

          // Methods
          setName(x)
          setColor(x)
          setFlavor(x)
     
    Let's create an object myFruit of the class Fruit:
    Code (Text):
    myFruit = new Fruit()
    Let's define the attributes by applying the methods:
    Code (Text):
    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 (Text):
    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: May 5, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Class, object, instance and attribute definition
  1. Classes and objects (Replies: 6)

Loading...