what is abstract.why we can not creat object for abstract class

Posted by Fundaravi under C# on 4/17/2012 | Points: 10 | Views : 4515 | Status : [Member] | Replies : 4
Why we are not create object for abstract class, what is the reason behind that? Give me with example

Ravindra


Responses

Posted by: Sabarimahesh on: 4/17/2012 [Member] Bronze | Points: 25

Up
0
Down
Because that's what the abstract class is for - specifying a set of methods a child class must implement on its own, while also allowing you to specify default implementations for some methods if you so desire.

This of it like that - you have a system that accepts "plug-ins" that are defined by a class that inherits your abstract class. Why would you ever want to load a plug-in that doesn't do anything beyond the defaults (if any)? You wouldn't... you'd expect the plug-in creator to define the methods you expect, and then call them from within your system to actually let the plug-in do its thing. If you could instanciate an abstract class, you'd essentially load an empty plug-in.

You could actually not use abstract classes, and just use normal classes. However, they won't allow you to define the methods a class must implement, which is where your system may start breaking.

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 4/17/2012 [Member] Bronze | Points: 25

Up
0
Down
The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class.

An abstract class cannot be instantiated. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class.

Please read : http://msdn.microsoft.com/en-us/library/ms173150.aspx

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kk86kiran on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
Abstract means we can not instantiate an object for that class. It has abstract methods and non abstract methods. Abstract methods means it has only declaration. non abstract methods means it has declarations and implementations. if you want to implement abstract methods you should implement in child class.

Abstract class Myclass

{
public abstract void abstractMethod();
public void NonAbstractMethod()
{
Console.WriteLine("NonAbstract Method");
}
}
}

class childClass:Myclass
{
static void Main(string[] args)
{
childClass obj = new childClass();
obj.NonAbstractMethod();
obj.abstractMethod();
}
public override absractMethod()
{
Console.WriteLine("Abstract Method");
}
}


k.m.j.kiran

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/17/2012 [Member] Starter | Points: 25

Up
0
Down
An abstract class is a class which doesnt have an implementation for one or more methods.
It means that the jvm doesnt have any direction of what to do in case if someone calls the method which is abstract.
So if you are able to create an object for the abstract class and call any abstract method of it the jvm will not be able to decide what to do and hence it my be crashed.
So to avoid this situation we are restricted to instantiate a abstract class.
If you need a object for that abstract class create a concrete subclass and create an object for it and use it.

------------------------------------------------
Learn throughout life

Fundaravi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response