inheritance

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

If I don't override method ToString in class Car will
ToString in the dynamic class SportCar be called in main because method
ToString is defined as virtual.
I can understand that.

But if I override this ToString method in class Car will class Car be used
when I use car:ToString in main.
I just wonder when I override ToString in class Car why is then class car
used instead of Sportcar that
was used when no override of ToString existed ??

Normally I know how it works if I only have user defined methods but here we
have this ToString which
cause me to be a litle bit uncertain.

public static void Main()
{
Car car = new SportCar();
Console.WriteLine(car.ToString());
}

public class Car
{
public string ToString()
{
return "Car";
}
}

public class SportCar : Car
{}

//Tony
 
Tony said:
Hi!

If I don't override method ToString in class Car will
ToString in the dynamic class SportCar be called in main because method
ToString is defined as virtual.

Sorry, I don't understand this sentence. For one thing, your SportCar
class doesn't define a ToString.
I can understand that.

But if I override this ToString method in class Car will class Car be used
when I use car:ToString in main.

Do you mean, "will the method defined in class Car be used ..."? The
answer is yes, unless you've overridden it in SportCar.
I just wonder when I override ToString in class Car why is then class car
used instead of Sportcar that

Again, you don't have a definition of ToString in SportCar.
 
Tony Johansson said:
Hi!

If I don't override method ToString in class Car will
ToString in the dynamic class SportCar be called in main because method
ToString is defined as virtual.
I can understand that.

But if I override this ToString method in class Car will class Car be used
when I use car:ToString in main.
I just wonder when I override ToString in class Car why is then class car
used instead of Sportcar that
was used when no override of ToString existed ??

Normally I know how it works if I only have user defined methods but here
we have this ToString which
cause me to be a litle bit uncertain.

public static void Main()
{
Car car = new SportCar();
Console.WriteLine(car.ToString());
}

public class Car
{
public string ToString()
{
return "Car";
}
}

public class SportCar : Car
{}

//Tony

Is it possible that you've missed the fact that all classes implicitly
inherit from class Object, and that Object defines a ToString() method that
will automatically be used by any user defined class like Car above, unless
overridden (as you've done)?

As you don't show any method definitions for your class SportCar, it
inherits the Car version of ToString() unless you provide an override
definition.

Your code in Main()

Car car = new SportCar();
Console.WriteLine(car.ToString());

Creates an object variable of type Car and an object of type SportCar, whose
address is stored in car. The next statemant will call the Car version of
ToString (inherited) if there is no ToString override in SportCar. If there
*is* a ToString override in SportCar, the SportCar version will be used
because of the object type.
 
Tony said:
Hi!

If I don't override method ToString in class Car will
ToString in the dynamic class SportCar be called in main because method
ToString is defined as virtual.
I can understand that.

I don't understand your statement. Your code example doesn't show an
implementation of ToString() in the SportCar class. And why do you call
the SportCar class "the dynamic class SportCar"? What is "dynamic"
about it?
But if I override this ToString method in class Car will class Car be used
when I use car:ToString in main.

Your code example doesn't show an override of ToString(). Instead,
you've posted code that shouldn't compile. Do you mean for the
ToString() method in the Car class to be "override" or "new". Note that
the two have very different meanings ("override" would typically be what
you use).
I just wonder when I override ToString in class Car why is then class car
used instead of Sportcar that
was used when no override of ToString existed ??

If there is no override of ToString() in the SportCar class, how could
an override of ToString() in the SportCar class be called? Why
_wouldn't_ the override in Car be called? After all, as far as the
SportCar class is concerned, the override in the Car class is just like
the original virtual method in the System.Object class.
Normally I know how it works if I only have user defined methods but here we
have this ToString which
cause me to be a litle bit uncertain.

The virtual System.Object.ToString() method is _exactly_ the same as any
other user-defined virtual method. If you truly understand how
user-defined virtual methods work, then you also understand this
particular virtual method.

Conversely, if you don't understand this particular virtual method, then
I'd assert you don't truly understand virtual methods generally, even
for user-defined methods.

Pete
 
Tony said:
Hi!

If I don't override method ToString in class Car will
ToString in the dynamic class SportCar be called in main because method
ToString is defined as virtual.
I can understand that.

But if I override this ToString method in class Car will class Car be used
when I use car:ToString in main.
I just wonder when I override ToString in class Car why is then class car
used instead of Sportcar that
was used when no override of ToString existed ??

Normally I know how it works if I only have user defined methods but here we
have this ToString which
cause me to be a litle bit uncertain.

public static void Main()
{
Car car = new SportCar();
Console.WriteLine(car.ToString());
}

public class Car
{
public string ToString()
{
return "Car";
}
}

public class SportCar : Car
{}

//Tony

I don't know which cases you compare, as you only showed one of them,
but this is how it works:

When you call a virtual method it uses the actual type of the object
(SportCar), not the type of the reference (Car).

If SportCar overrides the ToString method that will be used, otherwise
it uses the implementation from the closest base class that has one, in
this case the Car class.

If the Car class wouldn't override ToString either, the implementation
from the base class Object would be used.
 
Back
Top