Difference Between abstract Class and Interface

Programming & Compiler topics
Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Difference Between abstract Class and Interface

Post by Saman » Mon Feb 22, 2010 9:02 pm

Definition of abstract class
Also called an "abstract superclass," in object technology, it is a class created as a master structure. No objects of an abstract class are created, rather subclasses of the abstract class are defined with their own variations, and the subclasses are used to create the actual objects.

Definition of interface
A interface defines a set of methods but does not implement them. A class that implements the interface agrees to implement all of the methods defined in the interface, thereby agreeing to certain behaviour.


Probably Difference Between abstract Class and Interface is the most frequent question being asked in .Net world. This will explain the difference theoretically followed by code snippet.

Theoretically there are basically 5 differences between Abstract Class and Interface which are listed as below :-
  1. A class can implement any number of interfaces but a subclass can at most use only one abstract class.
  2. An abstract class can have non-abstract Methods(concrete methods) while in case of Interface all the methods has to be abstract.
  3. An abstract class can declare or use any variables while an interface is not allowed to do so.

    So following Code will not compile :-

    Code: Select all

    interface TestInterface 
    { 
        int x = 4;  // Filed Declaration in Interface 
    
        void getMethod(); 
        string getName(); 
    } 
    
    abstract class TestAbstractClass 
    { 
        int i = 4; 
        int k = 3; 
    
        public abstract void getClassName(); 
    }
    It will generate a compile time error as :-

    Error 1 Interfaces cannot contain fields.

    So we need to omit Field Declaration in order to compile the code properly.

    Code: Select all

    interface TestInterface
    {
        void getMethod();
        string getName();
    }  
    
    abstract class TestAbstractClass 
    { 
        int i = 4; 
        int k = 3; 
    
        public abstract void getClassName(); 
    }
    Above code compiles properly as no field declaration is there in Interface.
  4. An abstract class can have constructor declaration while an interface can not do so.

    So following code will not compile :-

    Code: Select all

    interface TestInterface 
    { 
        // Constructor Declaration 
        public TestInterface() 
        { 
        } 
    
        void getMethod(); 
        string getName(); 
    }
    
    abstract class TestAbstractClass 
    { 
        public TestAbstractClass() 
        { 
        } 
    
        int i = 4; 
        int k = 3; 
    
        public abstract void getClassName(); 
    } 
    Above code will generate a compile time error as :-

    Error 1 Interfaces cannot contain constructors

    So we need to omit constructor declaration from interface in order to compile our code .

    Following code compile s perfectly :-

    Code: Select all

    interface TestInterface 
    { 
        void getMethod(); 
        string getName(); 
    } 

    Code: Select all

    abstract class TestAbstractClass 
    { 
        public TestAbstractClass() 
        { 
        } 
    
        int i = 4; 
        int k = 3; 
    
        public abstract void getClassName(); 
    } 
  5. 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.

    Note here I am talking about the access specifiers of the member of interface and not about the interface.

    Following code will explain it better :-

    It is perfectly legal to give provide access specifier as Public (Remember only public is allowed)

    Code: Select all

    public interface TestInterface 
    { 
        void getMethod(); 
        string getName(); 
    } 
    Above code compiles perfectly.

    It is not allowed to give any access specifier to the members of the Interface.

    Code: Select all

    interface TestInterface 
    { 
        public void getMethod(); 
        public string getName(); 
    }
    Above code will generate a compile time error as :-

    Error 1 The modifier 'public' is not valid for this item.

    But the best way of declaring Interface will be to avoid access specifier on interface as well as members of interface.

    Code: Select all

    interface Test 
    { 
        void getMethod(); 
        string getName(); 
    }
Post Reply

Return to “Programming Languages & Compiler Theory”