473,769 Members | 8,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another inheritance question

I'm curious as to the need for the new keyword in an inherited method
redefinition, so -

Suppose I have a class hierachy that looks like

public class Base
{
public virtual void myMethod()
{...
}
}

public class Derived : Base
{
public void myMethod()
{...
}
}
From what I understand, without the override keyword the compiler

produces a warning and the Base class version is hidden, meaning that
if I tried

Base myObject = new Derived;
myObject.myMeth od();

Would this produce a compile time (or runtime) error ? Or maybe this
just means that myObject cannot be used polymorphically as in -

Base myObject1 = new Base;
Base myObject2 = new Derived;
myObject1 = myObject2;

Now if instead of override I use new, all this apparently does is get
rid of the compiler warning. I'm confused.

Thanks in advance,

May 29 '06 #1
4 1208
Ah it is changing the behavior .. a simple test will show you the following
...
public class Base

{

public virtual void myMethod()

{

Console.WriteLi ne("Base");

}

}

public class Derived : Base

{

public new void myMethod()

{

Console.WriteLi ne("Derived");

}

}

[STAThread]

static void Main(string[] args)

{

Derived Derived = new Derived();

Derived.myMetho d();

Base Base = (Derived) Derived;

Base.myMethod() ;

}

}

As you can see .. when used as base, it uses the base method (not the
derived method as the derived method is redefining the method for its own
uses, not overriding the behavior of the base's method)

Cheers,

Greg Young
MVP - C#
<ti************ ****@yahoo.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
I'm curious as to the need for the new keyword in an inherited method
redefinition, so -

Suppose I have a class hierachy that looks like

public class Base
{
public virtual void myMethod()
{...
}
}

public class Derived : Base
{
public void myMethod()
{...
}
}
From what I understand, without the override keyword the compiler

produces a warning and the Base class version is hidden, meaning that
if I tried

Base myObject = new Derived;
myObject.myMeth od();

Would this produce a compile time (or runtime) error ? Or maybe this
just means that myObject cannot be used polymorphically as in -

Base myObject1 = new Base;
Base myObject2 = new Derived;
myObject1 = myObject2;

Now if instead of override I use new, all this apparently does is get
rid of the compiler warning. I'm confused.

Thanks in advance,

May 29 '06 #2
Wrote:
I'm curious as to the need for the new keyword in an inherited method
redefinition, so -

Suppose I have a class hierachy that looks like

public class Base
{
public virtual void myMethod()
{...
}
}

public class Derived : Base
{
public void myMethod()
{...
}
}
From what I understand, without the override keyword the compiler produces a warning and the Base class version is hidden, meaning that
if I tried

Base myObject = new Derived;
myObject.myMeth od();


Base::myMethod will be called, since method was not overriden
Would this produce a compile time (or runtime) error ? Or maybe this
just means that myObject cannot be used polymorphically as in -
Only warning about hidden member of the base class
Base myObject1 = new Base;
Base myObject2 = new Derived;
myObject1 = myObject2;

Now if instead of override I use new, all this apparently does is get
rid of the compiler warning. I'm confused.


The warning says: "To make the current member override that implementation,
add the override keyword. Otherwise add the new keyword."
That is if you do not override implementation and to avoid confusion about
inherited methods there is new keyword, that disables that warning.

From the docs:
"Hiding an inherited member means that the derived version of the member
replaces the base-class version. Hiding members without the use of the new
modifier is allowed, but generates a warning. Using new to explicitly hide a
member suppresses this warning, and documents the fact that the derived
version is intended as a replacement."

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
May 29 '06 #3
<ti************ ****@yahoo.com> wrote...
I'm curious as to the need for the new keyword in
an inherited method redefinition,
Well, it's actually not *needed*, but gets rid of the compiler warnings...
;-)

When you write the "new" keyword, you have told the compiler that you're
*conciously* hiding the inherited method.

So, it won't make any difference when running the application whether you
have used "new" or not.

What really differs, is whether you're using "override" or not!
so -

Suppose I have a class hierachy that looks like

public class Base
{
public virtual void myMethod()
{...
}
}

public class Derived : Base
{
public void myMethod()
{...
}
}
From what I understand, without the override keyword the
compiler produces a warning and the Base class version is
hidden, meaning that if I tried
Base myObject = new Derived;
myObject.myMeth od();

Would this produce a compile time (or runtime) error?


Neither.

That will use the method defined in Base.

But..

Derived myObject = new Derived();
myObject.myMeth od();

....will use the method defined in Derived.
Or maybe this just means that myObject cannot be used
polymorphically as in -

Base myObject1 = new Base;
Base myObject2 = new Derived;
myObject1 = myObject2;
Correct. Non-overridden methods will use the implementation based on the
reference type, in this case the type of the variable 'myObject2': Base.
Now if instead of override I use new, all this apparently
does is get rid of the compiler warning. I'm confused.


There's a big difference between overriding and hiding.

To make it simple we could say that when you override a method, the method
gets connected to the type of the instance, while when you're "hiding" a
method, the method is connected to the type of the *reference* to the
instance.

public class Base
{
public virtual void myMethod() {}
}

public class Hider : Base
{
// "new" keyword eliminates compiler warnings
// If you can live with them, "new" isn't necessary...

public new void myMethod() { }
}

public class Overrider : Base
{
public override void myMethod() { }
}

------------------------------
Here the reference type matters:

Base bh = new Hider();
Hider hh = new Hider();

bh.myMethod(); // Runs myMethod from Base
hh.myMethod(); // Runs myMethod from Hider

------------------------------
Here the type of the instance matters.

Base bo = new Overrider();
Overrider oo = new Overrider();

bo.myMethod(); // Runs myMethod from Overrider
oo.myMethod(); // Runs myMethod from Overrider

------------------------------
/// Bjorn A
May 29 '06 #4
Thanks Bjorn.

Ted

May 30 '06 #5

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

Similar topics

1
3751
by: KK | last post by:
Windows Forms Inheritance, Incomplete? I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
2
2203
by: KK | last post by:
** Posting it here cause after couple of days no body responded.** I was playing around with Windows Forms and found out this Forms Inheritance feature. The moment I saw that, I felt this can be used effectively if the application contains couople of forms which have a consistant look and also shares SOME similar functionality between the forms.
51
4074
by: nospam | last post by:
THIS IS the DOTNETJUNKIES MESSAGE ------------------------- We're Sorry As many of you know we have recently launched SqlJunkies.com. We have overhauled our runtime and will be using it on DotNetJunkies.com also. -------------------------------------------------------------------------- YEP, DOTNET JUNKIES REDESIGN....
188
7252
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection - non-deterministic destructors - Objects can't exist on the stack - Type / Reference Types
14
1926
by: Bruno van Dooren | last post by:
Hi all, i am having a problems with inheritance. consider the following: class A { public: A(int i){;} };
6
2100
by: VR | last post by:
Hi, I read about Master Pages in ASP.Net 2.0 and after implementing some WinForms Visual Inheritance I tryed it with WebForms (let's say .aspx pages, my MasterPage does not have a form tag itself so, cannot be called a WebForm itself, the child pages will implement forms). I created a Master.aspx page and removed all HTML from it, added some code to the .aspx.vb file to add controls to my page. Then I created a Child.aspx and changed the...
14
2392
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class implement a common interface? thanks, craig buchanan
5
2473
by: Noah Roberts | last post by:
Is there anything that says that if you virtually inherit from one class you have to virtually inherit from anything you inherit from?
3
1841
by: RSH | last post by:
I have a simple question regarding inheritance in a web form. I have a DropDownList in an aspx form. It is called DropDownList1 I have a class that will be overriding the render event so I have a snippet of this class: Public Class CustomDDL Inherits DropDownList
8
328
by: RSH | last post by:
Hi, I am working on some general OOP constructs and I was wondering if I could get some guidance. I have an instance where I have a Base Abstract Class, and 4 Derived classes. I now need to make a list class that will store the objects. My question is how do I go about creating the list class...I am assuming it should be a standalone class that uses an arraylist to store the objects. If I go that route how do I instantiate the...
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10049
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
9998
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
8876
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...
0
5310
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.