M
Mark Broadbent
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
=============
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
=============