In OOP, there are entities (e.g. Person) which has attributes (e.g. name, address, etc) and it has methods. How do you describe new
? Is it a method or just special token to bring an abstract entity to real one?
views:
250answers:
12Sometimes it's a method, sometimes it's just syntactic sugar that invokes an allocator method. The language matters.
I tell people that a class is like a plan on how to make an object. An object is made from the class by new
. If they need more than that, well, I just don't know what to say. ;-)
Here's a simile that has worked for me in the past.
An object definition is a Jello Mold. "new" is the process that actually makes a Jello snack from that mold. Each Goopy Jello thing that you give to your new neighbors can be different, this one's green, this one has bits of fruit in it, etc. It's its own unique "object." But the mold is the same.
Or you can use a factory analogy or something, (or blueprints vs building).
As far as its role in the syntax, it's just a keyword that lets the compiler know the allocate memory on the heap and run the constructor. There's not much more to it.
new
is a keyword that calls the class constructor of the class to the right of it with the arguments listed inside ().
String str = new String("asdf");
str is defined as being a String class variable using the constructor and argument "asdf"
At least that's how it was presented to me.
Depending on the language new
is a keyword which can be used as an operator or as a modifier. For instance in C# new can be used:
- new operator - used to create objects on the heap and invoke constructors.
- new modifier - used to hide an inherited member from a base class member
For brand new students I would describe new
only as a keyword and leave the modifier out of the discussion. I describe classes as blueprints and new
as the mechanism by which those blueprints turn into something tangible - objects.
You may want to checkout my question: How to teach object oriented programming to procedural programmers for other great answers on teaching OOP to new developers.
To your CS student? Don't sugar-coat it, they need to be able to get their heads around the concepts pretty quickly and using metaphors unrelated to the computer field, while fine for trying to explain it to your 80-year-old grandmother, will not help out a CS student much.
Simply put, tell them that a class is a specification for something and that an object is a concrete instance of that something. All new
does is create a concrete instance based on the specification. This includes both creation (not necessarily class-specific, which is why I'd hesitate to call it a method, reserving that term for class-bound functions) and initialisation (which is class-specific).
In Java,
- new allocates memory for a new class instance (object)
- new runs a class's constructor to initialize that instance
- new returns a reference to that new instance
As far as the relationship between object/instance and class, I sometimes think:
class is to instance as blueprint is to building
In Ruby, I believe it's a instance method on the metaclass. In CLOS it's a generic function called make-instance
but otherwise roughly the same.
In some languages, like Java, new
has special syntax, and the metaclass part is hidden. In the case where you have to teach somebody OO with such a language, I don't know that there's much you can do. (Taking a break from teaching OO and Java to teach a second object system would almost certainly just confuse them further!) Just explain what it does, and that it's a special case.
You can say that a class is a prototype/blueprint for an object. When you give it the keyword new, that prototype/blueprint comes to life. It's like you're giving a breath of life to those dead instance.
Smalltalk: it's an instance method on the metaclass. So "new is a method that returns a newly-allocated instance."
In most object-oriented languages, new
is simply a convention for naming a factory method. But it's only one of many conventions.
In Ruby, for example, it is conventional to name the factory method []
for collection classes. In Python, classes are simply their own factories. In Io, the factory method is generally called clone
, in Ioke and Seph it is called mimic
.
In Smalltalk, factory methods often have more descriptive names than just new:
. Something like fromList:
or with:
.
new
in most languages does some variation of the following:
- designate some memory region for a class instance, and if neccesary, inform the garbage collector about how to free that memory later.
- initialize that memory region in the manner specific to that class, transforming the bytes of raw memory into bytes of a valid instance of the class
- return a reference to the memory location to the caller.