About Override Class Question

G

Guest

Hi all,

The code as follow:

using System;

class A
{
public virtual void F()
{
Console.WriteLine("A.F");
}
}

class B : A
{
public override void F()
{
Console.WriteLine("B.F");
}

public override string ToString()
{
return "Class B: ToString()";
}
}

class Test
{
static void Main()
{
B b = new B();
A a = b;
a.F();
Console.WriteLine(a.ToString());
}
}


Why the output is:
B.F
Class B: ToString()

Thanks
 
T

Tasos Vogiatzoglou

a is an instance of type B. The A a=b line does not change the instance
type of the variable despite the downcasting.
 
T

Tom Spink

Hi Sunny,

The reason why you are getting the overridden version of the method, is
because you've created an instance of type B in memory, and you are saving
the the object reference in 'b', then copying that object reference to 'a'.
That means that 'a' and 'b' point to the same object in memory, so, you are
calling the overridden method, as opposed to the method in the base class.

You need to declare an instance of A, if you want to use it's methods, as
opposed to the subclass B's methods:

///
A a = new A( );
Console.WriteLine( a.F() );
///

-- Tom Spink
 
M

Marcus Andrén

Have a way change the instance of a to instance of type B, in this example?
Thanks

Why would you want to do this in the first case?

An object is created with a specific type and it is impossible to
change that type. Anyway, the simplest way to do what you seem to want
would be to create a method in class B that creates a new instance of
class A and fills it with all relevant data. Something like this:

public A ConvertToBase()
{
A a = new A();
//Copy properties here
return a;
}

And in the main method do something like this:

B b = new B();
A a = b.ConvertToBase();
 
M

Mark Wilden

Why would you want to do this in the first case?

Because that's what the homework assignment requires, of course.
 
J

Jeff Louie

Mark... The point of the exercise is to demonstrate that overriding
completely
makes the base class implementation invisible and unreachable. This is
an
absolute requirement for base class polymorphism where a set of concrete
classes derive from a common base class. You can store an array of
references
of the base class type and invoke the overridden method and the proper
implementation of the actual concrete class is invoked at runtime using
a vtable
lookup.

http://www.geocities.com/Jeff_Louie/OOP/oop8.htm

which demonstrates the difference between the polymorphic behavior of
override and the hiding behavior of new.

Regards,
Jeff
 
M

Mark Wilden

Thanks, Jeff, but I've understood polymorphism since I first picked up
C++ 15 years ago.

My point was that, in my opinion, the OP was requesting help for a
homework assignment. :)
 

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

Similar Threads


Top