Wrox Programmer Forums
|
BOOK: Beginning Visual C# 2012
This is the forum to discuss the Wrox book Beginning Visual C# 2012 by Karli Watson, Jacob Vibe Hammer, Jon Reid, Morgan Skinner, Daniel Kemper, Christian Nagel, ; ISBN: 978-1-118-31441-8
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Visual C# 2012 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 August 22nd, 2014, 05:42 PM
Registered User
 
Join Date: Aug 2014
Posts: 1
Thanks: 1
Thanked 0 Times in 0 Posts
Default Abstract classes

Near the beginning of chapter 11, there is a section on Collections with a lengthy code example. In the example an abstract base class, Animal, is created with Cow and Chicken classes derived from it.

My understanding is that an abstract class cannot be instantiated. However the example on p. 238 contains this code:
Code:
Animal[] animalArray = new Animal[2];
Cow myCow1 = new Cow("Deirdre");
animalArray[0] = myCow1;
It appears to me that this code is instantiating the Animal class by creating an array of two Animal objects.

Can you explain what I am missing about not being able to instantiate an abstract class?
 
Old September 13th, 2014, 10:34 PM
Registered User
 
Join Date: Jan 2014
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Default

The code line Animal[] animalArray = new Animal[2]; is not instantiating an Animal object, it's declaring and instantiating an array of Animal variables which can hold references to derived objects of abstract class Animal.
The Following User Says Thank You to Plethorean For This Useful Post:
megreen (September 15th, 2014)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Abstract and Interface classes vbboyd BOOK: Beginning ASP.NET 4 : in C# and VB 1 May 5th, 2012 03:16 AM
Abstract classes/Interfaces vasancer BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 2 October 22nd, 2008 02:06 PM
polymorphism and abstract classes elvisfeverr C++ Programming 3 October 16th, 2008 01:39 AM
Abstract classes guppyheart C# 1 September 9th, 2003 05:23 PM





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