473,386 Members | 1,693 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,386 software developers and data experts.

new and Virtual.. differences...?

Hi there,

while trying to learn polymorphism, i am confused between
the 2.

================================================== =====

class Person
{
...
public void someMethod()
{
//do something
}
}
class Guy :Person
{
public new void someMethod()
{
// do something
}
}

===============================================
what is the difference between using the "New" keyword
(int the above code) and using a "Virtual" in the base
class and the "override" in the derive class?

thank you very much.

Nov 15 '05 #1
1 1195
superfrog

Consider this example below. WHen you look at the output you should get

A-X
A-Y
A-X
B-Y

What this shows is that when the base class runs PrintIt and calls this.X or this.Y it runs the
inherited class's implementation on the virtual Y method but doesn't see the new implemnetation
of X so it runs its own version of it.
using System;
namespace TestNewAndVirtual
{
public class A
{
public string X
{
get { return "A-X"; }
}
public virtual string Y
{
get { return "A-Y"; }
}
public virtual void PrintIt()
{
Console.WriteLine ( this.X );
Console.WriteLine ( this.Y );
}
}
public class B : A
{
new public string X
{
get { return "B-X"; }
}
public override string Y
{
get { return "B-Y"; }
}
public static void Main()
{
A a = new A();
B b = new B();
a.PrintIt();
b.PrintIt();
Console.ReadLine();
}
}
}
"superfrog" <me@azellow.com> wrote in message news:0a****************************@phx.gbl...
Hi there,

while trying to learn polymorphism, i am confused between
the 2.

================================================== =====

class Person
{
...
public void someMethod()
{
//do something
}
}
class Guy :Person
{
public new void someMethod()
{
// do something
}
}

===============================================


what is the difference between using the "New" keyword
(int the above code) and using a "Virtual" in the base
class and the "override" in the derive class?

thank you very much.

Nov 15 '05 #2

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

Similar topics

20
by: Raymond Lewallen | last post by:
I read this on this website page http://www.vbip.com/books/1861004915/chapter_4915_06.asp: Unlike many object-oriented languages, all methods in VB.NET are virtual. Now in BOL, Under...
9
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ }...
9
by: ajay.somni | last post by:
I would like to know what is virtual function in c++. Besides what is virtual.
3
by: news.microsoft.com | last post by:
Hi, It is possible to override a non virtual method with the "new" keyword So how is this different from specifying a method as virtual then providing the override keyword? Is there any...
14
by: Lorenzo | last post by:
Hello, I have a web application with a virtual directory in it. With 'virtual directory' i mean a folder whose physical path is different from the physical path of the application, but in which...
4
by: Gert Kok | last post by:
The microsoft page http://msdn2.microsoft.com/en-us/library/9fkccyh4.aspx states: Remarks (nr 4) Virtual properties behave like abstract methods, except for the differences in declaration...
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
5
by: Jose Fernandez | last post by:
Hello I have been reading about the differences between virtual and abstract keywords... What i understand is that with VIRTUAL keyword, you can actually write the whole method and override it...
2
by: lkrueger | last post by:
Hello-- I have an .html page that I'm trying to use a SSI (footer) and other headers, but it's not showing up. I've tried multiple combinations, but nothing is working. I understand the...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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...

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.