• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding classes and objects in terms of data types  RSS feed

 
Miles Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have seen many ways of describing what objects are, one being that objects are a user-defined datatype. However, if objects are datatypes, then what does that make classes? To me, it seems as though classes should be the "types" of data defined by the programmer, and objects should be the specific "values" of that user defined data type. As an example, an integer would be a class, while 1 would be a "value" of that class, i.e. an object. From this point of view, I don't see why a specific number would be a data type... Therefore, why do we say that objects are user defined data types rather than classes?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't heard people call objects user defined datatypes. I have heard classes called abstractions. So you can regard classes as the types (yes they are officially called types) and the objects are instances of those types so they are akin to values.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Miles Davis wrote:I have seen many ways of describing what objects are, one being that objects are a user-defined datatype. However, if objects are datatypes, then what does that make classes?

Objects are not datatypes. Classes are datatypes. People sometimes use sloppy language, mixing up "object" and "class", for example.

Miles Davis wrote:To me, it seems as though classes should be the "types" of data defined by the programmer, and objects should be the specific "values" of that user defined data type.

That's correct.
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like metaphors. To help me understand the difference between a class and an object I like to think of cars:

A car has a design (class) that describes things such as type of engine, type of tyres, etc (state) and it also has things that can be done to it such as start engine, change tyre (behaviour). These are just concepts of what can make up or be done to a car (abstraction), but of course they can't actually be done to the design itself. When a car is made (object) it then has an engine given to it (change of state: null to some type of engine), tyres added etc and the engine can be started (change in behaviour by sending a message to the car object).

Of course it is a simple metaphor and it falls over in that once a car is made it stays made. An object is only in existence at runtime once its constructor has been run and remains in existence until it has no more references to it or program execution terminates. Whereas the class definition is in existence for the duration of the program execution (and beyond if you count the lines of code in a file somewhere or other).

Whilst it doesn't really answer your question in terms of "data types" hopefully the above has helped you understand the role of classes and objects a bit better.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the computing world, an object is simply a designated region of memory allowed to exhibit a set of values and operations...

The class is simply a specification of what values and operations an object is allowed to exhibit...

Therefore everything is an object, even the primitives as they too have a predefined set of values and operations... But a programming environment can constrain its users to view things in a specific way according to a paradigm...

All in all, a type (class) is simply a set that contains values and operations and an object is an instance of that set of values and operations...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashley Bye wrote:Of course it is a simple metaphor and it falls over in that once a car is made it stays made. An object is only in existence at runtime once its constructor has been run and remains in existence until it has no more references to it or program execution terminates.

But that even works in the real world, with real cars. Once nobody is using the car anymore, someday the garbage collection service will take the car away and bring it to the scrapyard, destroying the car. In some programming languages (for example C, C++) you have to explicitly clean up your own junk - if you don't, you have a memory leak. In Java, you can just drop your garbage on the floor and the garbage collector will clean it up for you.

(And when the world ends, then all cars will probably be destroyed too...).
 
Ashley Bye
Ranch Hand
Posts: 140
2
Java Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:(And when the world ends, then all cars will probably be destroyed too...).


I prefer to think they will just be floating around in space in some kind of crazy asteroid belt-like situation waiting for some hapless race to stumble across them one day and be confused about how we managed to breath in them in space without oxygen supplies! Just like I am confused about how I didn't run out of oxygen when reading that sentence back to myself.

But yes, I suppose in a way you are right, the cars do eventually get destroyed. I now have visions of garbage collection being like a car crusher, with one poor, lone, object still inside screaming that it's not fair, it'll get the money whilst the garbage collector just laughs manically. I need to get out more.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:everything is an object, even the primitives

I thought that primitives and their associated operations are implemented directly in hardware (unlike Java objects which live on the heap). Or maybe you're referring to autoboxing of primitive types into wrapper objects.
 
Charles Sexton
Ranch Hand
Posts: 273
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Therefore everything is an object, even the primitives.....


This says different..... Oracle Link
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Joe Bishara & @Charles Sexton...

When I made the statement that everything is an object, even the primitives I was describing the general notion of an object and not the abstraction presented at the virtual machine layer...

Diving deeper and understanding what is a type and what is an object of a type is what I was trying to describe...

If we use set notation we can probably comprehend a bit better...

T = {possible values, possible operations} ... T defines the set for a type
O = {T1, T2, T3 ... Tn} ... O defines the set for an object

If we choose at random any element from the set O which can be some type, then we branch into the set T which contains values and operations...

From this description you should now be able to see that primitives are also objects...

P = {boolean, byte, char, short, int, long, float, double} ... P defines the set of Java primitives

B = {[true, false], [=, ==, !=, !, &, |, ^, &&, ||, ?:, &=, |=, ^=]} ... B defines the set for type boolean

boolean flag;

flag is an object of type boolean and can be of values true or false and can be manipulated using the operations from its type set...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico, but in Java programming, the term "object" has a very specific meaning that is different from the more general, mathematical meaning of the word.

In Java, primitives are not objects, with regard to what "object" means in Java. It's confusing if you talk about the more general meaning of the word when the discussion is about the Java programming language.
 
Miles Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I haven't heard people call objects user defined datatypes. I have heard classes called abstractions. So you can regard classes as the types (yes they are officially called types) and the objects are instances of those types so they are akin to values.


I actually read on the Java Beginner's FAQ here on JavaRanch that "An object is a User-defined Data Type (UDT)." Here is the link; it's bullet 12: http://www.coderanch.com/how-to/java/JavaBeginnersFaq. But besides that, I think I have a pretty good understanding now.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!