Method/Instance Precedence?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Method/Instance Precedence?

mchean
Hi:
In Visual Foxpro if one edits the method of the instance of a class the
instance method takes precedence,
and thus overrides the class method unless somewhere in the instance there
is a DoDefault() call.
In the Animal tutorial we have 2 methods #name: one in the Class, and one in
the instance.  When I do
Animal>>#name: (am I getting this nomenclature right?)  am I right in
assuming that the Class method has
precedence?

Also,  in the tutorial it states:

>>However, using #new to create an instance of Animal isn't really ideal
since, as we've seen, we end up with >>an animal that has a nil name. It
would be much better if we could ensure that as each instance is created it
>>is properly initialized with a valid String name. We can do this by adding
an instance creation method that >>takes as its parameter the name of the
animal to be created. Such a method is not applicable to an instance >>of
Animal but rather to the class itself.

How does one determine that it belongs to the Class side as opposed to the
instance, since it is an 'instance' creation method.  My suspicion is that
by adding it to the Class we are ensuring that any subclass would inherit
this behavior?

Finally it seems that the Animal tutorial was written for an older version
of Dolphin as the #new method is not in Class but in Class's Grandparent
Behaviour?

Mike


Reply | Threaded
Open this post in threaded view
|

Re: Method/Instance Precedence?

Jerome Chan
In article <[hidden email]>,
 "Michael Chean" <[hidden email]> wrote:

> Hi:
> In Visual Foxpro if one edits the method of the instance of a class the
> instance method takes precedence,
> and thus overrides the class method unless somewhere in the instance there
> is a DoDefault() call.
> In the Animal tutorial we have 2 methods #name: one in the Class, and one in
> the instance.  When I do
> Animal>>#name: (am I getting this nomenclature right?)  am I right in
> assuming that the Class method has
> precedence?
>

 From my understanding Animal is a class so you are calling the class
method. You can't call an instance method from a class method without an
instance.


Reply | Threaded
Open this post in threaded view
|

Re: Method/Instance Precedence?

Ted Bracht-2
In reply to this post by mchean
Hi Michael,

"Michael Chean" <[hidden email]> wrote in message news:<[hidden email]>...

> Hi:
> In Visual Foxpro if one edits the method of the instance of a class the
> instance method takes precedence,
> and thus overrides the class method unless somewhere in the instance there
> is a DoDefault() call.
> In the Animal tutorial we have 2 methods #name: one in the Class, and one in
> the instance.  When I do
> Animal>>#name: (am I getting this nomenclature right?)  am I right in
> assuming that the Class method has
> precedence?

There is no real precedence between the class method and the instance
method, you either sent the message #name: to the class OR you send it
to the instance. When you have an Animal class>>name: method, it is
typically used to create a new instance of an Animal and give it a
name at the same time. The method would look like this for example:
Animal class>>name: aName
self basicNew name: aName

The 'self basicNew' part of the method creates a new instance. This
instance is then the receiver of the _instance_ message 'name: aName'.

The instance method is only able to talk to an instance, so you have
to have an instance to send the message to. The method would look like
this:
Animal>>name: aName
name := aName

Here the instance variable 'name' is set to the object 'aName'.

So the class method is really a shortcut, doing two thins at the same
time, creating a new instance and setting the name. It uses the
instance method to do the job.

HTH,

Ted

>
> Also,  in the tutorial it states:
>
> >>However, using #new to create an instance of Animal isn't really ideal
> since, as we've seen, we end up with >>an animal that has a nil name. It
> would be much better if we could ensure that as each instance is created it
> >>is properly initialized with a valid String name. We can do this by adding
> an instance creation method that >>takes as its parameter the name of the
> animal to be created. Such a method is not applicable to an instance >>of
> Animal but rather to the class itself.
>
> How does one determine that it belongs to the Class side as opposed to the
> instance, since it is an 'instance' creation method.  My suspicion is that
> by adding it to the Class we are ensuring that any subclass would inherit
> this behavior?
>
> Finally it seems that the Animal tutorial was written for an older version
> of Dolphin as the #new method is not in Class but in Class's Grandparent
> Behaviour?
>
> Mike


Reply | Threaded
Open this post in threaded view
|

Re: Method/Instance Precedence?

Costas Menico
In reply to this post by mchean
On Sun, 12 Aug 2001 17:34:05 -0700, "Michael Chean"
<[hidden email]> wrote:

>Hi:
>In Visual Foxpro if one edits the method of the instance of a class the
>instance method takes precedence,
>and thus overrides the class method unless somewhere in the instance there
>is a DoDefault() call.
>In the Animal tutorial we have 2 methods #name: one in the Class, and one in
>the instance.  When I do
>Animal>>#name: (am I getting this nomenclature right?)  am I right in
>assuming that the Class method has
>precedence?

I think you have may have confusion between Class and Instance methods
and variables.  VFP does not have a concept of class methods nor class
variables.  All methods and variables are instance related, ie can
only be used when you have an instance.

In Smalltalk you can define methods/variables that are only understood
by the Class. So you could have a Class Dog with a method #info

To use it then you can evaluate

        Dog info

without creating an instance of Dog.

However an instance of Dog will not understand #info unless you also
defined it as a method.

The equivalent of Dodefault() is done by sending a message to super.
Just like in VFP a superclass method in ST does not get called
automatically.

So in your new method add the following code to also execute the
superclasses method.

Dog>>new: name
        | dog |
  dog:=super new.
        dog name. name.
        ^dog.

In this case #name: is an instance method.

costas