locked
diff between interface and abstract RRS feed

  • Question

  • Hi,

    I want brief explanation on Interface and abstract.  

    Please explain following questions...

    1. Why we have to create interface? Why class implement that interface? if our project do not have any interface or abstract what is consequences ?
    2. which place it is suitable creating interface?
    3. which circumstance developer will decide whether required interface or abstract?

    Please provide answer on the same.

    Regards & thanks
    Thirumurugan


    Tuesday, November 20, 2007 5:38 AM

Answers

  • Difference between Abstract & Interface class

    Abstract class can have abstract and as well as concrete members but Interface can have only abstract members i.e. implementation is left to the interface implementing classes

    A class can inherit only one abstract class on the other hand a class can implement more than one interface

    Abstract class member can have different modifiers; Interface members can not have any modifiers and Interface modifiers determines the access modifier of Interface members

     

    Interface or Abstract class

    Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.

     

    If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

     

    If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

     

    If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

     

    If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

    Hope this sheds some light on your queries

    Thanks

     

    Tuesday, November 20, 2007 9:52 AM
  • Tuesday, November 20, 2007 9:54 AM
  • Hi,

     

    You need to read up on the subject because there is no "brief" answer. However to get you going, the best way is to show one advantage:

     

    Look at this code

     

    Code Block

    public class CorrectCalculator : ICalculator

    {

    public int Add(int x, int y)

    {

    return x + y;

    }

    }


    public
    class WrongCalculator : ICalculator

    {

    public int Add(int x, int y)

    {

    return x - y;

    }

    }


    public
    interface ICalculator

    {

    int Add(int x, int y);

    }

     

     

    You find 2 different implementations of a Calculator. The trick is that they both implement the same Interface. You can compare it to a contract.This contract is so strong that if you consume it you KNOW that the instantiated class defines a method that is called "Add" with the specifics from the interface.

     

    You might define some helper class that actually creates the calculator. Notice that this factory creates a calculator from a Type. You might define other ways to create a caculator like from a string - that way you can change what calculator implementation to use from configuration - no recompile or anything.

    Code Block

    public class CalculatorFactory

    {

    public ICalculator Create(Type calculatorType)

    {

    object calc = Activator.CreateInstance(calculatorType);

    return (ICalculator)calc;

    }

    }

     

     

    Now all left is the consumer. This consumer doesnt have to know anything about the specific implmenetations. He's just interested in the interface - or contract if you will!

     

    Code Block

    public partial class CalculatorConsumer

    {

    private void Consume()

    {

    CalculatorFactory factory = new CalculatorFactory();

    ICalculator calculator = factory.Create(typeof(WrongCalculator));

    int result = calculator.Add(1, 2);

    }

    }

     

     

    The above example will yield result = -1 because it is the "WrongCalculator" that does the addition. Try change it to "CorrectCalculator" and the result is 3. 

    The above could be even more powerfull if you insert an abstract class between the consumer and the interface, but this would depend on actual usage. What difference there exists between interfaces and abstract classes you can read from here: http://www.codeproject.com/csharp/AbstractsVSInterfaces.asp

    Hope this gets you going in the right direction. Using interfaces can be really powerfull.

     

    --

    Werner

    Tuesday, November 20, 2007 9:57 AM
  • Hi,

     

    Just to explain these terms....

     

    Interface is like a contract. Say, you are a Program Manger or senior developer in your organization. There are few developers inside your organization, who are sharing the code while developing. Many times, developers depends on each other's code.

     

    In this case, you'll tell your developers to write methods and using interface, you'll decide the method signtature. In this case, one developer will be knowing the method signature and start working on it and using it. While the other developer will implement that interface and write the body (his own logic)  for it.

     

    E.g. Microsoft has written a IDisposable interface. When this interface has been implemented, one developer simply calles the Dispose method, while one developer decides, how to dispose the object. And that too, happens parallely.

     

    Yes, you can write code, without using Interface or Abstract classes, in that case, it may take longer time to complete the project as one developer will need to wait till the first one writes the code. But this is just a probability.

     

    It totally depends on developers, whether to implement an Interface or write an abstract class. The difference is, Interface contains only method signatures, while abstract class may have some methods with body.

     

    I hope, this will solve the confusion...

    Tuesday, November 20, 2007 10:32 AM

All replies

  • I don't know the exact answer, but the way I am following, I will let you know.

     

    I create and user interfaces, when I go for COM and abstract classes, in regular programming. Usually Interface and abstracts are used when the functionality has to re-implemented in the derived classes. We will provide the function in these and leave the functionality to the developer who uses our interface......

     

    Please correct me if I am wrong.

    Tuesday, November 20, 2007 9:12 AM
  • Difference between Abstract & Interface class

    Abstract class can have abstract and as well as concrete members but Interface can have only abstract members i.e. implementation is left to the interface implementing classes

    A class can inherit only one abstract class on the other hand a class can implement more than one interface

    Abstract class member can have different modifiers; Interface members can not have any modifiers and Interface modifiers determines the access modifier of Interface members

     

    Interface or Abstract class

    Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.

     

    If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

     

    If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

     

    If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.

     

    If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.

    Hope this sheds some light on your queries

    Thanks

     

    Tuesday, November 20, 2007 9:52 AM
  • Tuesday, November 20, 2007 9:54 AM
  • Hi,

     

    You need to read up on the subject because there is no "brief" answer. However to get you going, the best way is to show one advantage:

     

    Look at this code

     

    Code Block

    public class CorrectCalculator : ICalculator

    {

    public int Add(int x, int y)

    {

    return x + y;

    }

    }


    public
    class WrongCalculator : ICalculator

    {

    public int Add(int x, int y)

    {

    return x - y;

    }

    }


    public
    interface ICalculator

    {

    int Add(int x, int y);

    }

     

     

    You find 2 different implementations of a Calculator. The trick is that they both implement the same Interface. You can compare it to a contract.This contract is so strong that if you consume it you KNOW that the instantiated class defines a method that is called "Add" with the specifics from the interface.

     

    You might define some helper class that actually creates the calculator. Notice that this factory creates a calculator from a Type. You might define other ways to create a caculator like from a string - that way you can change what calculator implementation to use from configuration - no recompile or anything.

    Code Block

    public class CalculatorFactory

    {

    public ICalculator Create(Type calculatorType)

    {

    object calc = Activator.CreateInstance(calculatorType);

    return (ICalculator)calc;

    }

    }

     

     

    Now all left is the consumer. This consumer doesnt have to know anything about the specific implmenetations. He's just interested in the interface - or contract if you will!

     

    Code Block

    public partial class CalculatorConsumer

    {

    private void Consume()

    {

    CalculatorFactory factory = new CalculatorFactory();

    ICalculator calculator = factory.Create(typeof(WrongCalculator));

    int result = calculator.Add(1, 2);

    }

    }

     

     

    The above example will yield result = -1 because it is the "WrongCalculator" that does the addition. Try change it to "CorrectCalculator" and the result is 3. 

    The above could be even more powerfull if you insert an abstract class between the consumer and the interface, but this would depend on actual usage. What difference there exists between interfaces and abstract classes you can read from here: http://www.codeproject.com/csharp/AbstractsVSInterfaces.asp

    Hope this gets you going in the right direction. Using interfaces can be really powerfull.

     

    --

    Werner

    Tuesday, November 20, 2007 9:57 AM
  • Hi,

     

    Just to explain these terms....

     

    Interface is like a contract. Say, you are a Program Manger or senior developer in your organization. There are few developers inside your organization, who are sharing the code while developing. Many times, developers depends on each other's code.

     

    In this case, you'll tell your developers to write methods and using interface, you'll decide the method signtature. In this case, one developer will be knowing the method signature and start working on it and using it. While the other developer will implement that interface and write the body (his own logic)  for it.

     

    E.g. Microsoft has written a IDisposable interface. When this interface has been implemented, one developer simply calles the Dispose method, while one developer decides, how to dispose the object. And that too, happens parallely.

     

    Yes, you can write code, without using Interface or Abstract classes, in that case, it may take longer time to complete the project as one developer will need to wait till the first one writes the code. But this is just a probability.

     

    It totally depends on developers, whether to implement an Interface or write an abstract class. The difference is, Interface contains only method signatures, while abstract class may have some methods with body.

     

    I hope, this will solve the confusion...

    Tuesday, November 20, 2007 10:32 AM
  • Hi,

      thanks lot, I got some ided on Interface and abstract class
    Friday, November 23, 2007 6:56 AM
  •    1.If you are creating something that provides common functionality to

    unrelated classes, use an interface.

       2.If you are creating something for objects that are closely

    related in a hierarchy, use an abstract class.

    http://senthilvijayalakshmi.blogspot.in/2013/03/when-to-prefer-interfaces-or-abstract.html

    Tuesday, March 26, 2013 10:41 AM