OOP terminology

  1. 5 months ago

    Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    What is the relationship between a class, object, and instance?

    Although this seems like a simple question, I have had many 'interesting' discussions and am not exactly sure where one starts and another ends. Here is one possible definition:
    A class is created to hold an integer.

    This is a class and an object(?), or just a class?

    class Person
    Property Age as Integer

    in code, the instance of an object is created (?), or is this an instance of a class to create an object?

    Dim Bob as New Person

    I have heard two common definitions, and which one is right - or is this in subjective to someones view?

    Possible Definition 1) A class is created in IDE code. When the program runs then the class is considered an object. When a new variable of the object is created, then this is considered an instance of the object. When the class for Bob's age is changed, then it is the instance of the object data that is changed.

    Possible Definition 2) A class is created in IDE code. When the program runs then the class is still considered a class. When a new variable of the object is created, then the instance of the class is created to make an object. When the class for Bob's age is changed, then it is the object of the class that has changed.

    Which one is right, or are both incorrect?

  2. Dave S

    Dec 29 San Diego, California USA

    to me... a class and object are for the most part the same thing.... it would be code that defines an encapsulated relationship between attributes, properties, events and or methods (any one or combination).

    An instance is a variable that can perform the role as defined by the class.

    You can have only one definiton of CLASS X, but you can have many INSTANCES of CLASS X

  3. Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    @Dave S An instance is a variable that can perform the role as defined by the class.

    You can have only one definiton of CLASS X, but you can have many INSTANCES of CLASS X

    Ok, then the word object and class are possibly the same thing. Let me make another definition to see if I understand it:

    Possible Definition 3) A class is created in IDE code. When the program runs then the class is still considered a class, since there can be only one class. When a new variable of the class is created it can be called an instance or an object. When the object (or class) for Bob's age is changed, then it is the object (or class) that has changed.

    Did I get it right Dave?

  4. Tim P

    Dec 29 Testers, Xojo Pro Rochester, NY
    Edited 5 months ago

    It helps to think of these items as real world items.

    An object is literally that. The manifestation of anything that can be.
    A class is like a template, a mold. As a template, it is not an object.
    An instance is the manifestation of a class template. This is an object.

    If a class were the blueprint for a coffee mug, the instance would be any individual coffee mug made from the blueprint. Instances - mugs - can have different characteristics of the source template's defined properties. For example the template would define a spot for the handle, but whether or not the mug has a handle changes from mug to mug - instance to instance.

    I find tying the abstract ideas of OOP to a specific IDE makes things more complicated. I don't like either possible definition you've come up with. Edit Redux: Your third definition is close I think, but an age as a class is over engineering. To illustrate, Bob would be an instance of the Human class which would have an Age property.

    Something to note, while variables can hold a reference to the instance of an object; the variable itself is not the instance. This is an important differentiation, as otherwise you wouldn't be able to pass an object around between variables.

  5. Gavin S

    Dec 29 Testers, Xojo Pro, Forum Moderators, MVP UK
    Edited 5 months ago

    There's very little I'd change about Tim's excellent answer. I also agree that you can't really tie these concepts to a specific IDE because they're almost philosophical concepts.

    Here's my attempt at explaining:
    A class: this is the template. The example often given is of a blueprint of a house or a car.
    An object: this is where it gets more abstract. An object is something you could make from a class. But a specific one of these is usually called...
    An instance: the specific, in-memory actual thing, created from a class. This would be a specific, actual individual house or car.

    A lot of languages simply refer to classes and instances.

  6. Andy B

    Dec 29 Airdrie, AB, Canada

    I agree with Tim's take.

    I think of Class as one of the reserved words used in programming to define a potential object, just like Struct as a reserved word to define a structure.

    Object and Instance are more generic/all-encompassing terms, (like the word poly-morphism or inheritance) that are words used in OOP programming in any language.

  7. Rick A

    Dec 29 Testers, Xojo Pro (Brazil. UTC-3:00)

    @Eugene D This is a class and an object(?), or just a class?

    Class Person
    Property Age as Integer
    End Class

    in code, the instance of an object is created (?), or is this an instance of a class to create an object?

    Dim Bob as New Person

    Person is Class, Bob is an object of the Class Person instantiated by the keyword "new".

    Instantiation is the "creation" of a new object.

    Class is a "Cake Pan", object is a "Cake", every new "Cake" you produce is an instance of the "Cake Pan".

  8. Robert B

    Dec 29 Testers, Xojo Pro University of Connecticut

    @Tim P A class is like a template, a mold. As a template, it is not an object.

    Having taught object oriented scientific programming for many years, I had significant trouble getting the idea of an object across to my students. Tim Parnell's list is very useful, but I disagree that a class is not an object. To most it is an object.

    But I dont want to start class warfare here, so I respect the template argument, but dont agree with it.

  9. Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    @Tim P It helps to think of these items as real world items.

    Thanks for the good response Tim, this is quite helpful.

    I like your coffee cup manufacturing theme, and the article is also helpful, especially the following quote:

    An object is a member or an "instance" of a class.

    Using the helpful coffee cup analogy, a blueprint of a coffee cup is a class. When the coffee cup is being manufactured (poured, formed, molded, and coloured) then the coffee cup is being manufactured - or an instance (instantiation?) is occurring. Once the coffee cup is completely finished and and can hold a good cup of coffee, then the object of the class is created, and instantiation is finished.

    The steps seem like it starts as a class (blueprint), is instantiated (manufacturing step), and a final object (functional coffee cup) is created. Is an object slightly different than an instance?

    Does this seem right?

  10. Tim P

    Dec 29 Testers, Xojo Pro Rochester, NY

    @Eugene D Is an object slightly different than an instance?

    I'm having difficulty finding information to answer this for you.

  11. Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    @Robert B But I dont want to start class warfare here, so I respect the template argument, but dont agree with it.

    Thanks Robert.

    My intent is to ask the question and get some type of understanding, and definitely not to create negative feelings.

    When I finished my Ph.D., I took an oath to ask questions - even if I looked silly. I would rather be considered a fool for 5 minutes by asking the question, rather than a fool for life by not asking the question at all.

    I appreciate your comment.

  12. Rick A

    Dec 29 Testers, Xojo Pro (Brazil. UTC-3:00)
    Edited 5 months ago

    A created object is the result of the instantiation of a class.

    Instantiation is the creation of the object. A Cake is an object from the class CakePan, when you make 3 Cakes, you have 3 instances of "CakePan" that also can be said as 3 CakePan objects.

    When you do:

    Dim cake as CakePan // you can say cake is an CakePan object (holder), but it is "empty" (Nil), it IS NOT an instance of such class yet.

    But

    cake = New CakePan // cake is an instance of CakePan, the object now has all the proper content.

  13. Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    @Rick A Dim cake as CakePan // you can say cake is an CakePan object, but it is "empty" (Nil), it IS NOT an instance of such class yet.

    HI Rick,

    Thanks for the explanation. This seems to be yet another slightly different aspect of an instance - this is good!

    Once discussion I had with a colleague suggested that instantiation of a class is creating the object, whether the object is populated or not;

    Dim cake as CakePan // Some say this is an instance of a cake since there is a cake, although it is not customized or populated. 

    This is good information. Thanks, and I'll think about this some more, as its a helpful point.

  14. Rick A

    Dec 29 Testers, Xojo Pro (Brazil. UTC-3:00)
    Edited 5 months ago

    @Eugene D Dim cake as CakePan // Some say this is an instance of a cake since there is a cake, although it is not customized or populated.

    Some are wrong.

    There's no Cake there, just a place waiting for it. :D

    cake is a not instantiated object CakePan, after receiving the "new CakePan" it will be an instance of CakePan.

  15. Robert B

    Dec 29 Testers, Xojo Pro University of Connecticut

    Most textbooks treat modules and classes as the two most powerful and flexible objects of object oriented programming. I do not understand why a template cannot itself be an object.

    But we should all accept the fact that we are dealing with semantics, and that invariably results in point of view positions that are difficult to resolve.

  16. Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    @Rick A cake is a not instantiated object CakePan, after receiving the "new CakePan" it will be an instance of CakePan.

    Yes, that is a good point. To make sure I understand it, let me use a house analogy. Blueprints of a house would be the class. There may be two steps for instantiation, meaning the house is built but does not have people living in it. Once the house is populated (filled with furniture and is being used) then the house is now a home (an object).

    That's a good differentiation of the word instance. :)

  17. Russ L

    Dec 29 Xojo Pro

    i remember that i was taught it this way...

    in your program when it is designed in the IDE, you create a class. when the program runs there is a part of memory that is the class, you cannot set its properties or run its methods, thats an object. when you 'copy' that object into a variable you create an instance of the object.

    but that was last century so things may have been altered by time and alcohol....

  18. Eugene D

    Dec 29 Testers, Xojo Pro Canada scispec.ca

    @Russ L but that was last century so things may have been altered by time and alcohol....

    Oh yes, those were the days in University and College :)

    I have also heard of this definition in the past.

    It seems like there is no right or wrong answer to this question, as there are many valid ways of describing this.

    Thanks for the helpful posts everyone.

  19. Rick A

    Dec 29 Testers, Xojo Pro (Brazil. UTC-3:00)

    -image-

    https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-diagram-tutorial/

  20. Robert B

    Dec 29 Testers, Xojo Pro University of Connecticut

    So if two humans (objects) have intercourse is that an example of instantiation or object oriented programming?

  21. Newer ›

or Sign Up to reply!