C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

Abstract class and create(d) an instance in one application


Posted Date: 10 Oct 2008      Posted By: Hari      Member Level: Silver     Points: 1   Responses: 1



Hi All,

Here I have a doubt regarding basic defn. of abstract class, that we cant able to instantiate the abstract class that is we cant create an instance.

Please look at the follwg code:
****************************************************************************
public abstract class AuthorizationManager
{
#region Variables
private static AuthorizationManager objAuthorizationManager = null;
static string authorizationProviderReflection = System.Configuration.ConfigurationSettings.AppSettings["AuthorizationProviderReflection"];
#endregion


static AuthorizationManager()
{
objAuthorizationManager = (AuthorizationManager)Activator.CreateInstance(System.Type.GetType(authorizationProviderReflection ));
}

public static AuthorizationManager Instance()
{
return objAuthorizationManager;
}
}

//And we are invoking the abstract method (from the abstract class) by using:

objAuthorize = AuthorizationManager.Instance().GetAuthorization(info, ObjectType, ObjectId);

******************************************************************************

In the above piece of code we are creating an instance by using reflection(Activator.CreateInstance()),
please explain me in detail , whether my understanding is not right?
If it is true we are able to create an instance either through reflection(or runtime) we are creating an instance for an abstract class...





Responses

Author: Shankar Suresh    10 Oct 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

Abstract class is a class that has no direct instances, but whose descendants may have direct instances. There are case i which it is useful to define classes for which the programmer never intends to instantiate any objects; because such classes normally are used as bae-classes in inheritance hierarchies, we call such classes abstract classes These classes cannot be used to instantiate objects; because abstract classes are incomplete. Derived classes called concrete classesmust define the missing pieces.

Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts ot those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error, unless the derived class also is an abstract class.

In some cases, abstract classes constitute the top few levels of the hierarchy, for Example abstract class Shape with abstract method Draw() has tow derived abstract classe Shape2D & Shape3D inherites the method Draw() & also do not provide any implementation for it. Now we have normal classes Rectangle, Square & Circle inherites from Shape2D, and another group of classes Sphere, Cylinder & Cube inherites from Shape3D. All classes at the bottom of the hierarchy must override the abstract method Draw().

A class is made abstract by declaring it with Keyword abstract.

Example:
public abstract class Shape
{
//...Class implementation

public abstract void Draw(int x, int y)
{
//this method mustn't be implemented here.
//If we do implement it, the result is a Syntax Error.
}
}


public abstract class Shape2D : Shape
{
//...Class implementation
//...you do not have to implement the the method Draw(int x, int y)
}

public class Cricle : Shape2D
{
//here we should provide an implemetation for Draw(int x, int y)
public override void Draw(int x, int y)
{
//must do some work here
}

}

Difference between an abstract method & virtual method:

Virtual method has an implementation & provide the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.

important Notes:

(a)Any Class with abstract method or property in it must be declared abstract

(b)Attempting to instantiate an object of an abstract class retults in a compilation error

Example:

Shape m_MyShape = new Shape(); //it is Wrong to that.

But we can do that.
Shape m_MyShape = new Circle(); // True

Or
Shape m_MyShape;
/*

declare refrences only, and the refrences can refer to intances of
any concrete classes derived from abstract class
*/

Circle m_MyCircle = new Circle();
m_MyShape = m_MyCircle; // Also True

(d)An abstract class can have instance data and non-abstract methods -including constructors-.

Tutorial

Our Tutorial has 4 classes { Shape --> Point --> Cirlce --> Cylinder } Abstract Class Shape has 2 virtual methods Area() & Volume(), and one abstract property ShapeName

Class Point only override the inherited property ShapeName as point do not have any area or volume Also it has to properties X & Y (pntA[X,Y]) for example

Class Cirlce override method Area() & the property ShapeName and doen't override volume as it has no volume. Also Cirlce has some methods to provide its Diameter & Circumference which will be used by class Cylinder.

Class Cylinder override all methods in Class Shape, and use the service methods in Class Circle (Diameter & Circumference) to calculate the Area() & Volume(). Check the project and it is well commented.



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Excel filter problem in c#
Previous : OnFocus
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use