Interface vs Abstract Class

By Vinay | August 4, 2009 | 8,712 views
Category .NET, C++, Common, Java, open source


About author  I am Oracle/Java professional.Working on J2EE technologies and i.e Oracle ADF,Java,J2ee,PL/sql,Top Link,Apps for 2+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion. http://www.linkedin.com/in/vinaykumar2 Read more from this author


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.Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class.

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

  • Share/Save/Bookmark
Read more post on Abstract Interface Java Oracle ADF when to use abstract class when to use interface 

19 comments | Add One

Comments

  1. GonzaloNo Gravatar - 08/4/2009 at 6:28 am

    The exact same explanation applies to .Net too.

    Good Article.



  2. Ankit GoyalNo Gravatar - 08/4/2009 at 6:29 am

    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



  3. HmmNo Gravatar - 08/4/2009 at 10:14 am

    Weak. Some of this is very misleading. Talking about abstract classes without discussing inheritance and concrete classes as base classes only paints half a picture.



  4. vinay kumarNo Gravatar - 08/5/2009 at 12:21 am

    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.



  5. vinay kumarNo Gravatar - 08/5/2009 at 12:25 am

    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…….



  6. iskornNo Gravatar - 08/5/2009 at 1:41 am

    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);
    }

    }



  7. Jon Arild TørresdalNo Gravatar - 08/5/2009 at 4:11 am

    Using words like fast and slow in this context is misleading. The difference between the two in actual time measurements is so small that I’m not even sure its measurable. I’ve participated in creating huge enterprise applications with a lot of interfaces and abstract classes, and for performance there is no impact. For performance issues your focus should be on other aspects of your code and not on this.



  8. Urs EnzlerNo Gravatar - 08/5/2009 at 7:24 am

    In my opinion the titel is already misleading.
    There is no Interface VERSUS Abstract Classes.

    If you have a class with a dependency then this dependency should always be represented with an interface.

    The real class behind the interface may use an abstract class as its base because there is some default behaviour that the abstract class can implement.

    This is the way to build flexible, loosely coupled designs.

    Okay there are exceptions for simple data containers of course.

    Just my thoughts though.



  9. Roger vd KimmenadeNo Gravatar - 08/10/2009 at 12:42 pm

    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?



  10. PravinNo Gravatar - 08/14/2009 at 7:57 pm

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



  11. sagarNo Gravatar - 08/17/2009 at 3:08 am

    The article is misleading.

    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.

    The above extract from the article is completely incorrect.

    This article only demonstrates superficial knowledge.

    Interface is contrat what does that mean this tutorial doesn’t talk about that



  12. Ankit GoyalNo Gravatar - 08/17/2009 at 4:09 am

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



  13. vinay kumarNo Gravatar - 08/17/2009 at 4:27 am

    Is it????
    is it wrong.So please let us know the correct extract.

    And about the inteface is contract ..if i did’nt tak about that it doesn’t mean that article is misleading.I make it upto the point.May be u can add that point , if i forgot to mention.



  14. JetspeedNo Gravatar - 08/17/2009 at 4:48 am

    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.



  15. JenniferNo Gravatar - 08/21/2009 at 1:19 am

    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.



Trackbacks

  1. PimpThisBlog.com
  2. DotNetShoutout
  3. DotNetBurner - Architecture
  4. Interface vs Abstract Class | TechArtifact interface

Leave a Comment

Name:

E-Mail :

Website :

Comments :