What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 58480 |  Welcome, Guest!   Register  Login
 Home > Blogs > C# > Introduction of Abstract class and Abstract method of C# ...
Karam

Introduction of Abstract class and Abstract method of C#

 Blog author: Karam | Posted on: 8/24/2011 | Category: C# Blogs | Views: 1916 | Status: [Member] | Points: 75 | Alert Moderator   


        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:



 More Blogs from Karam

     More ...

    About Karam C

    Experience:0 year(s)
    Home page:http://www.dotnetfunda.com
    Member since:Thursday, June 30, 2011
    Level:Starter
    Status: [Member]
    Biography:

     Responses

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

    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.

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

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

    Thanks,

    Karam

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

    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

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

    § 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
    }

    kishor kumar

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

    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......

    Karam

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

    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.



    Karam

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

    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...


    Cheers,
    Laxman Mankala

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

    Thanks Laxman




    Karam

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

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

    Cheers,
    Laxman Mankala

    >> Write Response - Respond to this post and get points

    More Blogs

    About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
    General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
    Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 11:04:31 AM