GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Microsoft  >  C#
Go To First  |  Previous Question  |  Next Question 
 C#  |  Question 347 of 436    Print  
Can u create the instance for abstract classes

  
Total Answers and Comments: 5 Last Update: March 04, 2009     Asked by: v k kumar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Gaurav
 

This is not working. Following is the code:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication2

{

public abstract class sayhello

{

public void hello()

{

Console.WriteLine("hello");

}

}

class Program

{

static void Main(string[] args)

{

Object o=System.Activator.CreateInstance(typeof(sayhello));

((sayhello)o).hello();

}

}

}

Please clarify..



Above answer was rated as good by the following members:
yunuskhan
June 28, 2006 05:14:03   #1  
Ashishtandon.e Member Since: March 2006   Contribution: 14    

RE: Can u create the instance for abstract classes

no we cannot instanciate abstract class we hav to extend it first

ex- abstract class A

{}

class B : A

{}

Class C

{

B b new B;

}


 
Is this answer useful? Yes | No
July 03, 2006 11:33:08   #2  
Rich        

RE: Can u create the instance for abstract classes
This is sort of a trick question. You can instantiate an instance of an Abstract class by using System.Activator.CreateInstance
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
July 09, 2006 10:04:01   #3  
Elina        

RE: Can u create the instance for abstract classes
No you cannot it thows exception System.MemberAccessException: Cannot create an abstract class
 
Is this answer useful? Yes | No
September 26, 2006 07:11:57   #4  
Gaurav        

RE: Can u create the instance for abstract classes

This is not working. Following is the code:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication2

{

public abstract class sayhello

{

public void hello()

{

Console.WriteLine( hello );

}

}

class Program

{

static void Main(string[] args)

{

Object o System.Activator.CreateInstance(typeof(sayhello));

((sayhello)o).hello();

}

}

}

Please clarify..


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
March 04, 2009 06:59:59   #5  
yunuskhan Member Since: March 2009   Contribution: 1    

RE: Can u create the instance for abstract classes
We can create an instance of an abstract class.

The below code is not working:

sayHello is an abstract class

Object o System.Activator.CreateInstance(typeof(sayhello));


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape