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

override/ new/ <missing keyword> on virtual methods

Oh yes its that chestnut again!
Ive gone over the following
(http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this
subject and performed a few of my own tests.

I have two classes yClass which inherits xClass.
xClass has a virtual method which simply writes a line of text stating its
origin, yClass implements the same method which writes a line of text
stating its origin also (i.e. "From yClass").
I ran the following tests coding the yClass method using new, override or no
keyword at all.

e.g. xClass obj = new yClass();
obj.ShowMessage();

Results

Type-----------------|
Ref Inst Keyword on Inherited Method Method executed from
Class Notes
xClass xClass <none> xClass
Compiler warning, but build successful
xClass xClass new xClass
xClass xClass override xClass
xClass yClass <none> xClass
Compiler warning, but build successful
xClass yClass new xClass
xClass yClass override yClass
yClass yClass <none> yClass
Compiler warning, but build successful
yClass yClass new yClass
yClass yClass override yClass
Now from the results you will first see that when new or override is not
specified then we get a compiler warning -which is correct.
We are told that the use of new (which is the same as <none> minus a
compiler warning) causes the method in the derived class to hide the method
in the base class. However if you look at the results I dont see how the
xClass method is being hidden -and quite honestly the only difference in
behaviour appears when we have a reference type of xClass, object of type
yClass and the override has been used.

Could someone explain in what sense the xClass method is hidden when using
new?
THX for reading!!
--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
Nov 16 '05 #1
5 2568
Mark Broadbent wrote:
Oh yes its that chestnut again!
Ive gone over the following
(http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this
subject and performed a few of my own tests.

I have two classes yClass which inherits xClass.
xClass has a virtual method which simply writes a line of text stating its
origin, yClass implements the same method which writes a line of text
stating its origin also (i.e. "From yClass").
I ran the following tests coding the yClass method using new, override or no
keyword at all.

e.g. xClass obj = new yClass();
obj.ShowMessage();

Results

Type-----------------|
Ref Inst Keyword on Inherited Method Method executed from
Class Notes
xClass xClass <none> xClass
Compiler warning, but build successful
xClass xClass new xClass
xClass xClass override xClass
xClass yClass <none> xClass
Compiler warning, but build successful
xClass yClass new xClass
xClass yClass override yClass
yClass yClass <none> yClass
Compiler warning, but build successful
yClass yClass new yClass
yClass yClass override yClass
Now from the results you will first see that when new or override is not
specified then we get a compiler warning -which is correct.
We are told that the use of new (which is the same as <none> minus a
compiler warning) causes the method in the derived class to hide the method
in the base class. However if you look at the results I dont see how the
xClass method is being hidden -and quite honestly the only difference in
behaviour appears when we have a reference type of xClass, object of type
yClass and the override has been used.

Could someone explain in what sense the xClass method is hidden when using
new?
THX for reading!!


Consider the following code:

public class Foo
{
public virtual void SayHello()
{
Console.WriteLine("Foo says hello!");
}
}

public class Bar : Foo
{
public new void SayHello()
{
Console.WriteLine("Bar says hello!");
}
}

public class FuBar : Foo
{
public override void SayHello()
{
Console.WriteLine("FuBar says hello!");
}

}

class TestClass
{
static void Main(string[] args)
{
Foo foo = new Foo();
Bar bar = new Bar();
FuBar fuBar = new FuBar();

foo.SayHello(); // Output: Foo says hello!
bar.SayHello(); // Output: Bar says hello!
fuBar.SayHello(); // Output: FuBar says hello!

foo = (Foo)bar;
foo.SayHello(); // Output: Foo says hello!

foo = (Foo)fuBar;
foo.SayHello(); // Output: FuBar says hello!
}
}

I hope this helps.
Nov 16 '05 #2
Maybe. Lets get this straight then. Are we saying then that the "hiding"
that is talked about means that the derived object still implements the base
method along with it's own "new" method, but the difference being that the
base method is hidden from the derived object but is still accessible
through the base object.
Wheras an object that has an overridden method does not implement the base
method in the derived object (although it is still accessible through base)
Instead it is "overridden" and will still be accessible through the base
type if the object is cast to that type?
While we are on this subject, is there any point at all to using "virtual"
inside a base class IF you know for a fact that you :-

a. Will never need to "override" any methods in it
b. But still might need to implement a "new" method of the same name in a
derived class

because from what I see the "virtual" keyword is only a requirement when
creating an "override" in a derived class.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:eW**************@TK2MSFTNGP12.phx.gbl...
Mark Broadbent wrote:
Oh yes its that chestnut again!
Ive gone over the following
(http://www.yoda.arachsys.com/csharp/faq/ -thanks Jon!) again regarding this subject and performed a few of my own tests.

I have two classes yClass which inherits xClass.
xClass has a virtual method which simply writes a line of text stating its origin, yClass implements the same method which writes a line of text
stating its origin also (i.e. "From yClass").
I ran the following tests coding the yClass method using new, override or no keyword at all.

e.g. xClass obj = new yClass();
obj.ShowMessage();

Results

Type-----------------|
Ref Inst Keyword on Inherited Method Method executed from Class Notes
xClass xClass <none> xClass
Compiler warning, but build successful
xClass xClass new xClass
xClass xClass override xClass
xClass yClass <none> xClass
Compiler warning, but build successful
xClass yClass new xClass
xClass yClass override yClass
yClass yClass <none> yClass
Compiler warning, but build successful
yClass yClass new yClass
yClass yClass override yClass
Now from the results you will first see that when new or override is not
specified then we get a compiler warning -which is correct.
We are told that the use of new (which is the same as <none> minus a
compiler warning) causes the method in the derived class to hide the method in the base class. However if you look at the results I dont see how the
xClass method is being hidden -and quite honestly the only difference in
behaviour appears when we have a reference type of xClass, object of type yClass and the override has been used.

Could someone explain in what sense the xClass method is hidden when using new?
THX for reading!!


Consider the following code:

public class Foo
{
public virtual void SayHello()
{
Console.WriteLine("Foo says hello!");
}
}

public class Bar : Foo
{
public new void SayHello()
{
Console.WriteLine("Bar says hello!");
}
}

public class FuBar : Foo
{
public override void SayHello()
{
Console.WriteLine("FuBar says hello!");
}

}

class TestClass
{
static void Main(string[] args)
{
Foo foo = new Foo();
Bar bar = new Bar();
FuBar fuBar = new FuBar();

foo.SayHello(); // Output: Foo says hello!
bar.SayHello(); // Output: Bar says hello!
fuBar.SayHello(); // Output: FuBar says hello!

foo = (Foo)bar;
foo.SayHello(); // Output: Foo says hello!

foo = (Foo)fuBar;
foo.SayHello(); // Output: FuBar says hello!
}
}

I hope this helps.

Nov 16 '05 #3
Mark Broadbent wrote:
Maybe. Lets get this straight then. Are we saying then that the "hiding"
that is talked about means that the derived object still implements the base
method along with it's own "new" method, but the difference being that the
base method is hidden from the derived object but is still accessible
through the base object.
Basically the new keyword allows you to provide a completely 'new'
function with the same signature as a function in the base class. This
effectively breaks the inheritance chain for this function; however,
casting the instance back down to the base class will get the 'old'
function back. Phew! Still with me? ... ;)

This might make things slightly clearer:

public class A
{
public virtual void SayHello()
{
Console.WriteLine("Hello from A");
}
}

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

}

public class C : B
{
public new void SayHello()
{
Console.WriteLine("Hello from C");
}
}

If you get an instances of A, B & C like so:

A a = new A();
A b = new B();
A c = new C();

a.SayHello();
// Hello from A

b.SayHello();
// Hello from B

c.SayHello();
// Hello from B

Wheras an object that has an overridden method does not implement the base
method in the derived object (although it is still accessible through base)
Instead it is "overridden" and will still be accessible through the base
type if the object is cast to that type?
While we are on this subject, is there any point at all to using "virtual"
inside a base class IF you know for a fact that you :-

a. Will never need to "override" any methods in it
Obviously, if you don't want to override in the future, don't mark the
function as virtual.
b. But still might need to implement a "new" method of the same name in a
derived class

because from what I see the "virtual" keyword is only a requirement when
creating an "override" in a derived class.


From the MSDN documentation:

A compile-time error occurs unless all of the following are true for an
override declaration:

* An overridden base method can be located as described above.
* The overridden base method is a virtual, abstract, or override method.
In other words, the overridden base method cannot be static or non-virtual.
* The overridden base method is not a sealed method.
* The override declaration and the overridden base method have the same
return type.
* The override declaration and the overridden base method have the same
declared accessibility. In other words, an override declaration cannot
change the accessibility of the virtual method.

Nov 16 '05 #4
Thanks for that. It can get quite complicated!

Thanks for all your help.
--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
"Ed Courtenay" <re*****************************@edcourtenay.co.uk > wrote in
message news:uE**************@TK2MSFTNGP12.phx.gbl...
Mark Broadbent wrote:
Maybe. Lets get this straight then. Are we saying then that the "hiding"
that is talked about means that the derived object still implements the base method along with it's own "new" method, but the difference being that the base method is hidden from the derived object but is still accessible
through the base object.
Basically the new keyword allows you to provide a completely 'new'
function with the same signature as a function in the base class. This
effectively breaks the inheritance chain for this function; however,
casting the instance back down to the base class will get the 'old'
function back. Phew! Still with me? ... ;)

This might make things slightly clearer:

public class A
{
public virtual void SayHello()
{
Console.WriteLine("Hello from A");
}
}

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

}

public class C : B
{
public new void SayHello()
{
Console.WriteLine("Hello from C");
}
}

If you get an instances of A, B & C like so:

A a = new A();
A b = new B();
A c = new C();

a.SayHello();
// Hello from A

b.SayHello();
// Hello from B

c.SayHello();
// Hello from B

Wheras an object that has an overridden method does not implement the base method in the derived object (although it is still accessible through base) Instead it is "overridden" and will still be accessible through the base
type if the object is cast to that type?
While we are on this subject, is there any point at all to using "virtual" inside a base class IF you know for a fact that you :-

a. Will never need to "override" any methods in it


Obviously, if you don't want to override in the future, don't mark the
function as virtual.
b. But still might need to implement a "new" method of the same name in a derived class

because from what I see the "virtual" keyword is only a requirement when
creating an "override" in a derived class.


From the MSDN documentation:

A compile-time error occurs unless all of the following are true for an
override declaration:

* An overridden base method can be located as described above.
* The overridden base method is a virtual, abstract, or override method.
In other words, the overridden base method cannot be static or

non-virtual. * The overridden base method is not a sealed method.
* The override declaration and the overridden base method have the same
return type.
* The override declaration and the overridden base method have the same
declared accessibility. In other words, an override declaration cannot
change the accessibility of the virtual method.

Nov 16 '05 #5
Mark Broadbent wrote:
Thanks for that. It can get quite complicated!

Thanks for all your help.


It is confusing at first, but once you've got the hang of it it's fairly
simple.

Cheers

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #6

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

Similar topics

32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
4
by: Rafael Veronezi | last post by:
I have some questions about override in inheritance, and virtual members. I know that you can you override a method by two ways in C#, one, is overriding with the new keyword, like: public new...
14
by: JPRoot | last post by:
Hi I use the following syntax to have events inherited from base to child classes which works nicely (virtual and override keyword on events). But I am wondering if it is a "supported" way of using...
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...
5
by: Marcel Hug | last post by:
Hi NG ! I'm new in C# and I'm reading a book about the fundamentals and concepts. In the chapter Methods it's written to use virtual, if i would like to override the method in a subclass. This...
4
by: Dave Booker | last post by:
So did the .NET 2.0 working group just run out of steam before it got to a ReadOnlyDictionary<> wrapper? I am publishing events containing a SortedList<>, and obviously I don't want event...
1
by: muler | last post by:
"If an instance method declaration includes the sealed modifier, it must also include the override modifier." The C# Programming Language, § 10.5.5 Sealed Methods Why is this? Thanks,...
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: 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...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.