Clarification on method modifiers 'new', 'virtual', 'override' 
Author Message
 Clarification on method modifiers 'new', 'virtual', 'override'

As you can guess, I'm a Java programmer that wants to hedge his bets.
So I learn C# and .NET.  Anyways, the .NET rules for handling virtual
methods seem a bit contrived to me.  What I'd like to do is to
describe how I've managed to understand how 'new', 'virtual' and
'override' interrelate, and let the newsgroup tell me where I've got
it wrong.  BTW, I can't seem to find any other discussion on these
topics, so please pardon me for repeating some other thread.

First, in Java all methods are implicitly virtual.  If I have the
syntax:

// MyClass extends ParentClass(), both have toString().
Object o = (Object) new MyClass();
o.toString();

'o.toString()' will run MyClass.toString().

In C#, for some reason, the language designers wanted to give the
developer a chance to turn off virtual methods.  Perhaps the IL
run-time doesn't have virtual methods in its least-common-denominator
set.  So, I have to explicitly say 'virtual' and 'override' to get to
the same place as (implicit) in Java, and a slight speed gain if the
keywords (and virtual methods) aren't used.

Now the language designer sees that, whoops!  they want to let the
package (namespace) designer override methods after all, even if
non-virtual, so they invent the 'new' keyword for methods.  It almost
works like a virtual method.

Then there is the business of how structs can't have abstract
interface implementation.  Things seem rather arbitrary here.  I can
take rules, but I'd rather 'grok' the language spec.

Comments, anyone?

Jerome.



Sat, 18 Dec 2004 12:26:53 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'

<snip>

Quote:
> So, I have to explicitly say 'virtual' and 'override' to get to
> the same place as (implicit) in Java, and a slight speed gain if the
> keywords (and virtual methods) aren't used.

Well, any recent JVM will inline virtual methods until a subclass is
loaded, so there isn't any real speed advantage to explicitly avoiding
virtual methods.

Jim S.



Sat, 18 Dec 2004 20:56:16 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'

Quote:
> As you can guess, I'm a Java programmer that wants to hedge his bets.
> So I learn C# and .NET.  Anyways, the .NET rules for handling virtual
> methods seem a bit contrived to me.  What I'd like to do is to
> describe how I've managed to understand how 'new', 'virtual' and
> 'override' interrelate, and let the newsgroup tell me where I've got
> it wrong.  BTW, I can't seem to find any other discussion on these
> topics, so please pardon me for repeating some other thread.

"new" is not a method modifer. "new" is a keyword, and it's used
to create a new object, call its constructor, then return the
reference to the object.

Quote:
> First, in Java all methods are implicitly virtual.  If I have the
> syntax:

> // MyClass extends ParentClass(), both have toString().
> Object o = (Object) new MyClass();
> o.toString();

> 'o.toString()' will run MyClass.toString().

> In C#, for some reason, the language designers wanted to give the
> developer a chance to turn off virtual methods.  Perhaps the IL
> run-time doesn't have virtual methods in its least-common-denominator
> set.  So, I have to explicitly say 'virtual' and 'override' to get to
> the same place as (implicit) in Java, and a slight speed gain if the
> keywords (and virtual methods) aren't used.

In C++ you have a choice of virtual or non-virtual methods and
there are reasons for both. In C#, you also have that choice.
This isn't Java anymore, well, not all the way, at least. =)

Quote:

> Now the language designer sees that, whoops!  they want to let the
> package (namespace) designer override methods after all, even if
> non-virtual, so they invent the 'new' keyword for methods.  It almost
> works like a virtual method.

You are incorrect. You cannot "new" a method. "new" behaves exactly
as it did in Java.

You can "new" a delegate which is a special type of class or
function wrapper that allows you to make function pointers that
are strongly typed. Perhaps you're getting confused with delegates.

Quote:

> Then there is the business of how structs can't have abstract
> interface implementation.  Things seem rather arbitrary here.  I can
> take rules, but I'd rather 'grok' the language spec.

I don't know much about structs. I tend to stay away from them
as they just create confusion and don't really offer you much.

-c



Sun, 19 Dec 2004 03:45:37 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'

<snip>

Quote:

> "new" is not a method modifer.

The C# specification states otherwise.  Specifically, Section 10.5.

Quote:
> "new" is a keyword, and it's used
> to create a new object, call its constructor, then return the
> reference to the object.

One of its uses.  Another is as a modifier to a method:

method-modifier:
new
public
protected
internal
private
static
virtual
sealed
override
abstract
extern

See the specification for its meaning and usage.

<snip>

Quote:

> You are incorrect. You cannot "new" a method. "new" behaves exactly
> as it did in Java.

Please read the specification before making seemingly authoritative
comments in such matters.

Jim S.



Sun, 19 Dec 2004 04:35:35 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'
Yes, you *can* use the new keyword on a method.
new on a method is used to tell the compiler that a method in a base class
is to be considered a "new" version
of a virtual (but identical signature) method in the base class and should
not participate in the polymorphic chain.
In the example below, if override is used in Derived's version of SayHi,
then it will print out "Derived says hi".
If you don't use override (and use nothing) it will compile, but there will
be a warning.
When you run it will say "Base says hi". If you add the new keyword (instead
of override), it will be exactly
like leaving override or new off (that is, "Base says hi" will still print),
but you will not get the compile warning.

Example:
class Class1

{

[STAThread]

static void Main(string[] args)

{

Base obj = new Derived();

obj.SayHi();

System.Console.Read();

Quote:
}
}

internal class Base

{

public virtual void SayHi()

{

System.Console.WriteLine("Base says hi.");

Quote:
}
}

internal class Derived : Base

{

public override void SayHi()

{

System.Console.WriteLine("Derived says hi.");

Quote:
}
}



Sun, 19 Dec 2004 04:44:20 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'
That should be that a method in a <derived> class is a "new" version of
virtual method in the base class.
Sorry for the typo.

Steaven



Sun, 19 Dec 2004 04:47:49 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'
You can in fact "new" a method. It indicates that the method is not an
override of the same-named method in the base class, but is in fact a
completely new implementation. You'd use it if you created your own class
derived from some other class supplied by a third-party, and then find out
that the third-party has released a new version of their class that happens
to have a method name that is the same as one you put in your derived class.
The next time you try to compile your derived class, the compiler will
complain about it and you will need to specify if your class is an
independant method, or if it overrides the method in the base class.

Using new to modify a method isn't something you would design into a class.
It's there for when the base class is out of your control and a later
revision of it collides with naming in your derived class.

Colin


Quote:


> > As you can guess, I'm a Java programmer that wants to hedge his bets.
> > So I learn C# and .NET.  Anyways, the .NET rules for handling virtual
> > methods seem a bit contrived to me.  What I'd like to do is to
> > describe how I've managed to understand how 'new', 'virtual' and
> > 'override' interrelate, and let the newsgroup tell me where I've got
> > it wrong.  BTW, I can't seem to find any other discussion on these
> > topics, so please pardon me for repeating some other thread.

> "new" is not a method modifer. "new" is a keyword, and it's used
> to create a new object, call its constructor, then return the
> reference to the object.

> > First, in Java all methods are implicitly virtual.  If I have the
> > syntax:

> > // MyClass extends ParentClass(), both have toString().
> > Object o = (Object) new MyClass();
> > o.toString();

> > 'o.toString()' will run MyClass.toString().

> > In C#, for some reason, the language designers wanted to give the
> > developer a chance to turn off virtual methods.  Perhaps the IL
> > run-time doesn't have virtual methods in its least-common-denominator
> > set.  So, I have to explicitly say 'virtual' and 'override' to get to
> > the same place as (implicit) in Java, and a slight speed gain if the
> > keywords (and virtual methods) aren't used.

> In C++ you have a choice of virtual or non-virtual methods and
> there are reasons for both. In C#, you also have that choice.
> This isn't Java anymore, well, not all the way, at least. =)

> > Now the language designer sees that, whoops!  they want to let the
> > package (namespace) designer override methods after all, even if
> > non-virtual, so they invent the 'new' keyword for methods.  It almost
> > works like a virtual method.

> You are incorrect. You cannot "new" a method. "new" behaves exactly
> as it did in Java.

> You can "new" a delegate which is a special type of class or
> function wrapper that allows you to make function pointers that
> are strongly typed. Perhaps you're getting confused with delegates.

> > Then there is the business of how structs can't have abstract
> > interface implementation.  Things seem rather arbitrary here.  I can
> > take rules, but I'd rather 'grok' the language spec.

> I don't know much about structs. I tend to stay away from them
> as they just create confusion and don't really offer you much.

> -c



Sun, 19 Dec 2004 04:55:24 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'


Quote:

> <snip>

> > "new" is not a method modifer.

> The C# specification states otherwise.  Specifically, Section 10.5.

> > "new" is a keyword, and it's used
> > to create a new object, call its constructor, then return the
> > reference to the object.

> One of its uses.  Another is as a modifier to a method:

> method-modifier:
> new
> public
> protected
> internal
> private
> static
> virtual
> sealed
> override
> abstract
> extern

> See the specification for its meaning and usage.

> <snip>

> > You are incorrect. You cannot "new" a method. "new" behaves exactly
> > as it did in Java.

> Please read the specification before making seemingly authoritative
> comments in such matters.

Ah yes, you can, however, I thought he was implying you could do:

MyMethod method = new MyObject.MyMethod

Which you cannot.

I misread his post.

-c



Sun, 19 Dec 2004 08:16:14 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'

Quote:

> First, in Java all methods are implicitly virtual.  If I have the
> syntax:

Just a small nit.  All *instance* methods in Java are virtual, unless
they're final.  Static methods, and instance methods declared to be
final, aren't virtual.  Still, it's a subtlety that most developers
won't notice.

That said, your point is valid -- the developer doesn't have to
declare a method virtual -- if it can be, it is.  This seems to me to
be a step backwards in C#.  I'm sure there are uses for the
distinction between new, virtual, and override, but I haven't
encountered them yet in my own work.

Wayne
____________________________________

Wayne Citrin
JNBridge, LLC

www.jnbridge.com
Spanning the Worlds of Java and .NET
____________________________________



Mon, 20 Dec 2004 02:01:11 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'
A good question for "Ask a Language Designer" on the C# community site.
Maybe there'd be an answer in a year or so...

Colin



Quote:
> > First, in Java all methods are implicitly virtual.  If I have the
> > syntax:

> Just a small nit.  All *instance* methods in Java are virtual, unless
> they're final.  Static methods, and instance methods declared to be
> final, aren't virtual.  Still, it's a subtlety that most developers
> won't notice.

> That said, your point is valid -- the developer doesn't have to
> declare a method virtual -- if it can be, it is.  This seems to me to
> be a step backwards in C#.  I'm sure there are uses for the
> distinction between new, virtual, and override, but I haven't
> encountered them yet in my own work.

> Wayne
> ____________________________________

> Wayne Citrin
> JNBridge, LLC

> www.jnbridge.com
> Spanning the Worlds of Java and .NET
> ____________________________________



Mon, 20 Dec 2004 02:47:11 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'



Quote:
> > First, in Java all methods are implicitly virtual.  If I have the
> > syntax:

> Just a small nit.  All *instance* methods in Java are virtual, unless
> they're final.  Static methods, and instance methods declared to be
> final, aren't virtual.  Still, it's a subtlety that most developers
> won't notice.

Private methods are also non-virtual in Java.  Everyone knows this (Have you
ever worried about reusing the name of a private method in a subclass thus
causing the superclass to call the wrong version?  Of course not.) but
people often forget to mention it.


Tue, 21 Dec 2004 23:54:12 GMT  
 Clarification on method modifiers 'new', 'virtual', 'override'

Quote:





> > > First, in Java all methods are implicitly virtual.  If I have the
> > > syntax:

> > Just a small nit.  All *instance* methods in Java are virtual, unless
> > they're final.  Static methods, and instance methods declared to be
> > final, aren't virtual.  Still, it's a subtlety that most developers
> > won't notice.

> Private methods are also non-virtual in Java.  Everyone knows this (Have you
> ever worried about reusing the name of a private method in a subclass thus
> causing the superclass to call the wrong version?  Of course not.) but
> people often forget to mention it.

Quite right.  I forgot about that...


Wed, 22 Dec 2004 07:17:26 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. 'h' modifier in printf

2. '0', '1', '2', '3' ...???

3. kyduke(kyduke@yahoo.com)'s New Logic - New sorting, New paging method

4. MS-DOS NRO (an 'NROFF'- 'RNO'-like Source in C)

5. Create a 'virtual file'

6. Create a 'virtual file'

7. significance of 'virtual' keyword

8. virtual key code of '+'

9. What's the difference between 'new' and 'overrride'?

10. Problems with BuildProject method: 'Unspecified error'

11. Implementing a 'clone' method/property

12. Question about mfc's CFtpFileFind's GetFileURL method

 

 
Powered by phpBB® Forum Software