Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OOP Terminology: class, attribute, property, field, data member

I am starting studying OOP and I want to learn what constitutes a class. I am a little confused at how loosely some core elements are being used and thus adding to my confusion.

I have looked at the C++ class, the java class and I want to know enough to write my own pseudo class to help me understand.

For instance in this article I read this (.. class attribute (or class property, field, or data member)

I have seen rather well cut out questions that show that there is a difference between class property and class field for instance What is the difference between a Field and a Property in C#?

Depending on what language I am studying, is the definition of

  • Property
  • Fields
  • Class variables
  • Attributes

different from language to language?

like image 277
You Know Nothing Jon Snow Avatar asked May 25 '13 15:05

You Know Nothing Jon Snow


People also ask

What is attribute and property in OOP?

In summary: Property is a broad concept used to denote a particular characteristic of a class, encompassing both its attributes and its relationships to other classes. Attribute denotes a part of an aggregate object, and so is used during analysis as well as design to express a singular property of the class.

What is class attribute in OOP?

Any variable that is bound in a class is a class attribute . Any function defined within a class is a method . Methods receive an instance of the class, conventionally called self , as the first argument.

Is class an attribute or property?

Properties are special kind of attributes. Two types of attributes: Class attribute.

What are fields in OOP?

In object-oriented programming, a field (also called data member or member variable) is a particular piece of data encapsulated within a class or object.


2 Answers

"Fields", "class variables", and "attributes" are more-or-less the same - a low-level storage slot attached to an object. Each language's documentation might use a different term consistently, but most actual programmers use them interchangeably. (However, this also means some of the terms can be ambiguous, like "class variable" - which can be interpreted as "a variable of an instance of a given class", or "a variable of the class object itself" in a language where class objects are something you can manipulate directly.)

"Properties" are, in most languages I use, something else entirely - they're a way to attach custom behaviour to reading / writing a field. (Or to replace it.)

So in Java, the canonical example would be:

class Circle {      // The radius field     private double radius;     public Circle(double radius) {         this.radius = radius;     }      // The radius property     public double getRadius() {         return radius;     }     public void setRadius(double radius) {         // We're doing something else besides setting the field value in the          // property setter         System.out.println("Setting radius to " + radius);         this.radius = radius;     }      // The circumference property, which is read-only     public double getCircumference() {         // We're not even reading a field here.         return 2 * Math.PI * radius;     }  } 

(Note that in Java, a property foo is a pair of accessor methods called getFoo() and setFoo() - or just the getter if the property is read-only.)


Another way of looking at this is that "properties" are an abstraction - a promise by an object to allow callers to get or set a piece of data. While "fields" etc. are one possible implementation of this abstraction. The values for getRadius() or getCircumference() in the above example could be stored directly, or they could be calculated, it doesn't matter to the caller; the setters might or might not have side effects; it doesn't matter to the caller.

like image 78
millimoose Avatar answered Sep 17 '22 15:09

millimoose


I agree with you, there's a lot of unnecessary confusion due to the loose definitions and inconsistent use of many OO terms. The terms you're asking about are used somewhat interchangeably, but one could say some are more general than others (descending order): Property -> Attributes -> Class Variables -> Fields.

The following passages, extracted from "Object-Oriented Analysis and Design" by Grady Booch help clarify the subject. Firstly, it's important to understand the concept of state:

The state of an object encompasses all of the (usually static) properties of the object plus the current (usually dynamic) values of each of these properties. By properties, we mean the totality of the object's attributes and relationships with other objects.

OOP is quite generic regarding certain nomenclature, as it varies wildly from language to language:

The terms field (Object Pascal), instance variable (Smalltalk), member object (C++), and slot (CLOS) are interchangeable, meaning a repository for part of the state of an object. Collectively, they constitute the object's structure.

But the notation introduced by the author is precise:

An attribute denotes a part of an aggregate object, and so is used during analysis as well as design to express a singular property of the class. Using the language-independent syntax, an attribute may have a name, a class, or both, and optionally a default expression: A:C=E.

Class variable: Part of the state of a class. Collectively, the class variables of a class constitute its structure. A class variable is shared by all instances of the same class. In C++, a class variable is declared as a static member.

In summary:

  • Property is a broad concept used to denote a particular characteristic of a class, encompassing both its attributes and its relationships to other classes.

  • Attribute denotes a part of an aggregate object, and so is used during analysis as well as design to express a singular property of the class.

  • Class variable is an attribute defined in a class of which a single copy exists, regardless of how many instances of the class exist. So all instances of that class share its value as well as its declaration.

  • Field is a language-specific term for instance variable, that is, an attribute whose value is specific to each object.

like image 25
saza Avatar answered Sep 16 '22 15:09

saza