constructors and inheritance 
Author Message
 constructors and inheritance

Is there a way to direct call the super's constructor in the subclass?
Is there a prefered way of doing it? Basically, i see three ways of
doing it:

1. call the constructor directly
2. use an event, say Finish_Init, in the super class' contructor
3. create a method which before the super's and the inherited's
constructor calls

Obviously I'd prefer the first option but i can't seem to find any
documentation on it.
--
If you build something from the inside out,
you'll have no difficulties penetrating from the outside in



Mon, 19 Jan 2004 21:57:08 GMT  
 constructors and inheritance

Quote:

> Is there a way to direct call the super's constructor in the subclass?

Yes.  Just say MySuperclass(arg1, arg2), where "MySuperclass" is the
name of the superclass.

Cheers,
- Joe

--
,------------------------------------------------------------------.
|    Joseph J. Strout         Check out the Mac Web Directory:     |

`------------------------------------------------------------------'



Mon, 19 Jan 2004 22:50:07 GMT  
 constructors and inheritance

Quote:



> > Is there a way to direct call the super's constructor in the subclass?

> Yes.  Just say MySuperclass(arg1, arg2), where "MySuperclass" is the
> name of the superclass.

> Cheers,
> - Joe

thanks :)
-Eli
--
If you build something from the inside out,
you'll have no difficulties penetrating from the outside in


Mon, 19 Jan 2004 23:47:45 GMT  
 constructors and inheritance

Quote:

> Is there a way to direct call the super's constructor in the subclass?

I noticed Joe answered your question, but note one interesting aspect
of this.  Suppose you have a class FooA with a constructor like this:

Sub FooA(a as integer, b as integer)

Now suppose you make a class FooB subclassed from FooA.  But you don't
supply a constructor for FooB.  What happens when you make a new FooB?
You have to supply two integers, like this:

dim b as FooB

b = new FooB(1,2)

And the FooA constructor will be called.

Pretty cool, I thought when I encountered that!



Tue, 20 Jan 2004 02:54:27 GMT  
 constructors and inheritance

Quote:



> > Is there a way to direct call the super's constructor in the subclass?

> I noticed Joe answered your question, but note one interesting aspect
> of this.  Suppose you have a class FooA with a constructor like this:

> Sub FooA(a as integer, b as integer)

> Now suppose you make a class FooB subclassed from FooA.  But you don't
> supply a constructor for FooB.  What happens when you make a new FooB?
> You have to supply two integers, like this:

> dim b as FooB

> b = new FooB(1,2)

> And the FooA constructor will be called.

Yes. The rule, which is clearly enunciated in the new edition (I'm sorry I
wasn't clear in the first edition), is that RB looks up the hierarchy for
the first class that has a constructor and stops. If you want to trigger
constructors further up the hierarchy you have to do it manually. There's
nothing special about doing it manually; you're just calling a method that
you've inherited. So if A has a subclass B, and A has a constructor, then
there is a method A and B inherits it, so B can say:

  A

Destructors, on the other hand, are all called automatically all the way
up the hierarchy. m.

--

*** REALbasic, The Definitive Guide! ***
http://www.oreilly.com/catalog/realbasic/



Tue, 20 Jan 2004 05:08:02 GMT  
 constructors and inheritance


Quote:
>> Sub FooA(a as integer, b as integer)

>> Now suppose you make a class FooB subclassed from FooA.  But you don't
>> supply a constructor for FooB.  What happens when you make a new FooB?
>> You have to supply two integers, like this:

>> dim b as FooB

>> b = new FooB(1,2)

>> And the FooA constructor will be called.

>... RB looks up the hierarchy for
> the first class that has a constructor and stops...

I think of the constructor as the method with the same name as the class. As
fooA has no fooB method I didnt automatically think a constructor would be
called. If constructors were named "constructor" I think I would have
noticed the inheritance right away.

BTW, reading that part of your book on class inheritance is when 'everything
fell into place' :)

Quote:
> A

> Destructors, on the other hand, are all called automatically all the way
> up the hierarchy. m.

I dont use destructors much but this is very good to know! I love learning
things I didnt know I should know.

-Laz



Thu, 05 Feb 2004 10:37:38 GMT  
 constructors and inheritance
Laz - take a look at the new version of chapter 4 (it's online - see my
sig). I think I'm teaching and documenting this stuff a lot better than I
was in the first edition. :-) m.

----


Quote:



> >> Sub FooA(a as integer, b as integer)

> >> Now suppose you make a class FooB subclassed from FooA.  But you don't
> >> supply a constructor for FooB.  What happens when you make a new FooB?
> >> You have to supply two integers, like this:

> >> dim b as FooB

> >> b = new FooB(1,2)

> >> And the FooA constructor will be called.

> >... RB looks up the hierarchy for
> > the first class that has a constructor and stops...

> I think of the constructor as the method with the same name as the class. As
> fooA has no fooB method I didnt automatically think a constructor would be
> called. If constructors were named "constructor" I think I would have
> noticed the inheritance right away.

> BTW, reading that part of your book on class inheritance is when 'everything
> fell into place' :)

> > A

> > Destructors, on the other hand, are all called automatically all the way
> > up the hierarchy. m.
> I dont use destructors much but this is very good to know! I love learning
> things I didnt know I should know.

--

          *** REALbasic: The Definitive Guide! ***
http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt
             -- The Second Edition is coming! --


Thu, 05 Feb 2004 23:21:58 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Public inheritance (was: Restricting inheritance)

2. Different kind of inheritance/Multiple Inheritance

3. Inheritance: Interface vs Implementation (was: Questions on inheritance)

4. Haskell Q: constructors vs. constructor classes?

5. Classes Constructors

6. Casting and constructor method conflict

7. Overloading constructor methods

8. Brace constructor for VW

9. Constructors

10. Collections, Constructors and Destructors

11. Can windows have constructors ?

 

 
Powered by phpBB® Forum Software