473,839 Members | 1,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

hiding vs. overriding

What is the purpose of hiding intead of overriding a method? I have googled
the question but haven't found anything that makes any sense of it.

In the code below, the only difference is that when the Poodle is upcast to
the Dog (in its wildest dreams) it then says "bow wow" where the bernard
always says "woof" (see code). Basically, it appears that I'm hiding the
poodle's speak method from everything except the poodle.

Why would I want to do this?
bob

-------------------------------------------------------------------------------------
class Pets {
[STAThread]
static void Main(string[] args) {
Bernard b = new Bernard();
Poodle p = new Poodle();

b.speak();
p.speak();

((Dog)b).speak( );
((Dog)p).speak( );

pause();
}

static void pause() {
Console.Write(" \npaused");
Console.ReadLin e();
}
}

class Dog {
public virtual void speak() { print("bow wow"); }

protected void print (string msg) {
Console.WriteLi ne( "{0}: {1}\n", this.GetType(). ToString(), msg);
}
}

class Bernard : Dog {
public override void speak() { print("woof"); }
}
class Poodle : Dog {
public new void speak() { print("yip yip yip yip yip yip yip yip yip
yip"); }
}
Nov 17 '05 #1
17 2925
Bob,

Quite honestly, I can't see the reason one would want to do this. I
haven't found a truly GOOD reason yet to use method hiding, and I eschew
it's use.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Weiner" <bo*@engr.uconn .edu> wrote in message
news:eB******** ******@TK2MSFTN GP14.phx.gbl...
What is the purpose of hiding intead of overriding a method? I have
googled the question but haven't found anything that makes any sense of
it.

In the code below, the only difference is that when the Poodle is upcast
to the Dog (in its wildest dreams) it then says "bow wow" where the
bernard always says "woof" (see code). Basically, it appears that I'm
hiding the poodle's speak method from everything except the poodle.

Why would I want to do this?
bob

-------------------------------------------------------------------------------------
class Pets {
[STAThread]
static void Main(string[] args) {
Bernard b = new Bernard();
Poodle p = new Poodle();

b.speak();
p.speak();

((Dog)b).speak( );
((Dog)p).speak( );

pause();
}

static void pause() {
Console.Write(" \npaused");
Console.ReadLin e();
}
}

class Dog {
public virtual void speak() { print("bow wow"); }

protected void print (string msg) {
Console.WriteLi ne( "{0}: {1}\n", this.GetType(). ToString(), msg);
}
}

class Bernard : Dog {
public override void speak() { print("woof"); }
}
class Poodle : Dog {
public new void speak() { print("yip yip yip yip yip yip yip yip yip
yip"); }
}

Nov 17 '05 #2
Not the answer I was expecting but it is good to hear. I thought I was
missing something big.

Thanks,
bob

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:%2******** **********@TK2M SFTNGP14.phx.gb l...
Bob,

Quite honestly, I can't see the reason one would want to do this. I
haven't found a truly GOOD reason yet to use method hiding, and I eschew
it's use.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Bob Weiner" <bo*@engr.uconn .edu> wrote in message
news:eB******** ******@TK2MSFTN GP14.phx.gbl...
What is the purpose of hiding intead of overriding a method? I have
googled the question but haven't found anything that makes any sense of
it.

In the code below, the only difference is that when the Poodle is upcast
to the Dog (in its wildest dreams) it then says "bow wow" where the
bernard always says "woof" (see code). Basically, it appears that I'm
hiding the poodle's speak method from everything except the poodle.

Why would I want to do this?
bob

-------------------------------------------------------------------------------------
class Pets {
[STAThread]
static void Main(string[] args) {
Bernard b = new Bernard();
Poodle p = new Poodle();

b.speak();
p.speak();

((Dog)b).speak( );
((Dog)p).speak( );

pause();
}

static void pause() {
Console.Write(" \npaused");
Console.ReadLin e();
}
}

class Dog {
public virtual void speak() { print("bow wow"); }

protected void print (string msg) {
Console.WriteLi ne( "{0}: {1}\n", this.GetType(). ToString(), msg);
}
}

class Bernard : Dog {
public override void speak() { print("woof"); }
}
class Poodle : Dog {
public new void speak() { print("yip yip yip yip yip yip yip yip yip
yip"); }
}


Nov 17 '05 #3
Bob,
I use hiding primarily for version control.

For example I release V1 of my type that inherits from base. My type has a
SpecificMethod, where as base does not.

Along comes version 2 of base & it adds a SpecificMethod that is
incompatible with my type's SpecificMethod. Hiding ensures that my types
SpecificMethod does not wreak havoc with base's SpecificMethod & visa versa,
with out me needing to do some serious refactoring to my V2 of my type to be
compatible with V2 of base.
The other place I use hiding is for adding Attributes to properties of
controls in Windows Forms. For example changing the default
(DefaultAttribu te) on non-overridable Properties of controls... In this case
I ensure my property simply calls the base property...

Hope this helps
Jay

"Bob Weiner" <bo*@engr.uconn .edu> wrote in message
news:eB******** ******@TK2MSFTN GP14.phx.gbl...
| What is the purpose of hiding intead of overriding a method? I have
googled
| the question but haven't found anything that makes any sense of it.
|
| In the code below, the only difference is that when the Poodle is upcast
to
| the Dog (in its wildest dreams) it then says "bow wow" where the bernard
| always says "woof" (see code). Basically, it appears that I'm hiding the
| poodle's speak method from everything except the poodle.
|
| Why would I want to do this?
| bob
|
| -------------------------------------------------------------------------------------
|
|
| class Pets {
| [STAThread]
| static void Main(string[] args) {
| Bernard b = new Bernard();
| Poodle p = new Poodle();
|
| b.speak();
| p.speak();
|
| ((Dog)b).speak( );
| ((Dog)p).speak( );
|
| pause();
| }
|
| static void pause() {
| Console.Write(" \npaused");
| Console.ReadLin e();
| }
| }
|
| class Dog {
| public virtual void speak() { print("bow wow"); }
|
| protected void print (string msg) {
| Console.WriteLi ne( "{0}: {1}\n", this.GetType(). ToString(), msg);
| }
| }
|
| class Bernard : Dog {
| public override void speak() { print("woof"); }
| }
| class Poodle : Dog {
| public new void speak() { print("yip yip yip yip yip yip yip yip yip
| yip"); }
| }
|
|
Nov 17 '05 #4
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in news:
#s************* *@TK2MSFTNGP14. phx.gbl:
Quite honestly, I can't see the reason one would want to do this. I
haven't found a truly GOOD reason yet to use method hiding, and I eschew
it's use.


There are useful cases of hiding, but they are rare. In fact I didnt reply to this earlier as I could not
think of a good examle. But I have run across them and used it before.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/
Nov 17 '05 #5
haha, sorry, but it's kind of like saying "Yes, bigfoot exists, but I
have never seen him."

It's nothing personal, and I'm not putting down the post, it just made
me laugh when I read it.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Chad Z. Hower aka Kudzu" <cp**@hower.org > wrote in message
news:Xn******** *************** ***@127.0.0.1.. .
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in news:
#s************* *@TK2MSFTNGP14. phx.gbl:
Quite honestly, I can't see the reason one would want to do this. I
haven't found a truly GOOD reason yet to use method hiding, and I eschew
it's use.


There are useful cases of hiding, but they are rare. In fact I didnt reply
to this earlier as I could not
think of a good examle. But I have run across them and used it before.
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Empower ASP.NET with IntraWeb
http://www.atozed.com/IntraWeb/

Nov 17 '05 #6
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in news:em******** ******@TK2MSFTN GP09.phx.gbl:
haha, sorry, but it's kind of like saying "Yes, bigfoot exists,
but I
have never seen him."
Oh no, I've seen him. ;) I just cant remember where right now. Im sure I could dig through my archives
and find the pictures - but to be honest Im not going to spend 30 minutes or more digging up info for a
newsgroup reply....
It's nothing personal, and I'm not putting down the post, it just
made
me laugh when I read it.


Understood. ;)
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programmin g is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
Nov 17 '05 #7
"Bob Weiner" <bo*@engr.uconn .edu> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Not the answer I was expecting but it is good to hear. I thought I was
missing something big.


Hi Bob.
I think you are missing something big. Something enormous.
I'm thinking about performance and versioning.

This entire topic is why most Delphites think of C# and .NET as a
Delphi-derived platform and not a Java rip-off. The only thing .NET has in
common with Java is the Object class. Delphites understand .NET from day one
while Javaites have some learning to do. Anders Hjelsberg choosed to realize
the default-non-virtual model from Delphi into .NET.

Consider Java. Java methods comes in two flawors; (implicit) virtual and
(explicit) final. Hence all methods are virtual if you don't mark them as
final. This is why Java has no 'override' keyword. If a subclass introduces
the method Foo() and that method exists in any superclass you override it.
Period.

This is dangerous when it comes to versioning. I might give you a class 1.0
with 10 methods and you choose to subclass it with 5 methods. Then I release
a 1.1 version of my cool and useful class, but I've added 3 methods and one
of them has the same name as one of yours.

Without your knowledge you have just overidden a method. As there are no
explicit overriding mechanism in Java, the compiler will happily compile and
override. The problems and obscure bugs from this scheme is two-folded. Your
method is now part of a polymorphic behaviour without support for it. When
my code calls my method your method gets called. Your method will
(obviously) not call on super() and hence my important code will not get
executed. My method might change the object state and then assume that some
actions where committed (that was not executed). Do you see the problem?

This is a serious design-flaw in the Java language and this is why we see so
little inheritance in Java. Javaites tend to use final classes that derives
from object or beans in conjunction with interfaces to protect themselves
from these kind of unwanted overriding.

Also, virtual methods are not good for performance. A virtual method cannot
be statically linked. Whenever you call on a virtual method you have a level
of indirection as the runtime has to check what type the object really is
and call it's method. More on this below.

Delphi and .NET have solved this problem gracefully by including a third
mode that is default. Non-virtual methods. With non-virtual methods we are
no longer afraid of subclassing. Just compare TForm in Delphi 1.0 and Delphi
7.0 and you can see that Borland is not afraid of having many superclasses
and adding a lot of stuff to them. Same is true for .NET as it is nothing
but a rip-off from VCL and WFC. Not sure if it can be called a rip-off as
Hjelsberg was involved in all three of them.

Anyways, as methods are non-virtual by default the compiler can statically
link a method at compile-time. This is done by the JITer. An example:

MySuperClass o = new MySubClass();
o.DoStuff(); // method is non-virtual and the method in MySuperClass will be
called without hesitation. This is fast.
o.DoMyOverridde nStuff() //method is virtual and runtime have to check and
realize that o is typeof MySubClass and call the correct method. This hurts.

Now for some funny facts. The jitter in Java actually do support non-virtual
methods. The java runtime marks all leaf-classes (classes that have no
subclasses) and call on methods non-virtual. This is why the java runtime is
so much faster these days compared to older runtimes. The difference is that
..NET sport non-virttuality in the language while Java does not.

Now for your question about why you have to reintroduce non-virtual methods.
This is just to protect you from the flaws in Java. Virtual methods can be
overriden or reintroduced. Non-virtual methods can only be reintroduced.
Delphi actually uses the keyword 'reintroduce' while C# reuses the keyword
'new'. If you fail to do so; in Delphi you get a warning, in C# you get an
compile error. I like the latter.

This is a great safety-net. Whenever a subclass sports the same method as a
method in a superclass you cannot compile until you accept that. You can
reintroduce the method and if it's virtual you can override it. But the
great thing is that you have to respond and make a choice.

Then what is the correct choice?

Well, I think that reintroducing a method is a bad thing. If a superclass
wanna DoStuff() and your subclass wanna DoStuff() and these thingies are
different things; you don't wanna reintroduce stuff but rename your method
into DoThings().

My conclusion: Never ever reintroduce methods, but love the compiler for
stopping you from unwanted polymorphism and the weird bugs that follows.

Happy Coding
- Michael S





Nov 17 '05 #8
"Bob Weiner" <bo*@engr.uconn .edu> wrote in message
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
Not the answer I was expecting but it is good to hear. I thought I was
missing something big.


Hi Bob.
I think you are missing something big. Something enormous.
I'm thinking about performance and versioning.

This entire topic is why most Delphites think of C# and .NET as a
Delphi-derived platform and not a Java rip-off. The only thing .NET has in
common with Java is the Object class. Delphites understand .NET from day one
while Javaites have some learning to do. Anders Hjelsberg choosed to realize
the default-non-virtual model from Delphi into .NET.

Consider Java. Java methods comes in two flawors; (implicit) virtual and
(explicit) final. Hence all methods are virtual if you don't mark them as
final. This is why Java has no 'override' keyword. If a subclass introduces
the method Foo() and that method exists in any superclass you override it.
Period.

This is dangerous when it comes to versioning. I might give you a class 1.0
with 10 methods and you choose to subclass it with 5 methods. Then I release
a 1.1 version of my cool and useful class, but I've added 3 methods and one
of them has the same name as one of yours.

Without your knowledge you have just overidden a method. As there are no
explicit overriding mechanism in Java, the compiler will happily compile and
override. The problems and obscure bugs from this scheme is two-folded. Your
method is now part of a polymorphic behaviour without support for it. When
my code calls my method your method gets called. Your method will
(obviously) not call on super() and hence my important code will not get
executed. My method might change the object state and then assume that some
actions where committed (that was not executed). Do you see the problem?

This is a serious design-flaw in the Java language and this is why we see so
little inheritance in Java. Javaites tend to use final classes that derives
from object or beans in conjunction with interfaces to protect themselves
from these kind of unwanted overriding.

Also, virtual methods are not good for performance. A virtual method cannot
be statically linked. Whenever you call on a virtual method you have a level
of indirection as the runtime has to check what type the object really is
and call it's method. More on this below.

Delphi and .NET have solved this problem gracefully by including a third
mode that is default. Non-virtual methods. With non-virtual methods we are
no longer afraid of subclassing. Just compare TForm in Delphi 1.0 and Delphi
7.0 and you can see that Borland is not afraid of having many superclasses
and adding a lot of stuff to them. Same is true for .NET as it is nothing
but a rip-off from VCL and WFC. Not sure if it can be called a rip-off as
Hjelsberg was involved in all three of them.

Anyways, as methods are non-virtual by default the compiler can statically
link a method at compile-time. This is done by the JITer. An example:

MySuperClass o = new MySubClass();
o.DoStuff(); // method is non-virtual and the method in MySuperClass will be
called without hesitation. This is fast.
o.DoMyOverridde nStuff() //method is virtual and runtime have to check and
realize that o is typeof MySubClass and call the correct method. This hurts.

Now for some funny facts. The jitter in Java actually do support non-virtual
methods. The java runtime marks all leaf-classes (classes that have no
subclasses) and call on methods non-virtual. This is why the java runtime is
so much faster these days compared to older runtimes. The difference is that
..NET sport non-virttuality in the language while Java does not.

Now for your question about why you have to reintroduce non-virtual methods.
This is just to protect you from the flaws in Java. Virtual methods can be
overriden or reintroduced. Non-virtual methods can only be reintroduced.
Delphi actually uses the keyword 'reintroduce' while C# reuses the keyword
'new'. If you fail to do so; in Delphi you get a warning, in C# you get an
compile error. I like the latter.

This is a great safety-net. Whenever a subclass sports the same method as a
method in a superclass you cannot compile until you accept that. You can
reintroduce the method and if it's virtual you can override it. But the
great thing is that you have to respond and make a choice.

Then what is the correct choice?

Well, I think that reintroducing a method is a bad thing. If a superclass
wanna DoStuff() and your subclass wanna DoStuff() and these thingies are
different things; you don't wanna reintroduce stuff but rename your method
into DoThings().

My conclusion: Never ever reintroduce methods, but love the compiler for
stopping you from unwanted polymorphism and the weird bugs that follows.

Happy Coding
- Michael S





Nov 17 '05 #9

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:em******** **********@TK2M SFTNGP09.phx.gb l...
haha, sorry, but it's kind of like saying "Yes, bigfoot exists, but I
have never seen him."

It's nothing personal, and I'm not putting down the post, it just made
me laugh when I read it.


What? You haven't seen Bigfoot? I see him all the time, and I'm beginning to
think he's stalking me. My doctor keeps adding pills to my prescription but
the hairy giant refuse to go away! Oh, wait.... My Bad.... I was thinking
about Visual Basic. =)

Anyways, reintroduction of methods is not important in .NET. As references
to classes are not polymorphic. In Delphi we have type of object, which is a
typed reference to a class which gives a (weird) boost to polymorphism.
Delphites overrides .Free and reintroduces .Create all the time.

But as classes are very much static in .NET, and won't polymorph on
class-level; reintroduction is of lesser use. See my post on performance and
versioning. However, the 'new' keyword on methods is of little use in .NET.
It must be there to make the system complete, but you're not supposed to use
it.

Don't use it!

Happy Asylum
- Michael S




Nov 17 '05 #10

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

Similar topics

6
538
by: harrylmh | last post by:
Hi, I'm learning C# and I just don't quite understand the need for polymorphism. why do we need to use it? how does a base class variable holding a derived class instance do any good? Also, what's the difference between method hiding and overriding when they're both still overriding the base method. Thanks
4
4156
by: Sharon Tal | last post by:
Hi all. I am trying to figure out the differences between overriding and hiding a method name. The only difference i can see, is that with name hiding i can change the method access level. Are there any other differences? Thanks, Sharon.
1
1604
by: Tariq | last post by:
Any thoughts on how a method on a base class can be prevented from being overriden or hidden? Basically the idea is that no matter what all derived classes should use the method in my base class. Got to prevent hiding via the new keyword as well.
4
2360
dmjpro
by: dmjpro | last post by:
Look at my code carefully..... class Super { protected int a; } class Derived extends Super { protected int a; //Here Data Overriding or Data Hiding
0
9698
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,...
1
10654
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
10297
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9426
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...
1
7833
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5683
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
5867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4493
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
2
4066
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.