Difference between Abstract Class and Interface:


Most of the people confuses with Abstract class and interface. Here I am planning to share some information with examples, I hope this will help you more………

Simple abstract class looks like this:

public abstract class KarateFight{

public void bowOpponent(){

//implementation for bowing which is common for every participant       }

public void takeStand(){

//implementation which is common for every participant

}

public abstract boolean fight(Opponent op);

//this is abstract because it differs from person to person

}

The basic interface looks like this:

public interface KarateFight{

public boolean fight(Opponent op);

public Integer timeOfFight(String person);

}

The differences between abstract class an interface as fallows:

1.  Abstract class has the constructor, but interface doesn’t.

2.  Abstract classes can have implementations for some of its members (Methods), but the interface can’t have implementation for any of its members.

3.  Abstract classes should have subclasses else that will be useless..

4. Interfaces must have implementations by other classes else that will be useless

5. Only an interface can extend another interface, but any class can extend an abstract class..

6.  All variable in interfaces are final by default

7. Interfaces provide a form of multiple inheritance. A class can extend only one other class.

8. Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc.

9.  A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class.

10. Interfaces are slow as it requires extra indirection to to find corresponding method in in the actual class. Abstract classes are fast.

11. Accessibility modifier(Public/Private/internal) is allowed for abstract class. Interface doesn’t allow accessibility modifier

12.  An abstract class may contain complete or incomplete methods. Interfaces can contain only the signature of a method but no body. Thus an abstract class can implement methods but an interface can not implement methods.

13.  An abstract class can contain fields, constructors, or destructors and implement properties. An interface can not contain fields, constructors, or destructors and it has only the property’s signature but no implementation.

14. Various access modifiers such as abstract, protected, internal, public, virtual, etc. are useful in abstract Classes but not in interfaces.

15.  Abstract scope is upto derived class.

16.  Interface scope is upto any level of its inheritance chain.

Other Useful Links:

What are the differences between object and instance?

What are the differences between EAR, JAR and WAR file?

Differences between callable statements, prepare statements, createstatements

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , , ,
Posted in JAVA
20 comments on “Difference between Abstract Class and Interface:
  1. gopi kanth says:

    hi,
    mallik

    you have given good differences between interface and abstract class.
    i want to add few more points to this post.

    when to use interface and when to use abstract class:
    interfaces:
    1)whenever your class going to get high level abstraction.
    2)where project design changes frequently from time to time.
    3)To provide less coupling.

    abstract class:
    1)Abstract class also will give high level abstraction
    2)Along with high level abstraction it will give DEFAULT behavior.

  2. mallikarjungunda says:

    Thanks For your comments gopi,

    Actually i am planning to post on “when to use interface and when to use abstract class” in upcoming posts.

    Thanks for your reply,

  3. anu says:

    nice and thanks…..

  4. satish says:

    best differences…..great work

  5. P. vivek says:

    usefull.. thanks … keep doing these activities to encourage students …

  6. prakash says:

    It makes clear……. tan q…….

  7. Subhan says:

    Thank u Malli

  8. AnilReddy says:

    can you give me more practical examples regarding this topic(diff bw abstract class and interface)…..

  9. Ramakrishna. M says:

    Clear and Nice. Thank you Mallik.

  10. kishore says:

    class X extends A implements B
    {
    public void m1()
    {
    }
    }
    abstract class A
    {
    public void m1();
    }
    interface B
    {
    public void m1();
    }

    it is compiled but I have a doute which Method is overriding above class X either Abstract class A method m1() nor interface B method m1().
    thnak you

  11. Harikrishna says:

    Hi Kishore,
    When a class is deriving from a class and interface, if both have a method with same prototype we need to implement only once, this is the basic rule of inheritance.

    In this example we can say
    class X is overriding the method of class A and also
    class X is implementing the method of interface B

  12. […] Difference between Abstract Class and Interface […]

  13. Gowtham R says:

    In 11th point they have mentioned interface doesn’t allow modifiers, but it can be used.

  14. […] Difference between Abstract Class and Interface: […]

  15. Anurag Singh says:

    All the differences, you explained really useful . this is very- very important topic in core java interview.
    thanks for this great post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
June 2010
M T W T F S S
« May   Jul »
 123456
78910111213
14151617181920
21222324252627
282930  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 265,565 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100