Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is a member vs. a property

A friend who is new to OO programming asked me the difference between a Member and Property, and I was ashamed to admit that I couldn't give him a good answer. Since properties can also be objects themselves, I was left with a general description and list of exceptions.

Can somebody please lay out a good definition of when to consider something a member vs. a property? Maybe I'm bastardizing the concept, or is it just that a member is just the internal name I use, and the property is what's exposed to other objects?

I don't think that not knowing the answer to this question has affected the quality of my programming, and it's just a semantics point, but it still bothers me that I can't explain it to him.

like image 687
SqlRyan Avatar asked Aug 05 '09 18:08

SqlRyan


People also ask

What is difference between fields and properties?

Fields are ordinary member variables or member instances of a class. Properties are an abstraction to get and set their values. Properties are also called accessors because they offer a way to change and retrieve a field if you expose a field in the class as private.

What is a member in OOP?

In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (member functions).

What is member property in C#?

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they're public data members, but they're special methods called accessors.

What is a property OOP?

A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method.


2 Answers

A property is one kind of member. Others might be constructors, methods, fields, nested types, conversions, indexers etc - depending on the language/platform, of course. A lot of the time the exact meaning of terminology depends on the context.

To give a C#-specific definition, from the C# 3.0 spec, section 1.6.1:

The following table provides an overview of the kinds of members a class can contain.
(Rows for...)

  • Constants
  • Fields
  • Methods
  • Properties
  • Indexers
  • Events
  • Operators
  • Constructors
  • Destructors
  • Types

Note that that's members of a class. Different "things" have different kinds of members - in C#, an interface can't have a field as a member, for example.

like image 107
Jon Skeet Avatar answered Oct 28 '22 06:10

Jon Skeet


Neither of the two terms has any defined meaning whatsoever in Object-Oriented Programming or Object-Oriented Design. Nor do they have any defined meaning in the overwhelming majority of programming languages.

Only a very small number of programming languages have a concept called property or member, and even fewer have both.

Some examples of languages that have either one of the two are C++ (which has members), ECMAScript (which has properties) and C# (which has both). However, these terms don't necessarily denote the same concepts in different programming languages. For example, the term "member" means roughly the same thing in C++ and C#, but the term "property" means completely different things in ECMAScript and C#. In fact, the term "property" in ECMAScript denotes roughly the same concept (ie. means roughly the same thing) as the term "member" in C++ and C#.

All this is just to say that those two terms mean exactly what the relevant specification for the programming language says they mean, no more and no less. (Insert gratuitous Lewis Carroll quote here.)

like image 38
Jörg W Mittag Avatar answered Oct 28 '22 04:10

Jörg W Mittag