Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new vs override

Status
Not open for further replies.

cat5ive

MIS
Dec 3, 2004
184
US
Hi,

Code:
public override void DrawWindow()
public new void DrawWindow()
Can someone please explain what's the different between new and override? I don't see the different because both of them use its own version of DrawWindow and not the one from base class, isn't it?

Thanks in advance.
 
The new modifier is used to eliminate the warning caused by hiding an inherited name e.g the same signature as one in the base class.
class Base
{
public void F(int n) {}
}
class Derived: Base
{
//public void F(int n ) {} // Warning, hiding an inherited name
new public void F(int n) {} // Solved by using new
}
Hiding an inherited name is specifically not an error, since that would preclude separate evolution of base classes.
For example, the above situation might have come about because a later version of Base introduced an F method that wasn't present in an earlier version of the class.
Had the above situation been an error, then any change made to a base class in a separately versioned class library could potentially cause derived classes to become invalid.

An override method provides a new implementation of a member inherited from a base class.
The method overridden by an override declaration is known as the overridden base method.
The overridden base method must have the same signature as the override method.
You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.
obislavu
 
But in both methods (defined with new and override) from the derieved class, they only do things that are specified inside its own method. Neither methods will inherite any thing from the base class, right?

If I'm wrong, can you please provide a little sample that show what they inherite.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top