|
Instantiated is not equivalent with application programmer action. In case of static methods and fields the compiler does it in the background. At the runtime it is occupying memory and it is an object as well and integral part of OOP.
Object has a type defined by its class and at the core of OOP is compound type consisting of fields and functions (if any as you pointed out).
Sizeof operator will distinguish between int and float/double and C++ has features that are similar to reflection (RTTI). In fact anything you do in any language post C++ is C++. You are just not aware of this because certain complexities of C++ are hidden from modern programmer and rightly so.
Object is occupying memory and each and every object can be copied, moved etc. Just give me pointer to it.
What is confusing for most programmers? Programming abstraction which is class that does not exist unless object is created based on class design meaning that chunk of memory for data and code is assigned and then executed on the hardware. The designer creates classes but the actual work is done by objects residing on hardware.
I do not mind disagreements but to my astonishment none of the posts above was concrete enough to correlate programming abstraction with constrains of underlying hardware. This only confirms that the mumbo jumbo written in most programming books on OOP is truly confusing and able to fool experienced programmers.
|
|
|
|
|
Object is an heavily overloaded term.
So, let's try to put things into perspective.
"Object" was first introduced as a CONCEPT, as in Object-Oriented Programming. In this model, you have "stuff", which we'll call objects, that implements a program structure and logic merging procedures AND data on the same "thing". Or object, so to speak. In this new paradigm, we call procedures as Object Methods, and data as Object Properties, linked together as it makes more sense (before this the paradigm said you have code structured in procedures that manipulates data, which existed independently).
Then, based on this conceptual model, a lot of really smart people created computer languages.
Each one of them used this new paradigm as it seemed fit to solve problems.
So "objects" morphed into Classes, Instances, hierarchy, etc.
But, let's face it: Object sounds a lot better than Instance or Class, so, sometimes, people used the abstract concept of object to refer to something that was actually implemented (like Bruce Eckel in the quote from before).
So, the answer to "what is an object?" is: Anything that has Methods and Properties, merging those two really different things.
Really, really simple.
You're asking the wrong question: An Object is NOT a Class, nor an instance.
Exactly like as a function is not an Algorithm. A function can implement an Algorithm, pulling the concept into the "real world", even if not in a tangible form.
But in some languages a Class IS an object (e.g. if you have static methods and properties).
In .NET, both classes and class instances are Objects.
In Python, EVERYTHING is an object.
Conclusion: it's more a matter of asking the right questions.
|
|
|
|
|
Having static properties and methods does not make something an object. In C++, Java and C# such members are thinly disguised globals.
In Smalltalk, classes really are objects. They have methods and data, and can be manipulated at runtime (e.g. sending a message to add a new method or slot to a class).
(Of course System.Type IS an object - but that is fairly unrelated to the use of static members).
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
In C#, classes can have properties and methods: they are called static properties and methods.
They can be manipulated at run time, exactly in the same way as instances: you can change property values and call class methods.
There is no dynamic typing in C# (except for the dynamic type, but let's ignore this for the sake of this discussion).
So, a class in C# can be an Object (I was going to write "is a", but "can be" is more correct), as for the "object" definition.
|
|
|
|
|
Not at all.
A normal method of a class (an instance method) has a hidden "this" parameter passed at run time. Any virtual methods defined on the class use "this" to provide access to the vtable, used to resolve virtual methods.
In contrast, a static method has no "this" parameter at all, it does not get passed a reference to the class object. In this sense, they are exactly analogous in the way they are implemented to global variables and functions. The only (slight) benefit offered is a degree of encapsulation.
C#, C++ and Java do resolve virtual methods dynamically, by looking them up in a vtable at run time. That is the definition of polymorphism.
Its not as dynamic as message resolution in dynamic languages, but its all they offer.
Gilad Bracha (one of the people who defined the JVM) is eminently more qualified than me to point out the issues with this, and has done so eloquently over at Room 101[^].
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
99% correct. Everything is right, except for the first line: "Not at all".
They are implemented differently, yes. But this has nothing to do with the definition of Object. You seem to fail to completely grasp the difference between the concept of object and the implementation of the concept in the language.
If "An object is something that has methods and properties", you must accept that classes in C# are, in fact, objects. Special ones, implemented differently, but the concept has nothing to do with the implementation.
Static methods can access static variables and nothing else. This is the exact definition of object.
Right or wrong, useful or dangerous, like it or not
I agree with everything you say, but it's not a point, just a technical digression.
Classes are objects? Let's see...
It has properties? Mmmmm, yes, it can.
It has methods? Again, yes.
Ipso facto, in C# a class can be an object. Everything else is simply judging the pros and cons of the actual implementation (which I agree with you 100%).
Bye!
|
|
|
|
|
OK, in which case they are just extremely badly designed objects. They inhibit testability, break the Single Responsibility Principle (because a "class" now declares 2 objects), the Open-Closed Principle (because they inhibit "open for extension"), the Liskov Substitution Principle (because they cannot be substituted by anything), the Interface Segregation Principle (because a class cannot declare an interface for static members) and the Dependency Inversion Principle (because they cannot be abstract).
So, use of statics breaks every single one of the SOLID principles.
Alan Kay (who coined the term Object-Oriented) emphasises that message-passing (polymorphism) is crucial to object-oriented programming. An object that cannot be used polymorphically is at least breaking the spirit of OOP, if not the definition.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Agreed, 100%.
I was not telling in any way it's a good idea to use a lot of statics.
I was just referring to an "object" strict definition.
|
|
|
|
|
A very interesting discussion. You both have proven the point of the original question most eloquently. Object is a very ambiguous term. Its meaning depends on the context of which is used.
I wonder what the above debate would look like if you could not use the word object and could only use the terms class and instance.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
Couldn't be bothered looking it up on Wikipedia?
Wikipedia[^]
It's a better coverage than you're likely to get in any discussion here - I've noticed many inconsistent answers already.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
I started with Wikipedia. I also looked in several other places. At the conversation shows there is considerable disagreement/confusion on what an object is. There is no confusion on what a class and instance are.
In total I think robocodeboy got it right. This opinion is base not only on this thread but also other research. There are several others in this thread that said similar things. Robocodeboy had the most complete statement but still missed one important point. In most object-oriented systems, Object is the base class from which all other classes are derived. It further reinforces everything robocodeboy said.
Most important, I was able to successfully argue the point with my professor. He conceded that object was an ambiguous term and now feels that the only proper terms are class and instance. Thanks all for the help.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
The use of Object as a base class is, in my opinion, irrelevant.
An object is simply something that has state and behaviour. The state is typically encapsulated.
C++ has no "Object" class, but can be used for OO development.
The exact definition will vary by programming language. For example in Smalltalk, an object is pretty well anything that exists in the program at runtime - even classes are objects (instances of the class Class). The only way in ST to manipulate objects is to send them messages. It is important not to come up with a definition that excludes Smalltalk, as that is the context in which the term "Object-Oriented Programming" was invented.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
I think you just confirmed my conclusion:
. . . . Object is an ambiguous term. The only non-ambiguous terms are class and instance which we should use more often.
I do not think there is a single definition for object. Its meaning depends on the context of the discussion, the language, etc.
Since most often C++ is used in a Microsoft environment, then in general you can say that the base class of all classes is Object. It only shows there is another definition for the term Object. That is the relevant point. Please note the phrases "most often" and "in general". There are probably exceptions. Still, I suspect that even other implementations of C++ have a base class Object.
I have looked around and find Object is often the base class in many implementations of Java. Even some of the newer versions of ancient Lisp/Scheme that have added OOP use Object as the base class. I was very surprised at how consistent that usage is.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
Unfortunately, class and instance may be unambiguous (or less ambiguous anyway), but don't generalise well as not every object-oriented language has classes.
Both JavaScript and Self support object-oriented programming in which slots (member variables) and methods are defined directly on objects.
As far as I am aware though, every object-oriented programming language supports objects that hold (ideally encapsulated) data and respond to messages (method calls, member function invocations) in some way. That pretty well sums up the role of an object. The rest is all how particular environments implement that.
I'd dispute that "most often C++ is used in a Microsoft environment". I suspect its used much, much more on Linux and (in the form of Objective C++) on Macintosh and i-Whatever platforms. Its also used in a vast number of embedded systems ranging from phones to mars rovers.
While Microsoft's MFC class library is rooted in Object, their ATL library is not. Apple use an NSObject as the root of theirs (although not every class needs inherit from NSObject).
Most C++ coding actually tends not to use class hierarchies that much - most of the C++ classes I've encountered either do not inherit anything, or from a base class that does not inherit from a common "Object" class. This becomes particularly necessary in the presence of multiple inheritance. Indeed most of the standard C++ library does not even define the base classes. For example, a library must provide a std::vector class, but that class may nor may not inherit from other classes as an implementation decision - it simply has to offer a certain interface (better described in C++ terminology as a Concept). Indeed many objects in C++ are pretty abstract - like iterators, containers, etc. which all behave similarly but have no relationship through inheritance.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
What object oriented language does not have classes? I was never able to find one.
I should have been more careful in my criteria. I was referring to the amount of code use and not lines written. Consider much of Windows and its applications are based on C++. Also consider Windows used to hold a near monopoly on operating systems. I still think C++ is used most often in the Microsoft environment. That may be changing as C# gains hold and Windows loses more of its monopoly. As far as lines written I would not even venture to guess that.
Also, do not confuse C with C++. I am sure C under any criteria is the most used language.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
"What object oriented language does not have classes?" - I mentioned two in the post above: JavaScript and Self.
I'm not so convinced about MS's share of C++ market - witness the fact that their's is the least conformant version of C++ (compared to GCC and CLang). I think if it was still pervasive on their platform, they would have invested more effort (especially with Herb Sutter on the standards committee). I think .NET has overtaken C++, except in limited app domains such as games.
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
|
|
|
|
|
Yes, .NET has taken over everything. As you move from VB to C++ to C# you have to relearn little. Most of the program is .NET calls. Actually all the languages Java, C#, C++, etc. are actually very small. It is all the object model/libraries that surround it that do much of the work. It is often harder to learn a new object model than the language.
Some of the standards committees add things to the language that are really part of the object libraries. They then call things that only implement the original language non-compliant. I think they need to split the language core from its supporting environment. That is how things really work. In standards committees each push their own environment and no body wins. The result is often garbage or it takes so long to approve that it is either irrelevant or obsolete.
So many years of programming I have forgotten more languages than I know.
|
|
|
|
|
Too true, I've been trying to catch up with modern C++ developments (many of these are the language) and its just got so goddamn complex I no longer know how the hell a newcomer would begin.
I was remiss earlier and neglected to provide a link to Self: Self Wikipedia[^] and Self Website[^]. It's really worth a look (although only from the point-of-view of seeing another perspective, I feel its ill-suited to modern programming). It pioneered many of the techniques we take for granted in .NET, Java and modern JavaScript - particularly in efficient VM implementation (hotspots, JITing etc.). It also introduced prototype-based programming.
I'm a little biased when it comes to defining Objects - my perspective is largely informed by the original Smalltalk definitions and I cannot help feeling that not everything since then is really progress. In particular, most later languages support something based on v-tables rather than full open polymorphism, which to my mind actually limits possibilities rather than open them up.
*Edit*
Wow - just found an awesome video on YouTube on Self - gives some real context Self Video[^]
"If you don't fail at least 90 percent of the time, you're not aiming high enough."
Alan Kay.
modified 2-Dec-13 17:21pm.
|
|
|
|
|
My colleague found this and other horrors in a newish project.
Bearing in mind that "Id" is a nullable int and result.ID is a regular int, who in their right mind would do this (object and function names have been changed to protect the incompetent):
protected Thing populateThing(DataRow dr)
{
Thing result = new Thing();
result.Id = int.Parse(dr["Id"].ToString());
BTW, note no handling of the case when "Id" is DBNULL.
You can stop laughing now.
|
|
|
|
|
And with what did you replace it?
|
|
|
|
|
I didn't it's not my project.
My colleague was traumatised by this and many other code-crimes in the project, I'm hoping he'll recover soon
|
|
|
|
|
I must be missing something. Doesn't dr["Id"] return an object?
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
A drop down list populated from an enumeration you think? None (from FaceBook)
modified 1-Nov-13 18:25pm.
|
|
|
|
|
Nothing to see here move along.
-- The FaceBook Team
Hello World!
|
|
|
|
|
Nice... Should have "under construction"...
There are no secrets to success. It is the result of preparation, hard work, and learning from failure. Colin Powell
|
|
|
|
|