You must Sign In to post a response.
  • Category: .NET

    Abstract Vs Interface

    Hi,

    Can any one tell me when we will go for Abstract and interface ..

    Can you please explain with Examles ?

    Regards,

    Nidheesh
  • #603191
    This of it like this.

    You have many entities like Employee, Depenents, Beneficiaries, Managers in your database but ultimately they all are "Person" which are going to have some similar attributes/properties like firstname, lastname, address, email, phone etc., In this case, You can have an Abstract Class of type "Person" with maximum available attributes that other entities defined above can extend and use them.

    Now, you want to implement some similar functionalities like UpdatePersonalInfo() that can be used by any of above entities, BUT GetSalaryDetail() may only be used by Employee or Manager entities. You can think of creating interface for these type of similar functionalities.

    Example for simple understanding.
    Think of these entities: Manager, Executive and Clerk. They all are Employee of a company. So, Employee can be an Abstract Class that can be extended by Manager, Executve and Clark. Now, you want to have some similar functionality like Calculate HRA for these entities however, percentage or HRA or say calculation is different for these entities. So, you can have ICalculate interface which is similar becuase it will return HRA amount but still can have different logic in it while it will be implemented by each of above entity classes.

    //--abstract class of type Employee to be extended by Manager, Executive and Clerk
    public abstract class Employee
    {
    public string _firstName;
    public string _lastName;
    public string _homeAddess;
    public string _emailAddress;
    }

    //--interface for common functionality among the classes which extends the abstract class
    public interface ICalculate
    {
    decimal ApplyHra(decimal BasicSalary);
    }

    //--manager class
    public class Manager : Employee, ICalculate
    {
    public string _someManagerAttributes;

    public decimal ApplyHra(decimal BasicSalary)
    {
    return (BasicSalary * 30M) / 100;
    }
    }

    //--executive class
    public class Executive : Employee, ICalculate
    {
    public string _someExecutiveAttributes;

    public decimal ApplyHra(decimal BasicSalary)
    {
    return (BasicSalary * 35M) / 100;
    }
    }

    //--clerk class
    public class Clerk : Employee, ICalculate
    {
    public string _someClerkAttributes;

    public decimal ApplyHra(decimal BasicSalary)
    {
    return (BasicSalary * 25M) / 100;
    }
    }

    //--created a factory pattern class that will return proper interface object to calculate HRA based on passed empGrade
    public static class BusinessCalculation
    {
    public static ICalculate GetHraCalculation(string empGrade)
    {
    switch (empGrade.ToLower())
    {
    case "manager":
    return new Manager();
    case "executive":
    return new Executive();
    case "clerk":
    return new Clerk();
    }
    return null;
    }
    }

    So To get the Excutive HRA amount you can call via static factory class as:



    ICalculate objCalculation = BusinessCalculation.GetHraCalculation("executive");
    decimal hraResult = objCalculation.ApplyHra(50000);

    Regards
    Pankaj Rajput

  • #603195
    Interface is an abstract class with public abstract methods all of which must be implemented in the inherited classes.

    In C#, Class doesn't support multiple inheritance directly. However, we can ues Interface to have Multiple Inheritance.

    We use Abstract Class when there is a common strcture which can be implemented by multiple classes. That is why Abstract Classes cannot be instantiated, instead the derived class takes the responsibility to implement the abstract class.


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #603198
    Hi Pankaj,

    thanks for your detailed explanation. i have one more doubt. What is the use of interface other than multple inheritance?

    Regards,
    Nidheesh

  • #603203
    Difference between Abstract class and interface-

    The abstract class allows concrete methods (methods that have implementation) but interface does not.

    A class can inherit from only one class, but can implement any number of interfaces.

    Interface doesn't allow variables/constants to be declared but abstract class allows for declaration of variables and constants.

    You should consider using interface when your design is complete in itself i.e. no further methods need to be added later on the interface as any newly added method in interface need to be implemented in all classes that implement that interface. On the other hand, if we need to add a new method to an abstract class, we can provide default implementation of that method in the abstract class and there is no need for existing implementing classes to be changed in that case.

    If it helps you then Rate this.

    Best Regards,
    Manoranjan Sahoo
    http://www.dotnetsquare.com
    Blog: http://blog.msahoo.net

  • #603208
    Hi,

    C# does not supports multiple inheritance directly That's why we have an interface.The main purpose of interface is to provide multiple inheritance in C#:

    For more details follow the below link:
    http://www.daniweb.com/software-development/csharp/threads/114364
    http://fci-h.blogspot.com/2008/03/oop-design-concepts-interfaces_05.html

    Regards
    Pankaj Rajput

  • #603216
    • A class can implement any number of interfaces but a subclass can at most use only one abstract class.
    • An abstract class can have non-abstract Methods (concrete methods) while in case of Interface all the methods have to be abstract.
    • An abstract class can declare or use any variables while an interface is not allowed to do so.
    An abstract class can have constructor declaration while an interface cannot do so.
    An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface we can not declare any access modifier (including public) as all the members of interface are implicitly public.

  • #603218
    Hi all,

    thanks for your helps..

    nidheesh

  • #603219
    Hi Pankaj,

    The Better is To used Interface as compare To Abtract Class .

    Why?

    Because if you Inherit the Class You cannot Inherit Another Class.Becuase Multiple Inheritance in Not Allowed In c#.The Reason Behind This is Name ambiguity also called Diamond Problem.


    And If You Write Code in Interface Instead of Abstract Class,Then

    You Can Inherit Interface And You Have a Chance To Inherit One Class.

    Here I Am Not Telling That Abstract Class Does Not Have Any Feature.

    -in Abstract Class You Can ProVide Method Implementation But In Case of Interface You Cant Implement Method.


    -if Inherit Abstract Class Its Upon You To implement All or Few Of Its Method.But in Case ofinterface You Have To implement All Its Method in Inheting Class.

    Thanks,
    Hope This Help You.

  • #603250
    Hi nidheesh,

    1) Interface is only public modifiers for methods but abstract class is public or protected.

    2) Interface is a way of specifying the behavior of classes and methods of an interface are abstract methods.

    3) Abstract class is contain fields and implement properties but interface cannot contain fields and it has only the property's signature but no implementation.

    4) Abstract class does not show full abstartion but interface is a pure abstract class as it has only abstract methods.

    _____________________________________________________________________________

    Regards,
    Muhammad Farooq
    (DNS Mentor)
    Visit My Website....www.itechsoul.com

  • #603347
    Hi,

    Following are the difference between abstract and interface,

    1>Abstract class having method declaration as well as method method definition whereas interface having method declaration only.

    2>Abstract class are known as partial abstract class whereas interface is known as fully abstract class.

    3>Abstract class features we have to inherit to the child class whereas interface features we have to implement in the child classes.

    4>Abstract class support access specifiers whereas interface doesn't support access specifiers.

    5>Abstract class have normal variable as well as constant variable whereas interface have only constant variables.

    6>We can write constructor in abstract class whereas we can't write constructor in interface.


    Thanks & Regards


    Bunty

  • #604616
    [Response removed by Admin. Read forum policies.]


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