about inheritance and understanding

T

Tony Johansson

Hello!


I have this inheritance below. My base class is Car and derived from that is
SportCar.
When I create an instance of SportCar I assume that I can consider to have
an instance of my Car object within the instance of SportCar. Does that
sound reasonable ?
If we can agree upon that I come to next question.

If I now make the Car class abstract which mean that it's not possible to
have an instance of a Car class.

When I now create an instance of a SportCar where the base class Car is
abstract does the runtime
create an instance of Car behind the scene because I can still use
base.Drive() in method Foo in class SportCar.

class Car
{
public void Drive()
{
}
}

class SportCar : Car
{
public void Foo()
{
base.Drive();
}
}

//Tony
 
P

Peter Duniho

[...]
When I now create an instance of a SportCar where the base class Car is
abstract does the runtime
create an instance of Car behind the scene because I can still use
base.Drive() in method Foo in class SportCar.

No. And this is true whether Car is abstract or not.

I know it's long, but please read my previous reply in the previous,
nearly-identical thread you started. If you are thinking of the base
class and derived class as separate instances/objects, you are likely
misunderstanding how inheritance works. In that reply, I offered an
alternative way to think about it which I think is much more useful.

Short version: if SportCar inherits Car, then when you create a SportCar,
a) you create only one object, and b) that one object is simultaneously
both a SportCar and a Car. SportCar doesn't contain a Car, and there's no
separate Car instance being created. And again, this is exactly the same
whether Car is abstract or not.

Pete
 
P

Patrice

No it doesn't create an instance. It IS the instance. What I mean is that
it basically it makes a copy of the base class binary layout and then
add/change this layout depending on what you declared in the derived class
so it doesn't have to create an hidden instance of the base class. The
underlying implementation is done so that it already knows to do what the
base class do...

For example you have a table that holds pointers to the class virtual
methods. Overriding a method will just make the corresponding pointer in
this table point to a new method rather than to the same code than the base
class. Calling MyBase will just go into the base class method table to call
the corresponding pointer etc...

The good old Turbo Pascal documentation had a whole chapter describing the
implementation. It is the best description of OOP implementation I've seen
so far and .NET is likely similar...
 
T

Tony Johansson

Hello!

Can you please resend your answer for previous mail thread concering
inheritance because I can't fint it for some reason.

//Tony

Peter Duniho said:
[...]
When I now create an instance of a SportCar where the base class Car is
abstract does the runtime
create an instance of Car behind the scene because I can still use
base.Drive() in method Foo in class SportCar.

No. And this is true whether Car is abstract or not.

I know it's long, but please read my previous reply in the previous,
nearly-identical thread you started. If you are thinking of the base
class and derived class as separate instances/objects, you are likely
misunderstanding how inheritance works. In that reply, I offered an
alternative way to think about it which I think is much more useful.

Short version: if SportCar inherits Car, then when you create a SportCar,
a) you create only one object, and b) that one object is simultaneously
both a SportCar and a Car. SportCar doesn't contain a Car, and there's no
separate Car instance being created. And again, this is exactly the same
whether Car is abstract or not.

Pete
 
A

Alan

On Sep 21, 3:54 am, "Tony Johansson" <[email protected]>
wrote:

There is only one object which is the union of the fields and virtual
function tables. Your SportCar class will undoubtedly have virtual
functions to tailor the SportCar specific behavior. See vtables here
to get a grasp of that. http://en.wikipedia.org/wiki/Vtable

-- Alan
-- cleartopartlycloudy.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top