Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between interface and abstraction?

user-image
Question added by Viswanath telagamsetty , trainee , niit
Date Posted: 2013/10/31
Bowsil Ameen
by Bowsil Ameen , Sharepoint Development officer / architect , Etihad Airways

What is an Abstract Class?

 

 

An abstract class is a special kind of class that cannot be instantiated. So the question is why we need a class that cannot be instantiated? An abstract class is only to be sub-classed (inherited from). In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.

 

 What is an Interface

 

 

 

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritance.

 

 

 

When we create an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

 

 

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

 

 

Abstract Class:- 1) An abstract method is created by specifying the abstract type modifier. 2) An abstract method contains no body. 3) An abstract method is not implemented by the base class. 4) An abstract method is automatically virtual. 5) A derived class must override it. 6) Abstract class can have modifiers for methods,properties etc., 7) An abstract class can implement a property. 8) The abstract modifier cannot be applied to static methods. 9) Properties can also be abstract. 10) A class containing abstract methods must be declared as abstract with the abstract specifier. 11) There can be no objects of an abstract class. 12) If a derived class doesn't implement all of the abstract methods in the base class, then the derived class must also be specified as abstract. 13) An abstract class can inherit from a class and one or more interfaces. 14) An abstract class can implement code with non-Abstract methods. 15) Abstract class can have constant and fields. 16) An abstract class can have constructors or destructor's. 17) An abstract class cannot be inherited from by structures. 18) An abstract class cannot support multiple inheritance. 19) If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. Interface:- 1) Interfaces cannot be instantiated directly. 2) Interfaces can contain events, method, indexer and properties. 3) An Interface can contain property definitions. 4) Interfaces contain no implementation of methods. 5) Classes and Structs can implement more than one interface. 6) An Interface can be inherited from by structures. 7) An interface itself can inherit from multiple interfaces (Interface can support multiple inheritance). 8) An abstract class can implement a property. 9) If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method.

(IN JAVA)

Interface contains only public, final and static  variables like (public final static int a; ) and public abstract methods like (public abstract m1(); ) by default.

Abstract method means, no definition is there. For this Interface we can't create object, and this should be Implemented  by next class and abstract method definition should be given in Implementing class. To access it we have to createobject for implementing class and made a refernce to interface to use it.  see it in example.

But we can access Interface without creating object of implementing class. How means with the help of annonymous class. (SEE BELOW EXAMPLES)

 

EX:-   with creating a object for implemting class

Interface X

{

             public abstract void m1();        

}

 

class A implements X

{

            void m1()

            {

                               System.out.println("this is m1() which is in interface");

             }

}

class Main

{

         public static void main(String []args)

           {

                   A a= new A();

                   interface i;

                   i=a;

                   i.m1();

              }

}

 

Ex:- with annonymous class

Interface X

{

             void m1();        

}

class Main

{

             X x = new X()

            {

                         @override

                          public void m1()

                          {

                                   System.out.println("this is m1() through annonymous class");

                          };

           }

}

 

Abstarct Class is like user defined class but in this variable declaration & method declaration (abstract method) and method definition are present. 

For this is also we can't create object. The Abstract class sholud be extended in next class. To access abstract class you have to create object for extending class through which you have to acess.

We access with annonymous class with creating object for extending class (similar to interface annonymous class usage (see example of interface annonymous class)).

 

Ex:- 

 

abstract class A

{

              int age;

              abstract void m1();

               void m2()

               {

                       System.out.println("This is m2() in abstract class");

                }

}

class B extends A

{

           void m1()

           {

                    System.out.println("This is m1() extending in class B");

            }

            void m3()

             {

                        System.out.println("This is m3() present in B class");

              }

}

class Main

{

              public void static main(String args[])

              {

                            B b= new B();

                             b.m1();

                             b.m2();

                             b.m3();

               }

}

 

Ajay Prakash Garg
by Ajay Prakash Garg , Senior Software Engineer , kpit Cummins Infosystem Limited

Interface -

  • Only declaration of methods, properties, events and delegates.
  • A class can implement multiple interfaces.

Abstract Class -

  • Abstract methods should be declarative only whereas instance methods can have implementation.
  • A subclass can inherit only one parent class only.

Bhagat Singh Nittu
by Bhagat Singh Nittu , Programmer , Imagination learning solution

Interface take less memory space or neglegible to store different section of your  code 

But in case of abstract class they need more memory spaces for methods of base class and also for Object class which is by default extended by all classes in java

Zubair Ali
by Zubair Ali , Software Developer III , S&P Global

  • Interface is a contract which basically used to assure specific number of steps/methods/procedures must be taken inside your class(es). For that reason you create contracts (Interfaces) to make sure all the procedures are taken otherwise compiler will remind you to override missing one. I.e For Payment you need following methods1) GetCardInfo2) AuthenticateCardValidity3) GetGatewayInfo4) WithdrawAmount5) ResponseStatus. 
  • Abstract Classes are just as normal classes with a difference that they can have abstract methods inside to which you can override in your class and perform class specific job. (they are not must to be overridden but overrideable).  The class which contains even a single abstract method becomes abstract class. Whereas it can contain non abstract methods, properties, events or variables inside. 

Interface can be said as purely Abstract class. 

Rashid Anwar
by Rashid Anwar , senior php developer , sparx it solution pvt ltd.

in addition to the basic diffinations as mentioned here in many answers,  the main and the technical difference is that an abstract class is used when two or more objects have IS_A relationship among them whereas interface is used when two or more non-related objects have to implement some common functionalities.....

INTERFACE:

                          interface is the collection of only abstract methods.

ABSRTACT CLASS:

                         abstract class means it should not have defination. only declaration present in this absrtact class.

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.