more inheritance

T

Tony Johansson

Hello!

Is this text really correct ?
Sometimes you may want to create your own implementation of a method that
exists in a base class. The DerivedClass class does this by declaring its
own print() method. The DerivedClass print() method hides the BaseClass
print() method. The effect is the DerivedClass print() method will not be
called, unless we do something special to make sure it is called.
Notice the new modifier on the DerivedClass class print() method. This
enables this method to hide the BaseClass class's print() method and
explicitly states your intention that you don't want polymorphism to occur.
Without the new modifier, the compiler will produce a warning to draw your
attention to this. See later for a discussion of polymorphism.


I mean I don't quite understand when the text say The DerivedClass print()
method hides the BaseClass print() method ?
Can somebody explain that .

//Tony
 
T

Tony Johansson

Peter Duniho said:
Tony said:
[...]
I mean I don't quite understand when the text say The DerivedClass
print()
method hides the BaseClass print() method ?
Can somebody explain that .

Without the code, your question makes very little sense. But, let's
assume we have something like this:

class BaseClass
{
public void print()
{
Console.WriteLine("BaseClass.print()");
}
}

class DerivedClass : BaseClass
{
public new void print()
{
Console.WriteLine("DerivedClass.print()");
}
}

class Program
{
public static void Main()
{
DerivedClass dc = new DerivedClass();

dc.print();
}
}

Then, what does the last line of code, "dc.print()" do? In the Main()
method, is it even possible for the code to "see" the print()
implementation in BaseClass? Given that the answer to that question is
"no", can you see why we would use the term "hides" for the effect that
creating a new non-virtual method with the exact same signature as a
base-class method has?

If that doesn't answer your question, then you need to be clear about what
part of member hiding it is that is confusing you.

Pete

The only thing that this new keywork do as I understand it is that you can't
use polymorfism

//Tony
 
A

Arne Vajhøj

Peter Duniho said:
Tony said:
[...]
I mean I don't quite understand when the text say The DerivedClass
print()
method hides the BaseClass print() method ?
Can somebody explain that .

Without the code, your question makes very little sense. But, let's
assume we have something like this:

class BaseClass
{
public void print()
{
Console.WriteLine("BaseClass.print()");
}
}

class DerivedClass : BaseClass
{
public new void print()
{
Console.WriteLine("DerivedClass.print()");
}
}

class Program
{
public static void Main()
{
DerivedClass dc = new DerivedClass();

dc.print();
}
}

Then, what does the last line of code, "dc.print()" do? In the Main()
method, is it even possible for the code to "see" the print()
implementation in BaseClass? Given that the answer to that question is
"no", can you see why we would use the term "hides" for the effect that
creating a new non-virtual method with the exact same signature as a
base-class method has?

If that doesn't answer your question, then you need to be clear about what
part of member hiding it is that is confusing you.

The only thing that this new keywork do as I understand it is that you can't
use polymorfism

By using the keyword you tell the compiler that you don't
want polymorphism.

It is not frequently used keyword.

I tried searching in 1300 small C# source files:
- 189 occurrences of override
- 17 occurrences of virtual
- 2 occurrences of new

I will not claim that the exact distribution is representative, but
the relative order of the 3 keywords seems normal.

Arne
 
T

Tony Johansson

Peter Duniho said:
The "new" keyword is there so that the C# compiler can be sure that you
really meant to hide a base class method.

With polymorphism - that is, virtual methods - C# requires explicit use of
"virtual", "abstract", or "override" as appropriate. In those cases, the
programmer's intent is clear.

But without a "new" keyword, it would be possible to accidentally hide a
base class's member, either through forgetting to include the "override"
keyword, or through using an identical member name without realizing the
base class had the same method (or worse, because the base class added the
method later).

The "new" keyword allows the programmer to express their intent to the
compiler, that they really do mean to hide the method. Without the
keyword, the compiler emits a warning to alert the programmer to the fact
that they may have accidentally hidden a base class member.

Pete

Good explained Peter !

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top