Wrox Programmer Forums
|
BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4
This is the forum to discuss the Wrox book Beginning Microsoft Visual C# 2008 by Karli Watson, Christian Nagel, Jacob Hammer Pedersen, Jon D. Reid, Morgan Skinner, Eric White; ISBN: 9780470191354
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual C# 2008 ISBN: 978-0-470-19135-4 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 24th, 2009, 10:17 AM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Polymorphism -- Help Me Please

Hello, can some one please read this and comment on the sections in RED??

Extract from page 197 of Beginning Microsoft Visual C# 2008 (ISBN: 978-0-470-19135-4)


Polymorphism

One consequence of inheritance is that classes deriving from a base class have an overlap in the methods and properties that they expose. Because of this, it is often possible to treat objects instantiated from classes with a base type in common using identical syntax. For example, if a base class called Animal has a method called EatFood() , then the syntax for calling this method from the derived classes Cow and Chicken will be similar:

Cow myCow = new Cow();
Chicken myChicken = new Chicken();
myCow.EatFood();
myChicken.EatFood();
Polymorphism takes this a step further. You can assign a variable that is of the base type to a variable of one of the derived types, as shown here:
Animal myAnimal = myCow;
[ ISSUE : Shouldn't the statement above read.. "You can assign a variable of one of the derived types to a variable of the base type" ??]
No casting is required for this. You can then call methods of the base class through this variable:
myAnimal.EatFood();
This results in the implementation of EatFood() in the derived class being called. [ ISSUE : This statement is true only if EatFood() is virtual in the base class Animal and it is overridden in derived class Cow. If not, the EatFood() implementation in base class will be called.]--Am I right??
 
Old July 16th, 2009, 05:00 PM
Registered User
 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default I agree!

I have tested the sample and I agree with both of your opinions.
 
Old July 29th, 2009, 03:11 PM
Registered User
 
Join Date: Jun 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok i was right.
http://wrox.custhelp.com/app/account...il/i_id/308336
 
Old October 1st, 2009, 03:06 AM
Braindea
Guest
 
Posts: n/a
Default Thanks for the wording

On your first comment - I followed the wording in the book, but agree that your wording is more correct and clear. Thanks especially for the clarification on the way the method calls cascade (overlap).
 
Old October 18th, 2009, 06:56 AM
Registered User
 
Join Date: Sep 2009
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation Why is this not in the book's errata?!?!

I was just going to post the same exact thing! This actually thew me off on later chapters with Interfaces.

The statement:
You can assign a variable that is of the base type to a variable of one of the derived types...

Is backwards! We're assigning a derived type TO a base type.

Your statement is better:
You can assign a variable of one of the derived types to a variable of the base type.

But i think what's going on is more like:
You can assign a REFERENCE of a derived type to a base type. (as these are reference types in C#)

This statement confused me and i wondered what exactly is happening there? Does myAnimal become myCow.. etc. Rehashing some C++ i learned a while ago, i was able to gain an understanding.

In C++ imagine you have an array of pointers of type animal:
Animal* myAnimalArray[]
Pointers are simply like variables that contain an address. In this array, you have pointers that point to various objects derived from Animal (cow, chicken, duck, etc). So when you call a function with one of these pointers, polymorphism allows the correct (derived) function to be called. Essentially, the same thing happens here in C# - we give a reference of myCow to myAnimal, so:

myAnimalArray[0] -> DoSomething(); (assuming index 0 is a pointer to a cow object)
Or maybe more simply
*myCowPointer->DoSomething(); (the myCowPointer is of type Animal)
is the same as
myAnimal.DoSomething(); (given that: myAnimal = myCow;)

In the first example, a pointer of Animal type, which is pointing to a Cow object, calls the DoSomething() function. The -> replaces . for pointers. With polymorphism, it knows to call the overwritten function.

I think you're also correct in saying that it must be virtual and overriden in the derived class!

What i don't understand is why this is not included in the errata!?!?! The first statement is obviously incorrect!

Can the authors chime in on this?

Last edited by PolishPaul; October 18th, 2009 at 07:04 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
need of polymorphism? pavanonforums C++ Programming 4 February 22nd, 2018 03:36 AM
Need help with Polymorphism Piash Java Basics 1 August 8th, 2007 05:24 AM
What is Interface Polymorphism in C#? ramk_1978 C# 1 May 3rd, 2006 01:23 AM
Polymorphism with Interfaces digby_dog VB.NET 2002/2003 Basics 2 May 11th, 2005 12:50 PM
Polymorphism and Casts g_r BOOK: Beginning Java 2 1 July 13th, 2003 08:38 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.