|
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
|
|
|
January 23rd, 2008, 02:01 AM
|
Registered User
|
|
Join Date: Jan 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
January 23rd, 2008, 04:22 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
January 23rd, 2008, 04:29 AM
|
Authorized User
|
|
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Try to call for example animalArray[0].SomeAnimalMethod() and you'll see what happens ;)
|
January 23rd, 2008, 05:32 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
'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 -/
|
April 5th, 2013, 05:57 AM
|
Registered User
|
|
Join Date: Mar 2013
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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..
|
July 22nd, 2013, 06:58 AM
|
Registered User
|
|
Join Date: Jul 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
Originally Posted by joefawcett
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.
|
|
|