Java - difference b/n abstract and interface - Asked By Bhanuprakash T.R on 04-Feb-12 04:37 AM

Hello Sir,


   what is the difference b/n abstract and interface give an example(coding wise)  and then give example about real time. please help ..


Thanks & regards
Bhanuprakash T.R
dipa ahuja replied to Bhanuprakash T.R on 04-Feb-12 04:52 AM
(1)  An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface cannot implement methods.
 
(2)  An abstract class can contain fields, constructors, or destructors and implement properties. An interface cannot contain fields, constructors, or destructors and it has only the property's signature but no implementation.
 
(3)  An abstract class cannot support multiple inheritances, but an interface can support multiple inheritances. Thus a class may inherit several interfaces but only one abstract class.
 
(4)  A class implementing an interface has to implement all the methods of the interface, but the same is not required in the case of an abstract Class.
 
(5)  Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.
 
(6) Abstract classes are are faster than interfaces.
 
 
 
Suchit shah replied to Bhanuprakash T.R on 04-Feb-12 05:13 AM

Abstract Class

These classes are either partially implemented, or not at all implemented.

  • This class can contain declarations and also implementations.
  • We cannot make instance of Abstract class. We must inherit to use this class.
  • A non-abstract class derived from an Abstract class must include implementations for all inherited abstract methods.
  • Abstract class can contain abstract methods, abstract property as well as other members (just like normal class).

Interface

Interface is a pure abstract entity:

  • It contains only the declaration.
  • We cannot make instance of an Interface. We must inherit to use Interface.
  • A class which implements the interface should implement all the members of an Interface.
  • Members of an Interface are always public.

Few Advantages

An advantage of Abstract class over Interface

If you want to have additional functionalities for the interface, you must implement the new ones in all of the classes that implement the changed interface. The code will not work until you make the change. But if you want to have additional functions to the Abstract class, you can have default implementation. Your code still might work without many changes.

An advantage of Interface over Abstract class

A class can inherit multiple interfaces but can inherit only one class.

Example of Abstract Class

abstract class WashingMachine
{
   public WashingMachine()
   {
    // Code to initialize the class goes here.
   }

   abstract public void Wash();
   abstract public void Rinse(int loadSize);
   abstract public long Spin(int speed);
}

class MyWashingMachine : WashingMachine
{
   public MyWashingMachine()
   { 
    // Initialization code goes here.   
   }

   override public void Wash()
   {
    // Wash code goes here.
   }

   override public void Rinse(int loadSize)
   {
    // Rinse code goes here.
   }

   override public long Spin(int speed)
   {
    // Spin code goes here.
   }
}

Example of Interface

interface ISampleInterface
{
    void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
    // Explicit interface member implementation:
    void ISampleInterface.SampleMethod() { // Method implementation.
    }

    static void Main()
    {
    // Declare an interface instance.
    ISampleInterface obj = new ImplementationClass();

    // Call the member.
    obj.SampleMethod();
    }
}

Following are the differences between abstract and interfaces :-
√ Abstract classes can have concrete methods while interfaces have no methods implemented.
√ Interfaces do not come in inheriting chain, while abstract classes come in inheritance.
Hope it helps... and clear the understanding :)

Peter Bromberg replied to dipa ahuja on 04-Feb-12 08:01 AM
Abstract classes are not faster than interfaces. Internally, an interface method is just an abstract method definition.