473,699 Members | 2,702 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

About Override Class Question

Hi all,

The code as follow:

using System;

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

class B : A
{
public override void F()
{
Console.WriteLi ne("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.WriteLi ne(a.ToString() );
}
}
Why the output is:
B.F
Class B: ToString()

Thanks
Nov 17 '05 #1
8 1300
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.WriteLi ne( a.F() );
///

-- Tom Spink

Sunny wrote:
Hi all,

The code as follow:

using System;

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

class B : A
{
public override void F()
{
Console.WriteLi ne("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.WriteLi ne(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***@discussi ons.microsoft.c om> 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
3053
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 to have an easier time understanding what I do. Therefore this weekend I'm going to spend 3 days just writing comments. Before I do it, I thought I'd ask other programmers what information they find useful. Below is a typical class I've...
2
1572
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 level. There is a class TeachingAssistent that use multiple inheritance from both Student and Employee. There is a method named getName is class Person.
11
19455
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 base class) then override(in the derived class), but fails to compare and contrast the two... I've read this (short snippet)three times and by the looks of it they both do exactly the same thing- allow you to derive from the base and save...
3
1607
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 two or more derived classes that use the same base class. It allows you to write generic code that targets the base class rather than having to write specific code for each object type." And here is the example in the book:
2
1329
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 hope that somebody can explain more about why this works. The sequence is the following.
1
224
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 class hieracy that looks like public class Base { public virtual void myMethod()
4
6567
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 right? And, why not use "new" instead of using "virtual"? And the last question, what is the differences between a abstract method and a interface?
5
1456
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 function' in the subclass. It works. Then i removed them in both classes, like here below: Public Class ford .....
5
2230
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 keyword abstract. I have also another class called MyDerivedClass which is a sublass to MyClass. Now to my question when I implement this method myTest in class
4
1766
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 class. I have two questions about this method. Question number 1: If I use "(Cow)animal" insted of "animal as Cow" I get the following compile
0
8685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8613
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7745
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.