Using new vs virtual and override question

Z

z_learning_tester

Hello, yes another beginner question that I'm sure is obvious to many here
:)

My book is so bad. Really. It uses the exact same example of code for using
the new kw and for using virtual(in the base class) then override(in the
derived class), but fails to compare and contrast the two...

I've read this (short snippet)three times and by the looks of it they both
do exactly the same thing- allow you to derive from the base and save
writing some code in the derived class. It seems they both say "yes, use
this derived method, slightly modified, and don't use the original one in
the base class"

I know I'm missing something big here.
Any compare/contrast of using new vs using virtual and override much
appreciated.
If it's too long, maybe a reference to a good white paper or something(?)
Thanks!

Jeff
 
Z

z_learning_tester

OK, the MSDN example looked great until this line of code: "MyBase mB =
(MyBase) mD;"
Never seen anything like it. Is this a cast?
Does that mean mD is cast to a MyBase type?
How about if that line was missing? would the output be the following
instead?

MyBase-Meth1
MyDerived-Meth2
MyDerived-Meth3

This is what I was (almost) expecting, until that crazy unexplained line
above.
Thanks!

Jeff

--------------------------------------------
public class MyBase
{
public virtual string Meth1()
{
return "MyBase-Meth1";
}
public virtual string Meth2()
{
return "MyBase-Meth2";
}
public virtual string Meth3()
{
return "MyBase-Meth3";
}
}

class MyDerived : MyBase
{
// Overrides the virtual method Meth1 using the override keyword:
public override string Meth1()
{
return "MyDerived-Meth1";
}
// Explicitly hide the virtual method Meth2 using the new
// keyword:
public new string Meth2()
{
return "MyDerived-Meth2";
}
// Because no keyword is specified in the following declaration
// a warning will be issued to alert the programmer that
// the method hides the inherited member MyBase.Meth3():
public string Meth3()
{
return "MyDerived-Meth3";
}

public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}

Output:
MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3

Madhu said:
Hi,

The difference between the override keyword and new keyword is that the
former does method overriding and the later does method hiding.
 
Z

z_learning_tester

OK, so I went throught the MSDN examples today and they say:

Override is used in the derived class to say "use this one instead of the
one in the base class" I am overriding the one in the base class.
New also says "use this one instead of the one in the base class" I am
hiding the one in the base class.

I still fail to see the difference if you override the base class or if you
hide it, you still get the same results: the one on the derived class is
used instead.

The only difference I could find is the exception below, where new must be
used on MyBaseC instead of virtual and override b/c you cannot use virtual
with static.

So my question is- is there anything that new does that override doesn't or
vice-verse?
Aside from being usable with static? What's the difference between hiding
and overriding, or do they both just say "ignore the base, use the derived"?

This is by far the most difficult concept I've found in C#.
All examples look the same...
Again, any help *much* appreciated.
Thanks!

Jeff


public class MyBaseC
{
public static int x = 55;
public static int y = 22;
}

public class MyDerivedC : MyBaseC
{
new public static int x = 100; // Name hiding
public static void Main()
{
// Display the overlapping value of x:
Console.WriteLine(x);

// Access the hidden value of x:
Console.WriteLine(MyBaseC.x);

// Display the unhidden member y:
Console.WriteLine(y);
}
}
Output
100
55
 
J

Jon Skeet [C# MVP]

z_learning_tester said:
OK, so I went throught the MSDN examples today and they say:

Override is used in the derived class to say "use this one instead of the
one in the base class" I am overriding the one in the base class.
New also says "use this one instead of the one in the base class" I am
hiding the one in the base class.

I still fail to see the difference if you override the base class or if you
hide it, you still get the same results: the one on the derived class is
used instead.

The difference is what the *base* class will use if it calls the
method. The link I provided gave an example of the difference, saying
which would be called when.
 
Z

z_learning_tester

Jon Skeet said:
The difference is what the *base* class will use if it calls the
method. The link I provided gave an example of the difference, saying
which would be called when.

Yep, that seems the part I was really missing, that seems the key!
These derived classes are not to be used on their own.
I think the WHOLE point is to put all these methods back into the base class
type by a cast, or into a base class type array, etc, then use that instance
of the base class type.

Their choice of verbage however, though I'm sure is very standard,
is also very unintuitive in regards to hiding because all the lingo seems
reversed.
That caught me up for so long...

After all, the base method that is "hidden"(by the derived class- using
"new") is the one that shows up!
The one that is "hidden" by the derived class using no kw at all, also shows
up.
Dang that's confusing.

Now override is the only one that seems to make sense,
if it overrides the base then it well, overrides it, and is used instead of
the base(great! :)

// versioning.cs
// CS0114 expected
public class MyBase
{
public virtual string Meth1() //This one is "overridden" so it
doesnt show- OK :)
{
return "MyBase-Meth1";
}
public virtual string Meth2() //This one is "hidden", so it shows!
{
return "MyBase-Meth2";
}
public virtual string Meth3() //This one is "hidden", so it shows!
{
return "MyBase-Meth3";
}
}

class MyDerived : MyBase
{ public override string Meth1() //This one "overrides" the base-
cool.
{
return "MyDerived-Meth1";
}
public new string Meth2() //Usually in life, the 'newer' thing
is used... {
return "MyDerived-Meth2" }
public string Meth3() //This one "hides" the base, so on
output, it is itself- hidden. {
return "MyDerived-Meth3";
}

public static void Main()
{
MyDerived mD = new MyDerived();
MyBase mB = (MyBase) mD;

System.Console.WriteLine(mB.Meth1());
System.Console.WriteLine(mB.Meth2());
System.Console.WriteLine(mB.Meth3());
}
}Output
MyDerived-Meth1
MyBase-Meth2
MyBase-Meth3
 
Z

z_learning_tester

Sorry, I used one of the other folk's examples from MSDN in my last
response.
Your example is definitely one of the cleanest yet and I think actually
answers my questions('hiding' terminology aside ;-)

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

public class Derived : Base
{
public new void SomeOtherMethod()
{
}
}

....

Base b = new Derived();
Derived d = new Derived();
b.SomeOtherMethod(); //calls base.SomeOtherMethod()
d.SomeOtherMethod(); //calls derived.SomeOtherMethod()

Just one last question- did you need to use 'virtual' here?
Would it make a difference without it?
Thanks again,

Jeff
 
J

Jon Skeet [C# MVP]

z_learning_tester said:
Just one last question- did you need to use 'virtual' here?
Would it make a difference without it?

You can only override virtual methods - without the virtual modifier,
the override version wouldn't compile at all.
 
S

Sunny

Sample:

public class MyBase1
{
public string Say()
{
Console.WriteLine("Base1");
}
}

public class MyBase2
{
public virtual string Say()
{
Console.WriteLine("Base2");
}
}

public class Child1 : Base1
{
new public string Say()
{
Console.WriteLine("Child1");
}
}

public class Child2 : Base2
{
public override string Say()
{
Console.WriteLine("Child2");
}
}


Child1 ch1 = new Child1();
Base1 bs1 = (Base11)ch1;

ch1.Say();
bs1.Say();

//this will produce following lines:
// Child1
// Base1

while this:

Child1 ch2 = new Child2();
Base1 bs2 = (Base2)ch1;

ch2.Say();
bs2.Say();

//will produce:
// Child2
// Child2

The conclusion:

"new" hides the base method, with a new one. So depending on the
invocation, one or another will be executed.

"override" replaces the base method.


This is very simplified explanation of course. It took me some time to
grok it as well :)

Sunny
 

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