473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

About Override Class Question

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
Nov 17 '05 #1
8 1291
a is an instance of type B. The A a=b line does not change the instance
type of the variable despite the downcasting.

Nov 17 '05 #2
Have a way change the instance of a to instance of type B, in this example?
Thanks

"Tasos Vogiatzoglou" wrote:
a is an instance of type B. The A a=b line does not change the instance
type of the variable despite the downcasting.

Nov 17 '05 #3
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

Sunny wrote:
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

Nov 17 '05 #4
On Fri, 30 Sep 2005 00:40:02 -0700, "Sunny"
<Su***@discussions.microsoft.com> wrote:
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();
--
Marcus Andrén
Nov 17 '05 #5
> Why would you want to do this in the first case?

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

Nov 17 '05 #6
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

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7
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. :)

Nov 17 '05 #8
Oops. I meant to reply to sunny.

Regards,
Jeff

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: lawrence | last post by:
I've been bad about documentation so far but I'm going to try to be better. I've mostly worked alone so I'm the only one, so far, who's suffered from my bad habits. But I'd like other programmers...
2
by: Tony Johansson | last post by:
Hello Experts!! Here we use multiple inheritance from two classes.We have a class named Person at the very top and below this class we have a Student class and an Employee class at the same...
11
by: z_learning_tester | last post by:
Hello, yes another beginner question that I'm sure is obvious to many here :-) My book is so bad. Really. It uses the exact same example of code for using the new kw and for using virtual(in the...
3
by: John Salerno | last post by:
Along with events and delegates, polymorphism has been something I sort of struggle with every now and then. First, let me quote the book I'm reading: "Polymorphism is most useful when you have...
2
by: Tony Johansson | last post by:
Hello!! Below I have three classes. They are Base, Sub and Test. Sub is inheriting from Base. The main issue here is to pass the reference of Test up to the base class Base. This work but I...
1
by: tired_of_spaghetti | last post by:
As I understand it, the virtual keyword is necessary and also in a derived class you must use the override keyword so what if : 1) you don't use the override keyword ? 2) Suppose you have a...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
5
by: Ben | last post by:
Hi, i defined a function in the base class 'ford' and the same function (with different output) in subclass "peugeot". I first put 'Overridable function' in the base class and 'Overrides...
5
by: Tony Johansson | last post by:
Hello! Here I have an Interface called ITest and a class called MyClass which derive this intrface. As you can see I don't implement this method myTest in class MyClass because i use the...
4
by: Tony | last post by:
Hello! Below I have a complete working program.with some simple classes one of these is a generic class. The question is about this method GetCows() {...} which is a member in the generic...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.