Interface vs Abstract Class

Abstract classes
Abstract class is class which contain one or more abstract methods, which is implemented by sub classes. An abstract class can contain no abstract methods but also containe mehtod with body. Abstract classes are useful in a situation when some general methods should be implemented and specialization behavior should be implemented by subclasses.Abstract class can contain private as well as protected members.
When to use Abstract Class-If we have a base class where all the classes will perform the same function, then we can define that in our Abstract class. If you plan on updating this base class throughout the life of your program, it is best to allow that base class to be an abstract class. Because you can make a change to it and all of the inheriting classes will now have this new functionality.

Interface
Interface is extremely useful when you don’t want a big hierarchical type framework. As interfaces are implicitly abstract, they cannot be directly instantiated except when instantiated by a class which implements the said interface. The class must implement all of the methods described in the interface, or be an abstract class. An Interface can only have public members. A class implementing an interface must implement all of the methods defined in the interface, while a class extending an abstract class need not implement any of the methods defined in the abstract class.Maintainability–if you want to add a new feature (method) in its contract, then you must implement those method in all of the classes which implement that interface. However, in the case of an abstract class, the method can be simply implemented in the abstract class and the same can be called by its subclass.

When To use interface – Interfaces are useful when you do not want classes to inherit from unrelated classes just to get the required functionality.It is used where there of chances adding new method in future. Interfaces are more used to set standards. interface gave merely a specification,nothing implemented for any standalone project which can be changed at will its more design flexible and it can be utilized to model multiple inheritance.

Ref – http://www.interview-questions-java.com/java-questions/java-abstract-class-and-interface-interview-questions

kick it on DotNetKicks.com

Shout it

pimp it

Pin it

Vinay

I am Oracle ACE in Oracle ADF/Webcenter. Sr Java Consultant-working on Java/J2EE/Oracle ADF/Webcenter Portal/Webcenter content/Hibernate for Around 7 years. I'm an active member of hte OTN JDeveloper forum .Passionate about learning new technologies.I am here to share my knowledge. Give your views and suggestion on [email protected] .

More Posts - Website

Follow Me:
TwitterLinkedInGoogle PlusYouTube

  • Pingback: PimpThisBlog.com

  • Pingback: DotNetShoutout

  • Pingback: DotNetBurner - Architecture

  • Gonzalo

    The exact same explanation applies to .Net too.

    Good Article.

    • http://www.linkedin.com/in/ankitgoyal Ankit Goyal

      Exactly, explanation applies to all oops languages. I must appreciate the clear line which vinay has drawn between the interfaces and abstract classes. Good work

  • vinay kumar

    Hi iskorn,

    Well that is not misleading text.It depend on your perception.my understanding is that reader have some basic knowledge on interface and abstract class.Yes I am newbie and trying to share my knowledge becuase may be some of our friend who don’t have this much knowledge and who is more newbie then me.May be it can help them. NOM :-)

    Abstract classes are fast — This is perfectly true.Because Interface are slow in terms of speed. because it requires extra indirection to find the corresponding method in the actual class.

    This my meaning.please correct me if i am wrong.

  • vinay kumar

    Hi Gonzalo,

    Yes it is applicable to all OOPs lanauages.

    To HMM, — It is not misleading .my understanding is that reader have some basic knowledge on interface and abstract class.I try to give explation upto the point…….

  • iskorn

    Well, if we’re talking about JVM there is a difference between invokeinterface and invokevirtual commands. However from the performance point of view it is negligible (if exists) in a long run – see the test below. Please note the JVM performs optimization of such calls, so it is not a good idea to consider it (if you have more than basic knowledge of the subject).
    package test;

    interface Interface {
    void method();
    }

    abstract class AbstractClass {
    public abstract void method();
    }

    public class Test { // run with -server JVM option
    private static long count1 = 0, count2 = 0;
    private static final int TOTAL = 200000000;
    public static void main(String[] args) {
    Interface var1 = new Interface() {
    public void method() {
    count1++;
    }
    };
    AbstractClass var2 = new AbstractClass() {
    @Override
    public void method() {
    count2++;
    }

    };
    for(int i = 0; i < TOTAL; i++) { // allow JV< to optimize
    var1.method();
    }
    long start1 = System.nanoTime();
    for(int i = 0; i < TOTAL; i++) {
    var1.method();
    }
    System.out.println(System.nanoTime() – start1);

    for(int i = 0; i < TOTAL; i++) { // allow JVM to optimize
    var2.method();
    }
    long start2 = System.nanoTime();
    for(int i = 0; i < TOTAL; i++) {
    var2.method();
    }
    System.out.println(System.nanoTime() – start2);
    }

    }

  • http://rogervdkimmenade.blogspot.com Roger vd Kimmenade

    I agree with Urs Enzler, interfaces give you far more flexibility (for example by the use of the Dependency Injection pattern). Furthermore the performance is always a debate. Interfaces keeps the software manageable.

    Btw what has this topic to do with Service Oriented Architecture? Maybe include Abstract Class vs Interface vs Service Contract?

  • Pravin

    Abstract classes are not faster than interfaces. Only
    final methods could be faster depending on JVM implementation.

  • http://www.linkedin.com/in/ankitgoyal Ankit Goyal

    @sagar
    Can u give us the correct version of the statement u are saying as wrong??

  • Jetspeed

    Well i am surprised to see comment of Mr Sagar. Where does this article is mis-leading. I agree this article doesn’t contain all the information about abstract & interface, IMO it is not possible to cover every single aspect of any technology in single blog post, but It doesn’t mean it is contain some “superficial knowledge”.

    i think you should appreciate vinay’s effort in putting up all this.
    If you think information is “superficial knowledge” please post a link here. I think Vinay would be happy to update the post.

  • Jennifer

    Thanks for the nice article and I absolutely agree with Jetspeed’s statement. Those who think something is misleading shud prove their way of thinking else stop spamming.

  • Pingback: Interface vs Abstract Class | TechArtifact interface

  • http://www.jaykishanparikh.wordpress.com Jaykishan Parikh

    interface describes “What to do” but does not have anything like “How to do”. It is the responsibility of implementer to how to do.

    In early stage of your design, You are not clear with how to implement the things but you are very much sure about what to do. So interface should be used.

    In case you are some how clear with the implementation then use abstract class where you make concrete methods for which you know hoe to implement, and keep other methods as abstract to let derived class to implement them.

  • pavel

    The abstract classes and interfaces exist in OO languages and are semantically separated for a very good reason – software design. The question “Interface vs Abstract class?” is menanigless. Good software designer will not ask such questions as usage scenarios for interfaces are very different from those of the abstract classes and aren’t supposed to be interchangeable if we are to preserve the good design. It is not a coincidence the interface is named “inter” (between) + “face” (exposed) – it is the public face of whatever component/class implements it. The abstract classes on the other hand tend to hold specific implementation details and merely act as a common base for a given class hierarchy. Furthermore, it is not uncommon in the complex software systems, for abstract classes to implement interfaces by providing some common/shared implementation and allowing concrete classes to further deal with the specifics …

  • ajay

    wola…!
    after spending almost half hour on the net. i found this article. it clearly shows the difference between the both.

  • http://www.sadanand.com/contact.html artist studio ,gallery, paintings,Indian art, abstract, expressionistic,art

    Great items from you, man. I’ve be aware your stuff prior to and you’re just extremely excellent. I actually like what you have bought right here, really like what you’re stating and the best way through which you assert it. You’re making it enjoyable and you still care for to keep it smart. I can’t wait to read much more from you. That is really a tremendous website.

  • Geena

    Thanks so much for the post! I also liked the explanation given here about interfaces versus abstract classes:

    http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/

  • Rajnish

    Hi Vinay,

    I have gone through your previous article: JAR, WAR and EAR it was compiled fantastic :)
    About this article please elaborate why Interface takes much time in compared with abctrat class? Will wait for your reply :)

    Thanks :)