Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Professional C# 4.0 and .NET 4
This is the forum to discuss the Wrox book Professional C# 4.0 and .NET 4 by Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner; ISBN: 9780470502259
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional C# 4.0 and .NET 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
  #1 (permalink)  
Old September 24th, 2010, 10:03 AM
Registered User
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Why use base class as identifier?

On page 98 of the book in the example about multiple constructors in inheritance there is the following piece of code:

GenericCustomer customer = new Nevermore60Customer("Arabel Jones")

Why would you do this as opposed to:

Nevermore60Customer customer = new Nevermore60Customer("Arabel Jones")

I was much confused by this.
  #2 (permalink)  
Old September 24th, 2010, 04:57 PM
Friend of Wrox
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

First of all, the base class is *NOT* being used as an "identifier".

The only identifier in there (either version) is "customer".

As to why declare the variable as the base class: Since I don't have the book, I can't tell for sure, but *probably* because the author(s) are going to demonstrate polymorphism. That is, they are going to have some other code that takes an instance of the base class--not the derived class--so why declare the variable as an instance of the derived class??

Oh, more than likely the code will work either way. That is, if there is a function that takes GenericCustomer as an argument, it would of course take a variable declared as Nevermore60Customer as the argument. But unless you *need* some characteristic of Nevermore60Customer, it really makes no difference.

In "real life" code, you would probably be more likely to see code that looks like this:
GenericCustomer customer = functionThatMightReturnAnyClassDerivedFromGenericCustomer("Arabel");
So I think the author(s) are just illustrating the point that you can always assign an instance of a derived class to a variable declared to be of the type of the base class.

Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid and the WDA Base Class sirmilt BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 3 June 23rd, 2009 06:49 AM
Base Class for Code-Behind Problem groupmatch BOOK: ASP.NET Website Programming Problem-Design-Solution 3 September 8th, 2004 05:06 AM
classes :: base class is inaccessible .... Kaliste C# 3 July 16th, 2004 04:56 AM
base class inheritance krunch97 VB.NET 2002/2003 Basics 6 April 23rd, 2004 11:48 AM
Help with DBObject base class projectedNexus BOOK: ASP.NET Website Programming Problem-Design-Solution 1 August 12th, 2003 06:36 PM

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