Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 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 January 23rd, 2008, 02:01 AM
Registered User
 
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to nf0001384
Default A question about abstract class

In the Book Begging C# 2005 ,chapter 11, the try it out example Ch11Ex01:
I defined an abstract class called animal .
    public abstract class Animal
As I have learned from the book ,abstract class can not be instantiated .
But in the code of Main block , the example used code as " Animal [] animalArray=new Animal[2]".

and the program runs without any problem . Is the sentence above instantiated the class "Animal"? Since I've learned , if I use the keyword "New" , that means I instantiate a class.

Is there something wrong about the concept?
If possible , please tell me ,I'd be appreciated.

 
Old January 23rd, 2008, 04:22 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

No, that's declaring and creating an array that will hold Animal types. The Animal type cannot be instantiated directly, as you note, so it will end up holding concrete classes derived from Animal.

--

Joe (Microsoft MVP - XML)
 
Old January 23rd, 2008, 04:29 AM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try to call for example animalArray[0].SomeAnimalMethod() and you'll see what happens ;)

 
Old January 23rd, 2008, 05:32 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

'new Animal[2]' does not create 2 new Animal classes, it creates one instances of an Array class, which is designed to hold two Animal instances. It does not create those instances, just holes where they can live.

/- Sam Judson : Wrox Technical Editor -/
 
Old April 5th, 2013, 05:57 AM
Registered User
 
Join Date: Mar 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It is just an array declaration.
Code:
string [] names = new string[6]             //Declaration of array, 'name' with 6 
                                                       //elements of datatype, 'string'. 
Animal [] animalArray=new Animal[2]      //Similarly, declaration of array, 'animalArray' with 2 
                                                      //elements of derived type, 'Animal'.
The line of code has got nothing to do with instantiation of class, 'Animal'.

Last edited by vickyanu; April 5th, 2013 at 06:03 AM..
 
Old July 22nd, 2013, 06:58 AM
Registered User
 
Join Date: Jul 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by joefawcett View Post
No, that's declaring and creating an array that will hold Animal types. The Animal type cannot be instantiated directly, as you note, so it will end up holding concrete classes derived from Animal.

--

Joe (Microsoft MVP - XML)
I am agree with your answer, and its helps to those who suffering form same problem.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Why abstract class can't be instantiated? anisurr C++ Programming 5 July 23rd, 2008 01:32 AM
question about sub New() in class hertendreef ASP.NET 2.0 Basics 7 January 3rd, 2007 02:10 PM
interface and abstract class sreenu.pocha C# 2 July 26th, 2006 07:18 AM
question about abstract class/polymorphism csharplearner C# 4 October 11th, 2005 03:27 PM
How to write abstract class to xml haiying General .NET 4 September 21st, 2004 08:32 AM





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