-
February 5th, 2010, 04:10 PM
#16
Re: real world polymorphism
We responded at the same time, but don't use 'new' keyword in your derived class implementations. Use 'override' instead.
'new' hides the derived class' implementation. If you use 'new', then you can't do:
Human human = new Superhero();
You can, but then all human.Member calls will be calling Human versions instead of Superhero versions.
You must do:
Superhero human = new Superhero();
If you want to use the 'new' version of each implemented member.
The easy solution is to change 'new' to 'override' in your derived classes.
Then you can do:
Human human = new Superhero();
And human.Method will call the Superhero() overriden version instead of the base Human version.
-
February 5th, 2010, 04:10 PM
#17
Re: real world polymorphism
Originally Posted by sidhu688
ok so here is the program parts and the questions
protected new int power = 50; why the new keyword is used here ?
public int HumanPower { get { return base.power; } } // will this return the power of the base class?
public new int Power { get { return this.power; } } // is this used for returning power of the current object ?
Console.WriteLine("is human a human? " + human is Human);
Everything is coming false when i am running this code and a green line is coming below these and says the given expression is never of the provide ('oops_Concepts.Human) type ?
Console.WriteLine("memloo's human power: " + ((Human)memeloo).Power);
((Human).memeloo) what does this mean ?
Have you tried running the code? If not, try running the code and think about the output that you see. It might answer some of your questions.
-
February 5th, 2010, 04:13 PM
#18
Re: real world polymorphism
Originally Posted by sidhu688
public int HumanPower { get { return base.power; } } // will this return the power of the base class?
public new int Power { get { return this.power; } } // is this used for returning power of the current object ?
modify the code, run it and see it for yourself.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
-
February 5th, 2010, 04:15 PM
#19
Re: real world polymorphism
Originally Posted by mariocatch
We responded at the same time, but don't use 'new' keyword in your derived class implementations. Use 'override' instead.
you're right. it was stupid here to use the "new". I'll correct it right away.
edit: corrected
Last edited by memeloo; February 5th, 2010 at 04:23 PM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation
private lessons are not an option so please don't ask for help in private, I won't replay
if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|