Explain Difference Between Interface and abstract class

Posted on 2011-10-18
Last Modified: 2013-11-24
Explain Difference Between Interface and abstract class
Question by:stmani2005
    LVL 4

    Accepted Solution


    * Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior.
    * Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
    * Members of a Java interface are public by default. A Java abstract class can have the usual flavors of class members like private, protected, etc..
    * Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
    * An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
    * A Java class can implement multiple interfaces but it can extend only one abstract class.
    * Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
    * In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.


    LVL 47

    Expert Comment


    interface is a collection of methods which need to be implemented by any class which cliams to implement the interface

    abstract class is a class which has one or more methods which were not implemented and should be implemenetd in the
    non-abstract sub-class of this class

    so those are in fact very different  entities.

    interface does not include any iomplementation of any methods - only signatures
    which need to be followed by implmeneting classes

    abstract class can have implementation of all methods with the exception of one
    and abstrcat class can have instance variable and everything

    interfcae can have only method signtures and constants and nothing else

    LVL 47

    Expert Comment

    LVL 47

    Expert Comment

    Basically interface is much more abstract thing - it just specifies certain number of brehaviours
    taht the object should demonstrate, otherwise it does not specify much about the actual
    "meat" of the object

    Abstract class on the contrary will often specify almost everything about the object, but say one or two
    behaviors leaves open, so that some subclasses of this abstract class implement these behaviors in one way and other
    suvbclasses  - in another way

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now