By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

override/ new/ <missing keyword> on virtual methods

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.