Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 23rd, 2008, 02:01 AM
Registered User
 
Join Date: Jan 2008
Location: Shanghai, , .
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.

  #2 (permalink)  
Old January 23rd, 2008, 04:22 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
  #3 (permalink)  
Old January 23rd, 2008, 04:29 AM
Authorized User
 
Join Date: Sep 2007
Location: , , Finland.
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 ;)

  #4 (permalink)  
Old January 23rd, 2008, 05:32 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
  #5 (permalink)  
Old April 5th, 2013, 05:57 AM
Registered User
Points: 22, Level: 1
Points: 22, Level: 1 Points: 22, Level: 1 Points: 22, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2013
Location: Chennai
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..
  #6 (permalink)  
Old July 22nd, 2013, 06:58 AM
Registered User
Points: 3, Level: 1
Points: 3, Level: 1 Points: 3, Level: 1 Points: 3, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 09:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.