Search
Sponsor
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Blogs Author
Tue, 21-Oct-2014 Authors
All Time Authors
Samirbhogayta
9750
Satyapriyanayak
7575
Jvprabhusanthi
6150

Latest members | More ...


(Statistics delayed by 5 minutes)
 Blogs

Introduction of Abstract class and Abstract method of C#

Karam
Posted by Karam under C# on 8/24/2011 7:25:01 AM | Points: 75 | Views : 2812 | Status : [Member]

        Introduction of Abstract classes and Abstract methods of c#


 


              abstract  keyword is used to declare abstract classes and abstract methods in c#.


 


       Abstract class


 



Ø      The concept of Abstract Class and Abstract method is used under the concept of Inheritance. The abstract class can only be a base class of other classes.


 


Ø      Declaring class as abstract simply means this class cannot be instantiated , In simple words its object cannot be created.  As the instance cannot be created of this abstract class but can inherit another class and by creating the instance of this derived class you can access the method of the abstract class.


 


Ø        Abstract class plays a role when in lot of cases we don't want to create object of specific classes but still we want to take some base functionalities of that base class for its derived classes.


 


Ø       Abstract classes may or may not contain any concrete and even abstract methods.


 


Ø      Abstract classes are templates for future specific classes.


 


 


.


       Abstract method


 



Ø      Abstract methods are only declared in abstract classes.


 


Ø      Abstract method or abstract property just indicates that the method or property does not contain any implementation.


 


Ø      Because of no actual implementation in declaration of abstract method , it does not  contain any method body and simply ends with a semicolon without opening and closing curly braces.


 


Ø      Abstract methods and properties must be implemented in derived classes of abstract class.


 


Ø      Abstract method is implicitly a virtual method.


 


Ø       If you create a abstract method then you must override this method in the subclass otherwise it shows error in the program.


 



               Program to understand the concept of Abstract class.


 


public abstract class Organization


{


    //...Class implementation


 


    public abstract void Salary(int x, int y)


    {


        //this method mustn't be implemented here.


        //If we do implement it, the result is a Syntax Error.


    }


}


 


 


public abstract Department1 : Organization


{


    //...Class implementation


    //...you do not have to implement the the method Salary(int x, int y)


}


 


public class Designation : Department1


{


    //here we should provide an implemetation for Salary(int x, int y)


    public override void Salary(int x, int y)


    {


        //must do some Calculation here


    }


 


}


 


 


 


Karam
Found interesting? Add this to:



Comments or Responses

Posted by: Vishvvas on: 7/14/2011 | Level:HonoraryPlatinum | Status: [Member] | Points: 15

Good attempt. Observed few debatable points
1.Abstract classes are..incomplete classes:
Classes can't be termed as complete or incomplete, rather its a blueprint for object.
2. The basic purpose behind using the abstract class is to provide common definition of base class .
No, surely its not the basic purpose of abstract class. Abstraction is a technique in Object Oriented Programming and immplemented through different mechanisms in different technologies and languages.The basic purpose can be said to abstract the complex domain or logical objects promoting resue, DRY (Don't repeat yourself) and represent inheritance of OOPs.
3.The example given is logically questionable. Actually in .NET framework, the sorting is implemented through interface IComaparable adn custom sorting is achieved through IComparer.
Hope this helps.

Posted by: Karam on: 7/14/2011 | Level:Starter | Status: [Member] | Points: 15

Hi Vishvvas,
I accept your points as these helped me to upgrade this concept...and contents will be updated soon.

Thanks,

Posted by: Vishvvas on: 7/14/2011 | Level:HonoraryPlatinum | Status: [Member] | Points: 15

Karam,
Great attitude shown by you as I was little sceptical about posting. Anyway, I would look forward to upgraded article and Kudos to your attempt.

Regards

Posted by: Kishork80 on: 7/27/2011 | Level:Starter | Status: [Member] | Points: 15

§ Abstract class should contain atleast one abstract method But not compulsory to have all the methods abstract as it can also contain concrete methods (concrete method-A method having implementation and no need to override in derived class in this case).


Just to point here Abstract class should contain atleast one abstract method is not correct. We can have an abstract class without having any abstract/concrete functions.
example:
abstract class AbsClass
{
//No abstract/concrete functions
}

Posted by: Karam on: 7/29/2011 | Level:Starter | Status: [Member] | Points: 15

Could u tell me the benefits of using that class u mentioned...........I mean the logic and use of using this abstract class without having abstract or concrete functions......

Posted by: Karam on: 8/10/2011 | Level:Starter | Status: [Member] | Points: 15

Hi Kishor,

Please tell me the purpose of using that abstract class by giving an example, you mentioned above , which is not having any abstract as well as concrete methods and functions As if you are posting any comment , u should clear your point and any suggestion is acceptable to improve this topic.



Posted by: Mlaxman on: 8/18/2011 | Level:Starter | Status: [Member] | Points: 15

Hi Karam,

Good post... really helpful for many!

Well, Kishor is right (We can have an abstract class without having any abstract/concrete functions.)

I will give you the scenario where in you will not need any abstract/concrete method yet the class will be useful.

E.g:
In models/classes, generally we need some common attributes, for instance Id, IsDeleted, LastUpdatedDate, ModifiedBy etc. Instead of writing them in all models we can have one common BaseModel with such reusable attributes (Please note they need not be abstract or concrete) and use them in as many models as we want by inheriting this BaseModel.
With this we are making sure that no one is creating the instance of BaseModel... it needs to be inherited in order to use.
Example (C#) :

public abstract class BaseModel

{
public int Id { get; set; }
public bool IsEnabled { get; set; }
public bool IsDeleted { get; set; }
public DateTime LastUpdatedDate { get; set; }
public string ModifiedBy { get; set; }
}

public class Customer : BaseModel
{
//We dont really need these attributes to be declared in all classes...
//public int CustId { get; set; }
//public DateTime LastUpdatedDate { get; set; }
//public string ModifiedBy { get; set; }

public string FirstName { get; set; }
public string LastName { get; set; }
}


Hope I am clear to you.... Please let me know if anything is not clear or doesn't make any sense...


Posted by: Karam on: 8/24/2011 | Level:Starter | Status: [Member] | Points: 15

Thanks Laxman




Posted by: Mlaxman on: 8/24/2011 | Level:Starter | Status: [Member] | Points: 15

I am glad to know that it helped you... :)


Login to post response