468,733 Members | 1,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,733 developers. It's quick & easy.

should this be allowed?

look at the sampl

<pre

using System

class Bas
public virtual void Print(

Console.WriteLine( "Executing the base." )


class Derived : Bas
private new void Print(

Console.WriteLine( "Executing the derived." )
base.Print()
public void Print2(

Print()


class MainAp
static void Main(
Derived obj = new Derived()
obj.Print()

Console.WriteLine( "" )

obj.Print2()

</pre

now I basically have 2 versions of Print defined for Derived. one for external, one for internal. should the compiler actually allow this in the first place?
Nov 16 '05 #1
4 1093
Daniel Jin <an*******@discussions.microsoft.com> wrote:
look at the sample
<snip>
now I basically have 2 versions of Print defined for Derived. one for
external, one for internal. should the compiler actually allow this
in the first place?


Absolutely. The point of "new" is that it's defining a completely
different method. Whether or not it's a good idea to use this fact
arbitrarily is a different matter.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
The "new" keyword tells the compiler that you are defining a "new" method.
So, this makes perfect sense.
The compiler won't allow the restriction from public to private if you
replace "new" by "override".

Bruno.

"Daniel Jin" <an*******@discussions.microsoft.com> a écrit dans le message
de news:8D**********************************@microsof t.com...
look at the sample

<pre>

using System;

class Base
{

public virtual void Print()
{
Console.WriteLine( "Executing the base." );
}

}

class Derived : Base
{

private new void Print()
{
Console.WriteLine( "Executing the derived." );
base.Print();
}

public void Print2()
{
Print();
}

}

class MainApp
{

static void Main()
{

Derived obj = new Derived();
obj.Print();

Console.WriteLine( "" );

obj.Print2();

}

}

</pre>

now I basically have 2 versions of Print defined for Derived. one for

external, one for internal. should the compiler actually allow this in the
first place?
Nov 16 '05 #3


----- Jon Skeet [C# MVP] wrote: ----
Absolutely. The point of "new" is that it's defining a completely
different method. Whether or not it's a good idea to use this fact
arbitrarily is a different matter


Just seemed a little odd to me since Print from base class isn't hidden because the difference in visibility. Wouldn't it be better if the compiler at least generated a warning? say hypothetically, I wanted to make the new Print() public in Derived, but for some reason I forgot to mark it with public, so it defaulted to private. compiler doesn't complain. now I call Derived.Print(), but the base method executes. It'd be probably very easy to spot a mistake like this, but still, warning would be nice.
Nov 16 '05 #4
"new" methods are a rather dangerous feature IMO. So, you have to be careful
when you use it. Given its semantics, it is difficult for the compiler to
give you this kind of warning.

I try to restrict the use of new to cases where the new method performs
exactly the same thing as the method in the superclass but has a different
return type. The typical pattern is the following:

class BaseContainer { }
class BaseElement
{
private BaseContainer _container;
public BaseContainer Container { get { return _container; } }
}

class DerivedContainer : BaseContainer {}
class DerivedElement : BaseElement
{
public new DerivedContainer Container { get { return
(DerivedContainer)base.Container; } }
}

The idea is to get a correctly typed API in the derived API. (this is called
type covariance and languages like Eiffel support it).

But I don't allow any other use of new methods, because anything else
(redefining a method to give it different semantics than what it has in the
base class) is just heading for trouble. If the two methods do different
things, you get different results depending on whether you call them from a
BaseXxx or a DerivedXxx (the types that the compiler sees, not the runtime
types), and this can lead to very tricky bugs.

Bruno.

"Daniel Jin" <an*******@discussions.microsoft.com> a écrit dans le message
de news:7D**********************************@microsof t.com...


----- Jon Skeet [C# MVP] wrote: -----
> Absolutely. The point of "new" is that it's defining a completely
> different method. Whether or not it's a good idea to use this fact
> arbitrarily is a different matter.


Just seemed a little odd to me since Print from base class isn't

hidden because the difference in visibility. Wouldn't it be better if the
compiler at least generated a warning? say hypothetically, I wanted to make
the new Print() public in Derived, but for some reason I forgot to mark it
with public, so it defaulted to private. compiler doesn't complain. now I
call Derived.Print(), but the base method executes. It'd be probably very
easy to spot a mistake like this, but still, warning would be nice.
Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Lighter | last post: by
7 posts views Thread by Bernard Dhooghe | last post: by
82 posts views Thread by quiberon2 | last post: by
9 posts views Thread by Abandoned | last post: by
2 posts views Thread by puzzlecracker | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.