473,738 Members | 2,645 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

override verses hide

I do not understand what is meant when someone states that a given method is
"hidden" verses overriden.

Would someone please provide a short example of both cases and why you might
want to "hide" a parent method's implementation verses simply overriding it
(ie take advantage of polymorphism)?

Thanks,
TJ
Nov 16 '05 #1
3 5511
Hi Tom,

Suppose you have a method in a component which doensn't use "virtual" or
overridable keywords. It's not possible for you to override the method in
this case, you could only hide it.

Also, suppose you want to have a method in your derived class, which should
have the same name as the base class, but with different access specifiers,
the only possible way is to hide.

Using "new" keyword in method definitions helps you in these situations.

HTH,
- Rakesh Rajan

"Tom Jones" <to********@hot mail.com> wrote in message
news:uo******** ******@TK2MSFTN GP12.phx.gbl...
I do not understand what is meant when someone states that a given method is "hidden" verses overriden.

Would someone please provide a short example of both cases and why you might want to "hide" a parent method's implementation verses simply overriding it (ie take advantage of polymorphism)?

Thanks,
TJ

Nov 16 '05 #2
OK, the key thing to realise is that when a method is invoked on an object we have two concepts:
1) the object being invoked
2) the reference through which it is being invoked
in C# 1) and 2) do not have to be onf the same type, they must be related, but do not have to be the same. Consider the following code:

class Foo
{
public void Quux()
{
Console.WriteLi ne("Foo:Quux") ;
}
}

class Bar : Foo
{
public void Quux()
{
Console.WriteLi ne("Bar:Quux") ;
}
}

I can now write
Foo f = new Bar();

as Foo and Bar are type compatible (related).

Now the issue is what happens when we call
f.Quux();
in this setup it will print out Foo:Quux
if we write
Bar b = new Bar();
b.Quux();
it prints out Bar:Quux

This is known as static binding and the version of the function that gets called is determined by the type of the *reference* and is determined at compile time. And in this case Bar.Quux is said to be *hiding* Foo.Quux for as far as the compiler is concerned the two Quux methods are unrelated but it is impossible to get to Foo.Quux from a Bar reference - hence *hidden*.

Lets change the code a little

class Foo
{
public virtual void Quux()
{
Console.WriteLi ne("Foo:Quux") ;
}
}

class Bar : Foo
{
public override void Quux()
{
Console.WriteLi ne("Bar:Quux") ;
}
}

Now in this case
Foo f = new Bar();
f.Quux();
outputs Bar:Quux as the, in essence, Bar.Quux has *replaced* Foo.Quux in the virtual function table for Bar objects.This is what is mean't by *overridden*. This is known as dynamic binding (not to be confused with late binding) in that the version of the function f.Quux() will call cannot be determined until runtime when the type of object f references is known.

Two last points: the first code example will compile with a warning (saying that Bar.Quux hides Foo.Quux) and to get rid of the warning V=Bar's code needs to change to this

class Bar : Foo
{
public new void Quux()
{
Console.WriteLi ne("Bar:Quux") ;
}
}

This shows that the Bar author is aware of the issue and Bar.Quux is nothing to do with Foo.Quux they just happen to have the same name (this can happen if Foo and Bar are authored and evlove independently).

The last thing is what happens with this situation:

class Foo
{
public virtual void Quux()
{
Console.WriteLi ne("Foo:Quux") ;
}
}

class Bar : Foo
{
public void Quux() // no override keyword
{
Console.WriteLi ne("Bar:Quux") ;
}
}

In this case
Foo f = new Bar();
f.Quux();
outputs Foo:Quux

This is because the derived class must *explicitly* agree to dynamic binding for it to take place. this prevents a derived mthod being called accidently when a new version of the base class ships which now has a virtual method of the same name and signature as an existing one in the derived class.
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<uo************ **@TK2MSFTNGP12 .phx.gbl>

I do not understand what is meant when someone states that a given method is
"hidden" verses overriden.

Would someone please provide a short example of both cases and why you might
want to "hide" a parent method's implementation verses simply overriding it
(ie take advantage of polymorphism)?

Thanks,
TJ

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 22/09/2004

[microsoft.publi c.dotnet.langua ges.csharp]
Nov 16 '05 #3
Thank you *very* much for the excellent explanation Richard!!

-TJ

"Richard Blewett [DevelopMentor]" <ri******@devel op.com> wrote in message
news:OI******** ******@TK2MSFTN GP11.phx.gbl...
OK, the key thing to realise is that when a method is invoked on an object
we have two concepts:
1) the object being invoked
2) the reference through which it is being invoked
in C# 1) and 2) do not have to be onf the same type, they must be related,
but do not have to be the same. Consider the following code:

class Foo
{
public void Quux()
{
Console.WriteLi ne("Foo:Quux") ;
}
}

class Bar : Foo
{
public void Quux()
{
Console.WriteLi ne("Bar:Quux") ;
}
}

I can now write
Foo f = new Bar();

as Foo and Bar are type compatible (related).

Now the issue is what happens when we call
f.Quux();
in this setup it will print out Foo:Quux
if we write
Bar b = new Bar();
b.Quux();
it prints out Bar:Quux

This is known as static binding and the version of the function that gets
called is determined by the type of the *reference* and is determined at
compile time. And in this case Bar.Quux is said to be *hiding* Foo.Quux
for as far as the compiler is concerned the two Quux methods are unrelated
but it is impossible to get to Foo.Quux from a Bar reference - hence
*hidden*.

Lets change the code a little

class Foo
{
public virtual void Quux()
{
Console.WriteLi ne("Foo:Quux") ;
}
}

class Bar : Foo
{
public override void Quux()
{
Console.WriteLi ne("Bar:Quux") ;
}
}

Now in this case
Foo f = new Bar();
f.Quux();
outputs Bar:Quux as the, in essence, Bar.Quux has *replaced* Foo.Quux in
the virtual function table for Bar objects.This is what is mean't by
*overridden*. This is known as dynamic binding (not to be confused with
late binding) in that the version of the function f.Quux() will call
cannot be determined until runtime when the type of object f references is
known.

Two last points: the first code example will compile with a warning
(saying that Bar.Quux hides Foo.Quux) and to get rid of the warning
V=Bar's code needs to change to this

class Bar : Foo
{
public new void Quux()
{
Console.WriteLi ne("Bar:Quux") ;
}
}

This shows that the Bar author is aware of the issue and Bar.Quux is
nothing to do with Foo.Quux they just happen to have the same name (this
can happen if Foo and Bar are authored and evlove independently).

The last thing is what happens with this situation:

class Foo
{
public virtual void Quux()
{
Console.WriteLi ne("Foo:Quux") ;
}
}

class Bar : Foo
{
public void Quux() // no override keyword
{
Console.WriteLi ne("Bar:Quux") ;
}
}

In this case
Foo f = new Bar();
f.Quux();
outputs Foo:Quux

This is because the derived class must *explicitly* agree to dynamic
binding for it to take place. this prevents a derived mthod being called
accidently when a new version of the base class ships which now has a
virtual method of the same name and signature as an existing one in the
derived class.
Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog
nntp://news.microsoft. com/microsoft.publi c.dotnet.langua ges.csharp/<uo************ **@TK2MSFTNGP12 .phx.gbl>

I do not understand what is meant when someone states that a given method
is
"hidden" verses overriden.

Would someone please provide a short example of both cases and why you
might
want to "hide" a parent method's implementation verses simply overriding
it
(ie take advantage of polymorphism)?

Thanks,
TJ

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.768 / Virus Database: 515 - Release Date: 22/09/2004

[microsoft.publi c.dotnet.langua ges.csharp]

Nov 16 '05 #4

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

Similar topics

13
2241
by: Robert Lario | last post by:
C# verses VB.Net Which Way to go. I am sure this issues has come up before. Please direct me to any good articles that cover this issue. Ideally some neutral assessment. Thanks
5
6850
by: Darius | last post by:
I'm writing here in hopes that someone can explain the difference between the new and virtual/override keywords in C#. Specifically, what is the difference between this: public class Window { public void Draw() { Console.WriteLine("The WINDOW Draw method is running!");
9
2069
by: Ken Varn | last post by:
Is there anyway to override a public virtual method or property so that it is private in my derived class? I tried using new on the property and making it private, but no luck. -- ----------------------------------- Ken Varn Senior Software Engineer Diebold Inc.
5
2614
by: Mark Broadbent | last post by:
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...
11
1795
by: songie D | last post by:
Does c# support overriding by name and not signature For instance I am building a custom collection class, that ca only take a certain type of object For this, I have added an override for add from the class view and changed it fro public int Add(object value t public int Add(MyItemClass value This seems to work, but it generates a warning 'the new specifier is needed
18
4845
by: bhavin | last post by:
Hi, Can someone point me to some good best practices kind of documentation on use of events compared to method overriding. Ex. In Windows Forms when should i have an event handler for Paint, and when should i override OnPaint? I have to implement added functionality in a child class, and am in two minds .. to have the parent fire an event, or make the method virtual and override in the child. Am hoping some reading with description of...
5
4534
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 I've to do by using override. It's also written, that's possible to "hide" the base class method by using the new key word. Because I've already written some C# code and I didn't know anything
3
3374
by: ak | last post by:
Q1: I am looking to find/build a table of PHP version verses timezonedb version shipped with it. For example, I think that 5.2.0 updated the timezonedb to the 2006.14 version. Is there a table of PHP version verses timezonedb version. Q2: I am also looking to find a table of timezonedb version verses Olsen timezone database used to construct it. For example, I think that the 2006.16 version corresponds to the Olsen tzdata2006p.
2
3529
by: dolphin | last post by:
Hi everyone . I am confused about the different between override overload and hide. May be I have a wrong opinion, I always think that hide is very similar with override. Both of them call the function of derived class,do not care the function in the base class. I know I make something wrong here, but i dont kow what the wrong it is? Could some one point out for me? sorry for my poor english.
0
8787
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,...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9259
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
8208
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
6750
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
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4824
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.