Difference Between Interface and Abstract Class

  1. 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.
  2. Variables declared in a Java interface is by default final. An  abstract class may contain non-final variables.
  3. 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..
  4. Java interface should be implemented using keyword “implements”; A Java abstract class should be extended using keyword “extends”.
  5. An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.
  6. A Java class can implement multiple interfaces but it can extend only one abstract class.
  7. Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.
  8. In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.

This Abstract and Interface tutorial was added on 23/04/2008.

«

»

Comments on “Difference Between Interface and Abstract Class”

  1. mjt

    5. An interface can extend another Java interface only [...]

    It sounds like what you’re saying is that an interface can
    ONLY extend ONE other interface?

    • Ansumalini Jena

      Java interface inherits one or more than one interface using “extends” keyword.so we called java supports multiple inheritance using interface.

      • Govardhan

        Interface can not extend multiple interfaces it implements multiple intefaces
        see the difference carefully.

        • shaffic

          Hello Govardhan,

          you are wrong.. interface can extend many interfaces.. while using classes alone interfaces should implement.. dont misguide people.

          thanks.

          • sundar

            I verified. What Shaffic said was correct.

          • chinnapa reddy

            abstrat class are in java but why interduced interfacess.

          • Anonymous

            Dear u r also know that interface is also implement not extend so how can u say that, plz read carefully and than comment ok.. thanx.

          • abhi

            please, i m beginner sheffic send me the program

          • abhi

            hello shaffic

            this is abhi from kolhapur,
            sir i m beginner in java please send me the example, program of how the interface extends the multiple interfaces.
            regards
            Abhi

        • Rajkumar Chaudhary

          Mr.Goverdhan,
          An Interface can not implements another Interface It’s implements by class.An interface always extends another interface like a class extends another class But a class can extends only a single class and interface can extends multiple interfaces at a time.

          • Venkat

            I Verified in my project what i working on currently. What Rajkumar Chaudhary saying is absolutely correct.

            public interface InterfaceC extends OnterfaceA, InterfaceB{
            }

        • Ambro

          Please Remove Govindan’s comment. That wasted my 30 minutes.

        • Anonymous

          how it is possible can u tell me

        • harshad

          there are four main senerio to understand the concept of interface or inheritance..!!

          1) interface a
          {
          void f();
          }
          class abc implements a
          {
          public void n()
          {
          system.out.println(“hey”);
          }
          public void f()
          {
          System.out.println(“hello”);
          }
          }

          2) interface a
          {
          void f();
          }
          interface b
          {
          void n();
          }
          class abc implements a,b
          {
          public void f()
          {
          system.out.println(“hello”);
          }
          public void n()
          {
          system.out.println(“hey”);
          }
          }

          3) interface a
          {
          void f();
          }
          class abc
          {
          void n()
          {
          System.out.println(“hey”);
          }
          class aaa extends abc implements a
          {
          public void f()
          {
          system.out.println(“hello”);
          }
          }
          }

          4)
          interface a
          {
          void f();
          }
          interface b extends a
          {
          void n();
          }
          class abc implements b
          {
          public void f()
          {
          system.out.println(“hello”);
          }
          public void n()
          {
          system.out.println(“hey”);
          }
          }

      • Panchanan mohapatra

        hi Ansumalini Jena,
        An interface can only implements single or multiple interfaces.It can’t be extend.

        • Panchanan mohapatra

          sorry. An interface can extend single or multiple interfaces. Interface can’t be implements another interface.

        • Alok

          Hi Panchanan,

          Sorry, but please refresh your concept. Interfaces always use “extends” keyword to implement interface(s) and not “implements”.

          Thanks

          • sahithi

            in interface must use implements becoz its an abstract.
            if use extends it shows error at run time

    • Anonymous

      An interface can extend anoter interface
      but a class can implements any no of interfaces
      a java class can extend abstract class
      a java class can extend abstract class and implemnts any no of interfaces

    • Prakash Dahagam

      can you tell me the situations when we should go for interface and abstract class….?

      • salahuddin ansari

        strictly when you want the method to common and use more as required then interface else
        abstract

    • hasnen

      YES…INTERFACE CAN EXTENDS THAN ONLY ONE INTERFACE

      LIKE….

      INTERFACE A
      {…..
      }
      INTERFACE B EXTENDS A
      {…..
      }

  2. Joe

    No, an interface can extend multiple interfaces.

    Probably I should have phrased difference 5 as, “an interface can extend other interfaces and cannot extend an abstract or concrete class. Also, an interface cannot implement (but extend) other interfaces”.

    Thank mjt for pointing out.

    • Ramireddy

      Yes,An Interface can extend one or more interfaces but it cant implement any interface

      • Is it possible

        public void abstract class Employee
        {
        private String name;
        private String address;
        private int number;
        public void Employee(String name, String address, int number)
        {
        System.out.println(“Constructing an Employee”);
        this.name = name;
        this.address = address;
        this.number = number;
        }

        • Shashant

          yes it possible.

        • Anonymous

          yes why not!!!!!!!

          • Sailendra Narayan Jena

            Hi Moni,

            Can you please explain how a Java class Constructor will return. if we are putting any return type before constructor then that is not called as constructor that is called as user defined method. But you have mentioned that “Constructor is a special kind of method inside a java class with the name as class name. It does return anything not even ‘void’.”

            Here you have mentioned that It does return anything not even void. Can you please explain me this line what does this means. But i know if we are creating a constructor inside a class then rules is it should be class name and any access specifier but it doesn’t contain any return type. Like this Examples:

            class A
            {
            public/private/protected A()
            {
            System.out.println(“HI”);
            }
            }

            Here class a contains one constructor which don’t have any return type.

            class B
            {
            public void B()
            {
            System.out.println(“HI”);
            }
            }

            Here class B having two things one is default constructor provided by the compiler and another thing is that its contain one user defined method whose name is same as class name but not a constructor.

            So please explain your line.

            Thanks & Regards
            Sailendra Jena

          • Mohammad Umar Ali Karimi

            I hope I’m not beating the dead horse here with my comments.
            Constructors are meant for initialization. That means, you can initialize your instance variables including final ones inside constructors. Constructors don’t return anything. For example:

            class Employee {
            private String name;
            private final Date hiringDate;

            public Employee(String name) {
            this.name = name;
            Calendar c = Calendar.getInstance();
            hiringDate = c.getTime();
            }
            }

            In the above example, I’m simply initializing the name with the param name and final variable hiringDate with the current date. Also, your final variables are assigned the values when the class constructor is called or class is initialized in the JVM. So a class constructor’s job is to initialize instance variables with their default or assigned values. They don’t return anything rather they reside in the JVM for as long as the object is alive.

        • Anonymous

          No it’s wrong….

        • saikrishna

          yeas we can write

        • mat

          “public void abstract class Employee”… what do you mean by void in class declearation

        • pradeep

          how a constructor can have return type?

          otherwise this is perfectly fine even abstract classes can have constructors. so its correct.

        • Anurag Kumar

          a class as well as a constructor can not have return type,even if not void

          • satheesh

            you are right for C++. In java, if you specify void, it will consider it as a normal method instead of constructor.

        • swetha

          no its not

        • Anonymous

          No,I think it won’t work as an abstract class cannot be instantiated..how this constructor can get executed…

          • Anonymous

            It is valid. Here “public void Employee(…)” is a method which is having the class name but its not the constructor.

          • Manoj

            Abstract class can have constructor,we use it for varible initialization and also for constructor chaining.

        • Anonymous

          it is not possible, b’caz class returning void,

          • Anonymous

            constructor dose not have any return type,
            we can not create a object of a abstract class so how constructor will get called?

          • Bhushan

            constructor dose not have any return type,
            we can not create a object of a abstract class so how constructor will get called?

          • Bhushan sak

            constructor dose not have any return type,
            we can not create a object of a abstract class so how constructor will get called?

        • Pradeep Kumar Verma

          it is not possible because any class does not have any return type…

        • Ravi

          It wont work becoze its class and constructor have return type…which is not a valid,i think so…

        • Naushad

          The code is wrong because class cannot have return type void. If it is typed by mistake, than the code is perfect. If you write a method with the same name as class with return type it does not qualify as construtor. The compiler will concider it as a normal method. So the code is perfect. It can used by subclassing it and passing the arguments in method Employee.
          Hope it answers the query.

          • aman

            1. class acn not be void.. its not allowed
            2. you can use a method with same as class name
            hence public void employee is ok
            3. you cannot create object of class employee as it is abstract class but calling private memer by this pointer is allowed …

            so finally just remove void from class declaration
            nd it will work ..
            just don’t create object and don’t try to invoke method :-)

        • Anonymous

          no it’s not possible.

        • @naim

          yes!.. y nt. object class is der.. so it is true

        • Rushik Sutariya

          ya friend this is possible.
          we can use of “this” keyword here.

        • Anonymous

          no because not hav return type of the class

        • Anonymous

          public void Employee(String name, String address, int number)
          The abovecode is not constructor because it is returning void while constructor never return type..

          Anil Kesarwani

        • Parag

          There should be at least one abstract method in the class

        • Smruti

          The class declaration itself is incorrect.

          It should not be “void key word”

    • Anonymous

      extends means one abstract class or one simple class

    • hi
      this not possible,because constructor not come return type and than abstract class there also no return type so this code is error

  3. zelalem

    thanks for the abstract and interface difference ..
    please send this complete book for me

  4. Balraj kairamkonda

    if interface object’s initialization with corresponding class instantiation examples would have presented.

  5. Unknown

    Instead of picking points from Java books about the differences, it would be better if you describe the main advantages of using interfaces and abstract class, when to use interface and abstract classes. That would add value to your question and make easy understand to the viewer.

  6. Anonymous

    Excellent tutorial on abstract and interface difference
    One more point – If I dont want multiple inheritence then which one is better to use?? Interface or abstract class??

    • chinju

      interface is better if you don’t want multiple inheritance because Because if you add new method headings to an interface, then all of the classes that already implement that interface will have to be changed to implement the new methods.

      • Manoj

        I think in this situation abstract class is better option.Becaause it is easy to add new functionality in abstract class and modify it in the child classes.but in case of interface u r bound to implement the methods in the class though ur classes dont need them.

  7. Joe

    @unknown – I was just trying to reproduce the theory in a simple way as in my style.

    But, apart from this list of difference, I will write a separate article on when to use interface and when to use abstract class

  8. Arun Pratap Singh

    The difference no 8.
    “In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.”

    What is this “extra indirection”. I have seen same difference point in other sites without clear information.

    Also how can we prove that java interfaces are slow than abstract classes.

  9. rakesh

    to know more about the difference between interface and abstract class ….read SCJP kathy sierra’s book….best as far as i know…

  10. abubakar

    good i learn very much from this webiste

  11. rajesh

    thanks for giving explanation and difference between these two and i need more details about core java

  12. Sean, melbourne photographer

    Hi,
    some nice tips on this site!
    I also don’t get difference 8 though – can you clarify please?

  13. raghu

    how to utilize interface in another class without implementing that interface

    • Anonymous

      You have to define the interface from where you need the interface implementation ForEx. To write a thread program need to implement Runnable or to extend Thread .The other way is as follows .

      Runnable r = new Runnable(){

      public void run()
      {
      //Do you thread code here
      }
      };
      new Thread(r).start();

  14. Argha Basu

    It’s good, lists the complete abstract and interface difference in simple way

  15. maxmetallica

    Hey Raghu,you can do that by making the implementing class abstract and then extend it in another class of your choice .

  16. Manish

    apart from difference between them,
    use Interfaces when you require 100% abstraction otherwise use abstract class

  17. Manas

    Why interface cannot be instantiated?

    • Ankit Kumar

      we can not create instace of an interface because it hava incomplete methods. so can not provides sufficent memory by the jvm.

  18. why a default constructor is provided by jre in java

  19. monika bansal

    thanx for the complete abstract and interface difference thing… its good, it helps me alot

  20. Hema

    Your website looks so good..

  21. subbu

    Hi,

    We can also discuss like an interface will be used for an object reference of its implementing class while abstract class object reference can be used for its extending class.

  22. mohan

    your explanation is very good
    please send me the whole book

  23. Joe

    There is no book. But I have an idea of coming up with something in the future.

  24. interface is the most powerfull solution of using multiple inheritance because java does not support multiple inheritance

    Thank you

  25. rajesh

    good explanation but
    explain new topics on java

  26. Thank you…very much……..
    please…..
    send me one copy of this book.

  27. niyaz

    ya i accepted this concepts and more useful for beginers……..

  28. shafi

    Hi.
    can u give me the difference between the two following statements

    1.A sub1=new B();
    2.B sub2=new B();

    where A is suerclass
    B is the sub class

    //method over riding
    class A
    {
    int i,j;
    A(int a,int b)
    {
    i=a;
    j=b;
    }
    void show()
    {
    System.out.println(“super”);
    System.out.println(i);
    System.out.println(j);
    }
    }

    class B extends A
    {
    int k;
    B(int a,int b ,int c)
    {
    super(a,b);

    k=c;
    }
    void show()
    {

    System.out.println(“sub”);
    System.out.println(i);
    System.out.println(j);
    System.out.println(k);
    }
    }
    class Override{
    public static void main(String args[])
    {
    A sub1=new B(1,2,3);
    B sub2=new B(1,2,3);
    sub1.show();
    sub2.show();

    }
    }

    • Pradeep Kumar Verma

      1)it call the constructor of class B by passing the arguments(1,2,3) and it assign the reference variable of class A type
      2)it call the constructor of class B

  29. venkat

    first you have to concentrate on Polymorphism concept , why because if you are crating the object and the object reference is give to super class , then on that time it checks whether that method is there in subclass or not at compile time

  30. Chandrashekhar

    plz write a default construcctor in side class A then above code compile and run successfully………..

  31. Chandrashekhar

    example is nothing but a polymorphism………

  32. sindu

    thanks it is very useful.

  33. ramakrishna

    thanks for clarrification

  34. Remya

    When to use abstract class over Interface ?

    Thanks in Advance
    Remya

  35. Aryan Nardiganj

    its a great help for understanding main difference between both abstract class as well as interface.
    to know more about the difference between interface and abstract class ….

  36. manish

    plz mail me the difference in abstract and final keywords in java

  37. vivek kumar

    what is difference between overloading and overriding?

  38. RaamNaresh Reddy

    Wow… it is too good Blog..

    it is too Elegant..,
    Conceputally Nice…,

    I like the Way u explain about “Difference between Abstract class and Interface”

    What my suggeation is…, Write one Example Program For Every Explanation..

  39. Thadeuse

    thanks for ur explanation

  40. Thadeuse

    is it possible
    abstract class A extend b implements c
    {
    }

  41. trupti

    thanks for explain it

  42. Uma Karmakar

    thanks for the basic help in java

  43. Hari chowdary

    How many maximum number of Interfaces are implements in our class?

  44. Rahul Jaiswal

    Hi….. It is too good.

  45. meena

    Hi Joe,
    This is very useful.Being a fresher its very useful for me. Mainly the website design is really fantastic.Keep going……….

  46. sam

    why abstract class does not support for the multiple inheritance?

  47. Asha.BM

    Please mail me this book.

  48. Lokanath

    Thanking U for providing a comprehensive information. may allmight with u to have ur destiny.

  49. rajitha

    hai
    very nice and good.

  50. Zeeshi

    Thanks it was a little helpful for me….

  51. Zeeshi

    Difference B/W overRiding and overLoading:

    in over riding perameters and type of method remain same. but its internal properties changed you can say over ridden.

    in over loading the name of method is same but parameters are different. according to type or in number. the call to these functions depends on the parameters compatibility…

  52. dis infermation is very well and easy to understanding…..i am feel good………

  53. guest

    the main difference is interface doesnot talks about implementation.it talks about requirement specification
    abstract class:
    it talks about implementation but not completely(partial implementation).

  54. Hi readers,
    i go through all above mentioned discussion regarding interfaces and abstract classes. So i feel some person need example where to use interfaces . In order to understand the interface concept just thinking that how jdbc api worked with each and every dbms(oracle,mysql etc) because jdbc api provide just interfaces and implementations of these interfaces are provided by the dbms vendor that implementations are known as database drivers. (9888327755-ssubhi_2003@yahoo.com). Feel free to discuss any java topic

  55. thanks

    dis infermation is very well and easy to understanding…..i am feel good………

  56. Mansi Singhal

    well written

  57. Kavan Vakhariya

    Easy and point wise description help me to understand the exact difference.

  58. Anonymous

    Im ery helpful to all, this discussion has provide me clear knowledge.
    yes and i most lke info provided by satnam singh, you know we were using that but never came in mind that how basic java get used over there. great….

  59. Guest

    nice artiacle and very helpful also…Can anybody explain why java interfaces are slow and how it requires extra indirection??

    Thanks in advance.

  60. Madhuri Gorde

    All the above mentioned pts. are totally easy to undertand.

  61. ganesh

    hii can any one help me to know how interface is slower than abstract abstract class.
    in the above article what indirection means?

  62. arxlan ahmed

    Really nice points to understand thanx

  63. Swetha N

    Could you please give some program examples, about their diffrences

  64. krishna

    hi i through all above ur’s feedback pls can u people send me what is difference between class interface and abstract class
    send me j-krishna@live.com

  65. kasthuri

    how multiple interface is used instead of multiple inheritance??

  66. before studying this information i am not clear about interface and abstract class.just now i am know about this topic.thank you

  67. Yashas

    Neat & Clear!!! Information provided can be understood by even a average student.

  68. Anonymous

    i need a difference between a class and a interface?

  69. Very well written article

  70. All of the variables in an interface are implicitly static and final.

  71. hai guys, how to use in spring with struts?

  72. Deepak

    thanxxxxxxxxxxxxxxxx

  73. wtd

    all i saw was jibber man.. please for heavens sake , use examples…!

  74. Sayyed Imam

    Mr. Satnam Singh explained very well…..so nice

  75. Jeetu

    thats good, i understand the difference between interface and abstract class.
    but can you give a code which show the difference ……

  76. Anonymous

    Easy to understand

  77. srikanth venigalla

    its very helpful to me thanqs for your kindly info…….

  78. srikanth venigalla

    its very help to me .thanqs fro your kindly info…

  79. mohit

    nice difference between interface and abstract class

  80. i have a doubt, if we implement an interface need to write all the methods in that interface even if we don’t need the methods .what is the case of abstract class?

  81. Joe

    Thank you all for your great comments!

  82. T.PRIYDARSHINI

    plz send me the complete tutorials so that it will helps me a lot

  83. Pankaj

    Thank you so much for giving the best Answer…

    because of i am so confused
    Thank you again.

  84. T.PANDEESWARI

    abstract class and java class both are same or different???

  85. Selva

    HI Joseph,

    Let me know that why we are choosing “Interface” & “Abstract class” for a good application..

    Thanks in advance,
    Selva

  86. neeraj tanwar

    hiiiiiiiiiii joseph,

    what is we commoly use in java project that is absruct class or interface?

    my second question is that what the actuall meaning of implimentation and extand?

  87. ruchi

    my first ques is 1)6 points of difference between object and class.
    2)6 points of difference between dynamic binding and message passing.

  88. ruchi

    ques) explain briefly why cant we use a keyword as a variable name???

  89. ruchi

    tell me true or false…
    1. when if statement are nested,the last else gets associated with the nearest if without an else.
    2. one if can have more than one else clause.
    3. a switch statement can always be replaced by a series of if…else statements.
    4. a switch expression can be of any type.
    5. a program stops its execution when a break statement is encountered.

  90. very very good explanation on the differences

  91. VipinJeet Tripathi

    This is quiet good….
    It may be useful for interview…

  92. sushil

    very accurate answers.good.

  93. harshil

    very nice answers good

  94. soma

    This is really good….
    It is useful for interview

  95. Rajan E

    Its so easy to learn even if the people doesn’t know java

  96. Samantha

    This is really helpful!!

  97. Shitansu

    Hi Joe,

    Can you please tell me when to use Abstract class and when to use Interface in an application?

  98. vinod

    give reallife example of interface…….

  99. Anonymous

    hey i didnt get my answers,,,,,,,,,,,,

  100. khushboo

    thank u sir

    plz mail me similarities between an interface and classes

  101. In interfaces we cannot have any implementation for methods but that’s not the case with abstract classes we can have implementation for methods.

  102. mani

    thank you for giving informations
    but i want more details …..

  103. ramesh

    nice explanation for interface and abstract class

  104. sampath

    about java buzz words ?

  105. vinoth

    It is very use full for me….. Thank you… by V.Vinoth from poondi

  106. Ahmad

    Thank you very much … it was very useful to me

  107. sai

    thank u for giving this wondarful information

  108. Praveen

    Hi Joseph sir,

    Thanks for the detailed explanation of diff between abs class and interface. I would like to know , on what basis programmer deside he need to use interface or abstract class ? and if interface is having 2 abstract methods(only) and abstract class is having 2 abstract methods(only) then what was the difference in both ? Please post answers for the questions.
    Thanks and Regards,
    Praveen G

  109. Anudeep

    Very helpful…

  110. karabi das.

    plz give me the 6 points of difference between messege passing and message binding……..

  111. srividhya

    i still have a doubt, when we have to use interface and when we have to use abstract(class as well as method)

  112. ana

    plz suggest me how can i become expert in making programs by myself?? because coding postion in java is a biiiiigggg giant for me can u suggest how can it be easier for me the coding in java

  113. swati

    nice web site

  114. sabrina

    its really helpfull for my class lacture

  115. mayank jain

    what is the main reason to create abstract class?…..
    sir i know we cant create object of abstract class and we also provide method body in abstract class compare to interface…

  116. Anonymous

    good and simple

  117. Anonymous

    difference b/w instance variable and class variable.

  118. jangbahadur patel

    what are the changes to a variable ,method and a class when a final keyword is added to them.

  119. jayashree

    I was put forth this question in the interview.Please help.

    When you have to pass the data in the form of keys which collection would you use ?

  120. Thirupathi

    thank you for information about abstact class and interface

  121. ram kumar

    Apart from these differences one more difference is in my mind, i want to ask is it correct or not-

    * interface can be declare as a member of a class while abstract class is’t.

  122. vivek bhatt

    some interface define just constants and no methods……thanks 4 giving such simple and good differences….can u send me whole book on my given id….plz…..

  123. Mahesh Reddy

    can u send me the real time example that when do we use interfaces and when do we use abstract class

  124. samson

    Its very easy and useful to me

  125. Muruga G

    One more point:

    1. If we created Interface we can add new methods easily, but abstract is not easily.
    2. Interface act as Service layer its interact different types of two entities(node) but abstract is not.
    3. Interface is high level of abstraction(fully abstract class) but abstract is next level of abstraction.

    I have added some point. Please verify and let me know if it is correct or not?
    Please forgive any typical and grammatical mistake.

    Please send me if any feedback: murugadoit@gmail.com

  126. Abstract class start with letter “A” and Interface starts with letter “I”.

  127. Shaleen

    What is the purpose of using Abstract class????

  128. Jayaram

    Hi,please provide the Real-time examples for Interface and the Abstract class.So that every one can understand easily..

  129. Balachandiran

    Hi Joe ,
    when we have to use abstract class & interfaces can you explain

  130. mallika.oleti@gmail.com

    If i create any reference variable to an interface and also i create the object of implementing the class.just mail me the difference

  131. shiv

    Hi Joe,

    First of all its a nice blog. Congrats for writing and maintaining this blog.

    On Dec 19th you have mentioned that “an interface can extend multiple interfaces”.
    But java doesnt support multiple inhertance. Please explain your statement.

    With Regards
    Shiv

  132. maagi

    thanks joe for detailed explanation

  133. Rajesh

    Hi All,

    I am new to java. Can you please explain what are the topics covered in J2EE? if i want to serve in Java field, what are all i need to learn..

    Please help me.
    -Thanks in advance

  134. Rajesh

    Hi All,

    I am new to java. Can you please explain what are the topics covered in J2EE? if i want to serve in Java field, what are all i need to learn. and also please explain the ways

    Please help me.
    -Thanks in advance

  135. Sandesh

    what if abstract class implements two interfaces then it has to provide bodies for method declarations in interfaces or not?

  136. Anonymous

    when we have to use abstract class & when we have to use interfaces can you explain

  137. bharath

    Thank you,that the difference between them is clearly understood to me.Please explain in simple programatic way?

    when we have to use abstract class & when we have to use interfaces can you explain?

  138. vishnugandhi

    Thank you for clearing doubts…..please send this book to my mail id…i have some more doubts

  139. Venkatesh Bestha

    This differences can be found everywhere, say new things like
    1. when to use Interface and when to use Abstract class
    2. which one is efficient ?

    Just give some practical programming scenarios.

  140. Anonymous

    hello nyc blog

  141. keith

    hiiii gud source of info……keep goin!!

  142. Anonymous

    catch
    {quotient=divide(num1,num2);
    cout<<"The quotient is "<<quotient<<endl;
    }
    try(char *exceptionString)
    {
    cout<<exceptionString:
    }
    wat is the error in this code……..

  143. rajesh

    Thank you sir it is really helpfull to me keep going on………..

  144. Pranav Thipse

    Thanks a lot !!!!!!!!!!!!
    you are The Best.
    Thanks for writing and maintaining this nice blog. this is very nice blog.

    pranav thipse.

  145. Sujay

    Hi Sir, really good blog.Very much impressed by the blog and the information displayed.It would be nice if you can start covering advanced topics also in this blog.

  146. Kiran

    Hi joe,
    I know about Interfaces & Abstract classes. But can you please let me know why there are 2 different things Interface & Abstract class. When only Interface was enough(I guess, to do all the work.)

  147. pritz

    i stil hav a lot of doubts.can u jst giv some example and also show whn to use wht?

  148. Anonymous

    it was really nice and in a very simple language… thanks joe

  149. saravanan

    can any1 give me A simple program for managing abstract and interface which includes all aspects as joe said?

  150. neha

    can any body explain me constructor in detail…i don’t no the use of it pls. explain me..

  151. siddu

    Thanks joe really helpful,But I’m having confusion about the term “extra indirection” used in 8th difference can u please explain what it is ?

  152. indian freak

    I have a doubt Joe,

    In my current coding, my aim is to call the same method in different classes simultaneously. say class1 and class2 access add()method present in class3. My issue is a null pointer is getting displayed on execution. Can u guide me on this???

  153. yogesh

    can you please explain how the servlet communication done to other? explain different ways ?

  154. Anonymous

    Thanks you for giving to this this answer.

  155. pazuzu

    Some simple examples would be really great :)

  156. Anonymous

    this is awesome. thanx

  157. Anuj

    Please any one explain ,where we use Abstract calss and where we use interface .
    Plese give example

  158. Rajeev Kumar

    is an interface implicitly an abstract class?

    Thanks…

  159. Anonymous

    any book to get a good knowledge of java in simple and easy text?

  160. hi thanks for differences of abstract and interface .please send the complet book to my mail id are the address in url pattern (rakesh.gandua9@gmail.com)

  161. Anonymous

    hiii,,,,thanku for u r description plzzz send the java book which can be helpful in the basics point of view to my email id (kalyaniapple93@gmail.com)

  162. navi

    In the 7th difference pls give an example that how can we invoke abstract class if main exist…

  163. anjani

    pls send the best book for core java to my mail id i.e. anjaniatmakuri@yahoo.com

  164. Matheen

    Nice work again…but i don’t understand 7th difference,can u explain me in detail.

  165. Arjun

    Is it possible to implement Iterator interface
    Methods in our own class?
    for example

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package Collection;

    /**
    *
    * @author arjun gm
    */
    public interface java.util.Iterator {
    public abstract boolean hasNext();
    public abstract E next();
    public abstract void remove();
    }
    public class MyInno implements Iterator {
    boolean hasNext()
    {
    System.out.println(“Add”);
    return true;
    }
    void remove()
    {
    System.out.println(“Sub”);
    }
    public static void main (String args[])
    {
    MyInno my=new MyInno();
    my.remove();
    }
    }

  166. Musaddiquie Husain

    why variables declared in interface are final by default and why all members are public by default?

    when and where interfaces and abstract class is to be use?

    Thanx…..

  167. Alps

    nice explanation .Can u tell me how can we instantiate Abstract class

    • satish

      by extends that abstract class by concrete class means public class classname extends Abstract class.
      sub class that extends super abstract class
      we create object to sub class that is also called super class object becoz super class behaviours and methods all came to sub class so it is also super class object

  168. Bhaskar

    No, you cannot instantiate an abstract class. An abstract class is a class that is not yet “finished” – it contains methods without an implementation. A non-abstract subclass is supposed to implement those methods

    You can create anonymous subclasses that implement all abstract methods:

    AbstractAction action = new AbstractAction(“Hello”)
    {
    public void actionPerformed(ActionEvent e)
    {
    // this is the only abstract method in AbstractAction
    }
    };

    These anonymous subclasses only support the constructors of the abstract class though, you can’t create your own ones. If you need that kind of behaviour you need to create actual subclasses

  169. Java Learner

    Joe, Can you please re-design the site to help the user to reply to a specific comment, rather than commenting in end.

    It would help people in following better, to the question/tips left by fellow contributors

  170. Joe

    @Java Learner, yes I need to make that change so that we can reply to a particular comment. Sure I will do it as soon as possible.

  171. Swami

    theoritically good .try to show it with examples….

  172. prakash

    Thanks a lot !!!!!!!!!!!!
    is very useful for my exam preperation
    Thanks again

  173. appu

    Thank u very much…… this explanation is short pls put more anyway thanks guyz

  174. Reshma

    such a nice example for prepare an interview

  175. Aricent

    Hi all,
    I came to know that most of you are clear with the differences. Can anyone of you explain this point,“In comparison with java abstract classes, java interfaces are slow as it requires extra indirection.” Even i read in several sites interfaces require extra indirection and hence slow. why is it and what is indirection,

  176. shiva

    nice one but give one example

  177. javabean

    when we use abstract class? and when we use interface? pls reply

  178. Anonymous

    Can you please explain with an example for both abstract and interface? It is very useful and easily understand

  179. Anonymous

    thanks man u are great!!

  180. Anonymous

    perfect….dude….

  181. Manoj Singh

    when do we use interface and abstract class ?? what is the necessity of using both of them differently if they are similar to each other ?

  182. karthik

    where should we use interface and Abstract?

  183. Koray

    are you alive

  184. Anonymous

    grt site.

  185. kiran

    thanks for yr dis ans………

  186. ravi

    Please let me know how the abstract class is faster than interface

  187. Very helpful site. Will share it on facebook :)

  188. Bimal Modak

    Interface Requires more time to find the actual method in the corresponding classes.
    this is why Abstract class implementation is faster than interface

  189. abstract class does not have a body

  190. chirag pandey

    sometimes a problem occurs where we have to only define methods in abstract class,but using only methods in a class is against an Object-oriented concept.that is the reason we use interfaces..because interface is not a class so we can easily put methods in an interface.

  191. pawan kumar

    why we use interface or abstract class and what is the best approch interface or abstract class

  192. amit

    This is really useful

  193. jasmy jose

    Thanx a lot

  194. Srinivasulu

    plz send me some java course materilas, i am learning initial stage and i need not take any training, i have learn java my own thats why please send me some course meterial that materials useful my starting stage.

    plz avoid authors books

  195. Sanjay Patel

    Is it possible to implement Iterator interface
    Methods in our own class?
    for example

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package Collection;

    /**
    *
    * @author arjun gm
    */
    public interface java.util.Iterator {
    public abstract boolean hasNext();
    public abstract E next();
    public abstract void remove();
    }
    public class MyInno implements Iterator {
    boolean hasNext()
    {
    System.out.println(“Add”);
    return true;
    }
    void remove()
    {
    System.out.println(“Sub”);
    }
    public static void main (String args[])
    {
    MyInno my=new MyInno();
    my.remove();
    }
    }

  196. Sakthivel

    Thank u for useful information

  197. Mayank

    Can we extends the interface ? if yes then show me by one example if no then tell me a proper reason ?

  198. aashraye rai

    thanks

  199. Susagar

    Thank you for giving the nice info….

  200. its a useful tutorial

  201. its a useful tutorial

  202. Anonymous

    thanks for giving valuble information…….. friends please send some more java material to me my e-mail is:govind.588@gmail.com

  203. Thank you for providing us these answers

  204. Anonymous

    if any interface is enxtend the same abstact method with in the abstract class then what will happen……. how? please send me one example////

  205. Francis

    What is the significance of Interface?

    Its just a declaration of method names. But the implementation is still in the class.

    Any one can answer?

  206. Ajay Patil

    joe .plz explain
    how transient keyword works with small example and what do u mean by serialization

  207. anu

    hi joe
    can i know exact usage of both interface and abstract class?

  208. Anonymous

    hai joe sir,
    Iam a begginer in java,before reading this blog i had lot of confusions in my mind now I got clear idea about interface and abstraction.Thank you so much for that……can you plz include some examples(programm).

    Biji A

  209. josh

    Nice blog:)

  210. sid

    hi Guys ……

    My Question : Can Over riding supports Inner classes in java?

    package com;
    public class Test
    {
    public static void main( String[] args )
    {
    A b = new B();
    System.out.println( “Calling b.test(): ” + b.test() );
    B b1 = new B();
    System.out.println( “Calling b.test(): ” + b1.test() );
    }

    private static class A
    {
    public static String test() { return “A”; }
    }

    private static class B extends A
    {
    public static String test() { return “B”; }
    }
    }

    if you need any help in java … contact me …

    siddardhas@yahoo.co.in

  211. avdhesh samele

    examples are good for me……*

  212. Munna

    nice blog..

  213. Anonymous

    not bad

  214. Bala

    pls include some examples

  215. Anonymous

    thank u…

  216. Akshit

    Thanx for difference …
    it is help me lot in tomorrow exam of java ….

  217. SANKARGANESH L

    thanks…It is very useful….

  218. imran

    thanks a lot it is helpful for my exam

  219. Nagarjuna Reddy.M

    Could you Please Explain Difference Between Interface and abstract class with real time example?

  220. malli

    i think 5 point i have doubt

  221. Srikanth K

    malli,
    “i think 5 point i have doubt”
    coming to your Qestion

    Class A extends Class B(abstract class)
    {
    // Class can extend only ONE Class
    }

    Class A implements Interface A, Interface B, Interface C {
    //Where as Class can implement Multiple Interfaces
    }

    Hope you are clear now

  222. pramod

    hi, sir

    i have recently complete my MCA and Join a company as java programmer, but i dont have knowledge of java, and i dont have much time to join coaching, so is there a way to learn java pls suggest me some sophisticated way to learn java programs…..

  223. sudhir

    Good initiation

  224. Farhan Munir

    Very useful and very clear. Thanks.

  225. Vijay

    1 more diff=
    An interface cannot have a constructor whereas an abstract class can have a constructor which is called indirectly by the derived class.

  226. ram

    its very useful for all.
    thanks

  227. Anonymous

    hmmm have to improve

  228. hmmm have to improve

  229. Anonymous

    its usefull to me… thanks..

  230. Rawinder dhillon

    thx

  231. Ramaseshan

    Proud to see a chennai guy coming up with lot of info..

    Generally only Americans do this…

    Thanks a lot…

    Ur website is very interesting and attractive….

    If u cannot share ur pone number pls call me at 9543111591.
    I wanted to ask u some doubts..

    Ramaseshan , Chennai

  232. akanksha mathur

    sir can u plz explain the difference between an abstract class and an interface through a small program… i mean can u plz give me a code which can only be implemented by using an interface not by using an abstract class…???

  233. Mallikarjuna

    when to use abstract and when to use interface would you explain with an ex. sir

  234. Mayur Kumar

    Hi sir,

    i like your blof content very much. but i would like if you would give some practival scenario laong with the points. likw e.g whne Interface is good and bad. same for teh ABSTRACT classes. which one to choose when. specially we are open to both.

    Thanks in advance..

    | Mayur Kumar

  235. Anonymous

    Can you please explain about when to go for interface and abstract class?

  236. Shailendra kumar shail

    Hi Joi,
    I have my own doubt if you can clarify.

    When we compile a class or interface java create .class file.

    For example if we create Test.java and it contains
    public class Test{….}

    in other file if we create
    public interface Test{….}

    on compilation both file will be created as .class then how can we know that whether we need to extends or implements if we don’t have any IDE.

    Thanks.

  237. Anonymous

    These r text book differences write some real time orientation sir…………

  238. Rakesh

    great job…. :)

  239. Understand clearly but,i need when to use interface and when to use abstract?
    with program?

  240. Java_Gun

    JOE dear,
    In Diiference # 3. You have added that the Abstract Members can be Private Flavour..is that possible to have Private Method in Abstract Class…???????

    If your Answer is Yes Please Post executable Code…

    • selvakumar

      Hi,
      We can have private method as we can call the private method inside another non abstract method.:)

  241. Priyanka

    Why we have only final variables declared in an interface?

  242. N.rajlaxmi

    I want miniproject title in core java..please suggest me.

  243. Javaid

    @priyanka

    Answer to you question

    All variable in interfaces are final by default

  244. shyam

    could you please explanin me the exception handling concepts

  245. udhaya

    Quiet Good… Interesting.

  246. Anonymous

    Its said like interface can have private members and default is public. But, I don think its true. Can anyone please confirm this??? Can interface have private members??

  247. Anonymous

    Sorry for the wrong information. It was a misunderstanding. Kindly ignore!!!

  248. Raman K

    very useful to me thanks

  249. muthu

    yes its usefull thaks …

  250. Anonymous

    really good …. thank you…..

  251. thanks for this explanetion
    thanks so much

  252. sanjeev

    what is return type of constructor in a java

  253. roshan

    constructor can’t have any constructor….

  254. Rajeesh.N.Santhu

    @Sanjeev

    Constructor is not a method, so it cannot have a return type, but can have arguments and access modifiers.

  255. nnn

    in point 5 the statement is there interface can extends another interfaces but interfaces are used using implements keyword

  256. nnn

    can unterface can be use through extends keyword because extebds is use for only classes

  257. Anonymous

    Nice to Learn.

    Thanks,
    Udhayakumar G K
    http://saijava.blogspot.in/

  258. Anonymous

    it is gd

  259. Anonymous

    you are telling differences
    but not telling about the correct features like where do we use abstract and where do we use interfaces in real time scenarios?

  260. please clear with the example programs for abstract class and interface

  261. Tony

    Hi Joe

    One of my sincere request can u give a real time scenario where to use abstract class and where to use interface.

  262. pritha

    very useful to me…
    Thanks a lot………….

  263. Bhushan

    When to use Abstract and when to use Interface ?

  264. Shahjahan

    Hi joe

    A Java abstract class can have instance methods that implements a default behavior.

    I Can’t understand what that means please clarify using some example.

  265. This explanation is good but, can you explain when to use interface over abstract class and when to use abstract class over interface ..

  266. Vishaka

    Hi Joe,
    – One more difference between Interface and Abstract class is that:

    Abstract class contain Constructor Where as Interface does not contain Constructor.

    If any mistake is there then PLZ let me knw.

  267. nagajothi

    what is the difference between Interfaces and abstract? please give the real time example ?

  268. nagajothi

    when to use interface and when to use abstract class? please send the clear example sir?

  269. mano

    why is abstract class is needed?everybody says for extending a common functionality throghout the application we define an abstract class with one concrete method which holds the common logic and extend this abstact class.we can define a concrete class and subclass it..right?.what is the real need to define an abstract class here?

  270. Anonymous

    you are the best joe………

  271. Anonymous

    wow….it is too good

  272. M Abbas

    Nice discussion

  273. NEERAJ KUMAR SINGH

    nice

  274. droni

    hello ,this is a good information to know about interface and abstract class.pls send me a copy of these java papers.

  275. srinivas battineni

    when we should go for interface and when we should go for abstract class?

  276. OVERRIDDING-when you extend a class and write a method in the derived class which is exactly similar to the one present in the base class, it is termed as overriding.
    EX-
    public class BaseClass{

    public void methodToOverride()

    {

    //Some code here

    }

    }

    public class DerivedClass extends BaseClass{

    public void methodToOverride()

    {

    //Some new code here

    }

    }

    OVERLOADING-More than one method, in the same class shares a same name but different arguement is called overloading..
    EX-
    public class OverLoadingExample{

    public void add(int i, int j)

    {

    int k = i + j;

    }

    public void add(String s, String t)

    {

    int k = Integer.parseInt(s) + Integer.parseInt(t);

    }

    }

    You wanna learn html visit http://htmlatul.host56.com

  277. RKG

    Good explanation.

  278. sai

    Thank you , more helpful

  279. prathamesh

    it’s really valuable……….
    thanks to you for explanation………

  280. Respected Sir,

    I want talk to you.I have more doubts please clear to me. your explanation is okay but u dont mention realtime and practical information

  281. abc

    Informative site….

  282. P.Vijayakumar

    Sir,i have one doubt with abstract class..
    Why we need abstract classes in java??..

  283. Anonymous

    how do we pass values for concrete methods from main function in abstract class

  284. Kumar

    Very nice and simple explanation. if can you have information what is precise difference
    why interface is slow,why to use interface or abstract. What is need of two interface and abstract concepts in Java.

  285. Anonymous

    7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated,????????????

    HttpServlet is an abstract class;

    new HttpServlet(){};

    Servlet is an interface;

    new Socket(){
    implements all methods;
    };

  286. sou

    this is nice i want some more explanation with example for about interface and abstract class…….

  287. Venike ViswanathReddy

    this is nice sir but which one is pure abstract either interface nor abstract class

  288. Dharmendra

    in which condition abstract class failure but interfaces are used.

  289. Hari Krishna

    i want one answer related on interface?
    when one class can be implements an interface and as same class extends one another abstract class of this interface.?
    that is possible ?

  290. Hari Krishna

    //this is possible?plz give me ans.
    interface r1{
    void display();
    void add(int i,int j);
    int ram(int k);
    void ganesh(char c);
    }

    abstract class ram implements r1{

    ram() {
    System.out.println(“constructor is called”);
    }

    public abstract void ganesh(char c);

    public int ram(int k){
    return k;
    }

    public void display()
    {
    System.out.println(“i am here”);
    }

    public void add(int i,int j)
    {
    System.out.println(“i and j value is”+i +” “+j);
    }

    void display(int k)
    {
    System.out.println(“the value is”+k);
    }
    }

    class a implements r1 extends ram
    {
    public void ganesh() {
    System.out.println(“this is”);
    }
    }

    class i {
    public static void main(String args[])
    {
    a var=new a();
    a.display( );
    a.add(5,6);
    //var.display(5);
    System.out.println(a.ram(4));
    }
    }

  291. ashis

    differences between collection and collections in detail please explain this am not able to understand

  292. Anonymous

    Hi

    I want to know when can i prefer Abstract Class or Interface.

    ie When can i choose Abstract Class or Interface.

    Please explain somebody, because i have little bit confusion on that

    Kindly please explain some body effectivly…

  293. Manibharathi

    //Interface
    public interface interfaces {
    void multiply();
    }

    //AbstractClass
    public abstract class AbstractClass {
    abstract void add();
    void display(){
    System.out.println(“hello world”);
    }
    }
    //SuperClass
    public class superClass extends AbstractClass implements interfaces{

    @Override
    void add() {
    int a=10;int b=10;int c;
    c=a+b;
    System.out.println(“Addition=”+c);
    }
    void sub(){
    int a=10;int b=10;int c;
    c=a-b;
    System.out.println(“Subraction=”+c);
    }
    @Override
    //method implementation
    public void multiply() {
    int a=10;int b=10;int c;
    c=a*b;
    System.out.println(“Multiply=”+c);

    }

    }

    //SubClass

    public class subClass extends superClass {
    public static void main(String[] args) {
    subClass s=new subClass();
    s.add();
    // code reusability
    interfaces inter=new superClass();
    inter.multiply();
    }
    }

  294. Manibharathi

    differences between collection and collections in detail please explain this am not able to understand

    Answer:
    Collection is an Interface it will extends Iterable Interface.
    eg..methods(add,delete,remove…)
    Collections is a Class it will extends a
    Object class.
    eg..List,Set,Map,SortedMap,Comparator,Comparable…

  295. Manibharathi

    I want to know..In MultiThreading if I am using HashMap means is there any chance to occur DeadLocks?

  296. Manibharathi

    What is meant by ThreadPool?

  297. Manibharathi

    what is the use of abstract class? give some examples?

  298. Manibharathi

    which is the best way to increase the application perfomance?(Multithreading Application)

    1.Extending Thread.
    2.Implementing Runnable Interface.

  299. bharat patel

    why it is said that java provides more security than any other language?

  300. bharat patel

    why it is said that java provides more security than any other language?

  301. Rohit vasavda

    Thank

  302. swati gupta

    very well explained.

    Thanks

  303. Akhilesh

    Hi Joe,

    Thanks for the detailed articles…your site is very good for beginners as well as for experienced java programmers…

    About the interfaces and abstract class one thing always come to my mind that why do we need an abstract class? As if I write an abstract class with all abstract methods then there is no difference between interface and abstract class so why to have it?
    If given a chance to write an abstract class or an interface which one to choose?

  304. Jyothylakshmy.P.U

    Thank you..Outstanding discription.

  305. Jyothylakshmy.P.U

    Thanks.Outstanding discription.

  306. raju

    using System;

    namespace AbstractsANDInterfaces
    {
    ///

    /// Summary description for Employee.

    ///

    public abstract class Employee
    {
    //we can have fields and properties

    //in the Abstract class

    protected String id;
    protected String lname;
    protected String fname;

    //properties

    public abstract String ID
    {
    get;
    set;
    }

    public abstract String FirstName
    {
    get;
    set;
    }

    public abstract String LastName
    {
    get;
    set;
    }
    //completed methods

    public String Update()
    {
    return “Employee ” + id + ” ” +
    lname + ” ” + fname +
    ” updated”;
    }
    //completed methods

    public String Add()
    {
    return “Employee ” + id + ” ” +
    lname + ” ” + fname +
    ” added”;
    }
    //completed methods

    public String Delete()
    {
    return “Employee ” + id + ” ” +
    lname + ” ” + fname +
    ” deleted”;
    }
    //completed methods

    public String Search()
    {
    return “Employee ” + id + ” ” +
    lname + ” ” + fname +
    ” found”;
    }

    //abstract method that is different

    //from Fulltime and Contractor

    //therefore i keep it uncompleted and

    //let each implementation

    //complete it the way they calculate the wage.

    public abstract String CalculateWage();

    }
    }

  307. raju

    Feature

    Interface

    Abstract class

    Multiple inheritance

    A class may inherit several interfaces.

    A class may inherit only one abstract class.

    Default implementation

    An interface cannot provide any code, just the signature.

    An abstract class can provide complete, default code and/or just the details that have to be overridden.
    Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties

    Core VS Peripheral

    Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

    An abstract class defines the core identity of a class and there it is used for objects of the same type.

    Homogeneity

    If various implementations only share method signatures then it is better to use Interfaces.

    If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.

    Speed

    Requires more time to find the actual method in the corresponding classes.

    Fast

    Adding functionality (Versioning)

    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.

    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.
    Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined

  308. khan

    When to use Abstract and when to use Interface ?

  309. khan

    When to use Abstract and when to use Interface ?

  310. Revan

    Hi,

    Thankx for posting this article, but ,
    instead of posting what is there in books, you explain the main difference with an example. That may use for beginners who are reading this article.

  311. suresh

    Dear friend,
    could you please send your core java preparation kit.

  312. sound great…..!

    Dear joe, you can also add particular program based on any topics for the
    demonstration ,so we can understand this better way.
    and u r site structure is inovatic.

  313. Puja Kumari

    Is it possible that an abstract class have no any abstract method?

  314. risit j patel

    thank you….
    its easy to understand…..

  315. risit j patel

    thnx ….
    its easy to understand…..

  316. suresh

    hai im newly learning java i have question. i.e is in below

    In Java, under what circumstances would you use abstract classes instead of interfaces? When you declare a method as abstract, can other nonabstract methods access it? In general, could you explain what abstract classes are and when you might use them?

  317. suresh

    Those are all excellent questions: the kind that everyone should ask as they begin to dig deeper into the Java language and object-oriented programming in general

    Yes, other nonabstract methods can access a method that you declare as abstract.

    But first, let’s look at when to use normal class definitions and when to use interfaces. Then I’ll tackle abstract classes.
    Class vs. interface

    Some say you should define all classes in terms of interfaces, but I think recommendation seems a bit extreme. I use interfaces when I see that something in my design will change frequently.

    For example, the Strategy pattern lets you swap new algorithms and processes into your program without altering the objects that use them. A media player might know how to play CDs, MP3s, and wav files. Of course, you don’t want to hardcode those playback algorithms into the player; that will make it difficult to add a new format like AVI. Furthermore, your code will be littered with useless case statements. And to add insult to injury, you will need to update those case statements each time you add a new algorithm. All in all, this is not a very object-oriented way to program.

    With the Strategy pattern, you can simply encapsulate the algorithm behind an object. If you do that, you can provide new media plug-ins at any time. Let’s call the plug-in class MediaStrategy. That object would have one method: playStream(Stream s). So to add a new algorithm, we simply extend our algorithm class. Now, when the program encounters the new media type, it simply delegates the playing of the stream to our media strategy. Of course, you’ll need some plumbing to properly instantiate the algorithm strategies you will need.

    This is an excellent place to use an interface. We’ve used the Strategy pattern, which clearly indicates a place in the design that will change. Thus, you should define the strategy as an interface. You should generally favor interfaces over inheritance when you want an object to have a certain type; in this case, MediaStrategy. Relying on inheritance for type identity is dangerous; it locks you into a particular inheritance hierarchy. Java doesn’t allow multiple inheritance, so you can’t extend something that gives you a useful implementation or more type identity.
    Interface vs. abstract class

    Choosing interfaces and abstract classes is not an either/or proposition. If you need to change your design, make it an interface. However, you may have abstract classes that provide some default behavior. Abstract classes are excellent candidates inside of application frameworks.

    Abstract classes let you define some behaviors; they force your subclasses to provide others. For example, if you have an application framework, an abstract class may provide default services such as event and message handling. Those services allow your application to plug in to your application framework. However, there is some application-specific functionality that only your application can perform. Such functionality might include startup and shutdown tasks, which are often application-dependent. So instead of trying to define that behavior itself, the abstract base class can declare abstract shutdown and startup methods. The base class knows that it needs those methods, but an abstract class lets your class admit that it doesn’t know how to perform those actions; it only knows that it must initiate the actions. When it is time to start up, the abstract class can call the startup method. When the base class calls this method, Java calls the method defined by the child class.

  318. priyanka

    page is developed very nicely… i liked it…

  319. please send whole book it is very nice

  320. Madhan

    Very nice. Thanks

  321. latha

    please send d complete buk for me

  322. Jayapal

    Hi All,

    Interface is also method It was implemented in class.But abstract class contain abstaract methods(I mean concrete methos)

    multiple interface is implemented with interface.But with abstract class its not possible.

    Interface contains all public static variable and methods.But in abstract class static variables,static blocks,concrete methoods

    defination of abstract class:
    class A{
    int sum(){
    }
    }
    abstract class B extends A{
    sum(){
    }

    }

    B is called abstract class because it should not override all the methods and parameters in super class A so it is called
    abstract class.

  323. Jayapal

    sorry multiple inheritance implementer with interface but with abstract class its not possible

  324. Venkat

    The explanation which is very transparant.

  325. Sajjan

    Pls Send me A Complete boook For java…

  326. pratap kumar rout

    plz explain with exampl.

  327. sandeep

    Joe u wrote that an interface can extend multiple interfaces but java does not support multiple inheritance so how this is possible ? please explain .

  328. Suncrack Oreocookie Gupta

    Hi, this is Harold Kumar Oreocookie Gupta, of the Gupta family in India.

    Can you write me example of the aforementioned article? Ideally with examples of the main concepts and differences given by your beloved article? I’ll be waiting for your examples and I’ll check back tomorrow afternoon precisely at 15:00.

    • Joe

      Harold,

      I don’t get you! You want an example for an interface and an abstract class, is it? Those are straight forward!

  329. Pawan Ydav

    why does java have both abstract class as well as interface? what is the need or importance of having both?

    • ashish

      Interface has incomplete implementation so that a user can do it himself as needed,it also forces the implementing class to write for the methodsw in interface,so that becomes a standard.While in case of abstract class the user can override the methods of superclass or can use the implementation written in that superclass …

  330. Sivakumar

    Hi Joe,

    Thanks a lot for your article posted. Could you please provide me an example program for interface and abstract classes better can understand?
    Thanks in advance for your reply.

    Regards,
    Sivakumar.

    • vivek

      java introduced interfaces for solving multiple inheritence (which it donot support) A class(particular class) can be a subclass of only one super class, but an interface can extend as many other interfaces as you want. Just list them after extends, separated by commas.

      public interface interfaceName extends superinterfaceName,superinterfaceName1,superinterfaceName2
      {
      interface body…
      }

      and coming to abstract class is a class which may or may not contain abstract methods but if does the child class which inherits it must do implementation for abstract methods
      abstract class A()
      {
      public void add();
      public void bark();
      }
      class B implements A
      {
      public int add()
      {
      //some code for addition;
      }
      public int bark()
      {
      //some code for bark;
      }

      }

      }
      note:class cannot implement an interface but it can extends the interface

      • saj

        ‘java introduced interfaces for solving multiple inheritence (which it donot support)’

        Believe Java guys had a reason for not supporting multiple inheritance by not allowing extending from multiple classes. What could be the reason for it. And if multiple inheritance was not desired, y did they allow implementation of multiple interfaces?

  331. Satheesh.C

    what is real time use of interface and abstract class..can u explain Joe?..I am waiting for your valuable reply

    thanks in advance

  332. vivek

    ur content is good but i think u must change background color caz its difficult see content wid hat brown back ground
    u can feel that tooo by watching content in blog and our comments bakground

  333. selvakumar

    We can add below,
    1,We can restrict the Method access outside of the class.If class A has 5 methods and Client A1 wants only 3 methods and Client B1 wants 2 methods then we can implement corresponding interfaces 1 has 3 methods and another has 2 methods and same will be used in client.
    2.We can generate Dynamic proxies using interfaces whereas its not possible in abstract class.

  334. Gayathri Chitnhala

    Which one is better in Inheritance and Interface?
    Please give me example also.

  335. Bhavesh

    1)we can compile and execute abstract class But Interface class only Compile not Execute …

    2)abstract class can extends only one class But Interface can extends Multiple interfaces
    Ex. interface C extends A,B
    But in Abstract class B extends A only..

  336. Mani

    When do use interface and abstract class in the following case when both looks the same?

    public interface TestInterface {

    void method1();
    void method2();
    }

    public abstract TestAbstract {
    void method1();
    void method2();
    }

    Please advise..

  337. Anonymous

    pls send some java material because i applied for ocjp

  338. Anonymous

    dude an abstract class can’t impalement Interface…………..i think u r wrong!!!!

  339. seshu

    nice,easy understandable

  340. vaibhav

    vary nice difference is easy understandable

  341. Ranjith

    can u give the real time example for the abstract class and interfaces

  342. Rajeswari

    Thanks, I have one doubt
    In one of the interview they asked like this
    any other use is there for Interfaces? other than Multiple Inheritance?

  343. Mit

    how we can implement security on web-site, which ve developed in java.

  344. suresh

    why java using class ,abstract class and interface

  345. suresh

    why we are using string args[] in main method header

  346. Ashish

    Please tell me different scenario for both interface and abstract class when can we use ?

  347. Raghu

    Sir,

    in what situation we have to use abstract class or interface .interfaces has only method declaration what is the use of it?

  348. amitesh

    some point is not true

  349. rajkumar

    An abstract class allows you to provide a default implementation for some of the methods but an interface does not allow you to provide any implementations.

    You can implement multiple interfaces but you can only inherit from one abstract class.

  350. Kiran

    when do we create interface instead of creating abstract class??

  351. jeevani

    Why we need to use interface?

  352. JItendra Trivedia

    Hey that’s a nice tutorial…..But i just want to know what is the real use of the interface except Multiple Inheritance……………Please I’m confused

  353. Manjinder singh

    According to me,,:

    An interface consists of abstract methods only and if we implement any interface into our concrete class we have to give the body of all the abstract functions of the interface,, but if i make a toString method in my concrete class which implementing an interface, the toString method is working efficiently, whereas there is no abstract method in the interface…..

    And if the toString method is coming from the object class then it must be coming with some other methods also, then why we haven’t given the body of all other functions in our concrete child class…

    Please correct me and guide me for this, its a humble request joe sir…

    thanking you in advance…

  354. This difference is syntactically which is not good
    You have to give difference by real time use .
    Suppose i make all method as abstract in abstract class and this method are also in interface then abstract and interface become same so why java found interface? Any body have Answer of my Question then please send me mail

    • Manjinder singh

      kamlesh , i think interfaces helps in achieving multiple inheritance, thus we need them too.

      hope this satisfy your question a lil bit.

  355. Arvind

    Hi Joe,

    I am big fan of your blog. several times, I got a big help from this for searching answers tuff FAQs of interviews…………

    I want to add one thing that this question is SUPER-HOT and EVERGREEN in any java interview……..but as per your another answers of another question on your blog ………….this answer is just childish……..So please post your response in a professional manner to understand the need and use of both the concepts into the root level.

    I want to understand properly about Abstract Classes and Interfaces that………..at which situation we generally use Abstract classes??????? and in which specific situation, we use Interfaces………?????? Would U please give explanatory response for this with real-time examples…………

  356. Noorul

    Hi,
    Can u write code for this point
    7.Interface is absolutely abstract and cannot be instantiated; A Java abstract class also cannot be instantiated, but can be invoked if a main() exists.

    I tried but no result yet..

  357. Anonymous

    back ground is too worst please change the back ground as early as possible we could not find the words and letters.
    with regards,
    venkatesh.j

  358. jignesh

    thank you for this diffrence

  359. Rajesh

    hai sir, i hava one question.

    There is a list of countries.
    and each country has some number of products.

    To store these which collection class should I take.. and how would I store them.

    please reply with answer.

    thankyou sir.

    • Manish

      Go for HashMap, Country Name is your key and list of product your ArrayList.

      For Example,

      if Country India having
      list.add(“Prod1″)
      list.add(“Prod2″)
      list.add(“Prod3″)

      then,

      map.put(“India”, list);

    • Anonymous

      Its better to the apache commons collection class MultiValueMap there this can be done very relaible

  360. Gyanendra Singh

    Abstract class: some
    method
    implementations, some
    methods abstract
    (method definition
    only)

  361. Sathishkumar

    Change the font color or change the background. I feel it not readable.

  362. rakesh

    please relist your topics sequentially to make this tutorial even more valuable. And make the missing topic available. great tutorial by the way.

  363. Manish

    Thanks ! Well written article :)

  364. Aasif Abrar Khan

    i like 7 difference bcoz it contain logic,u show good difference.

    Thanks

  365. saravana guru

    in which situation we have to use interface and abstract class

  366. saravana guru

    which situation we have to use interface and abstract class

  367. Avinash

    Hi Joe, You have given a bookish points. Why don’t you tell us what is the use of interfaces or abstract class? Why do we need both of them, either of one is sufficient. Isn’t it? What are their individual importance?

  368. Anonymous

    “public void abstract class Employee” and “public void Employee(String name, String address, int number)” These two lines are wrong.. a class as well as a constructor cannot have any return type.

  369. Anonymous

    Does an interface provide almost all benefits of multiple inheritance which is not supported in java.?

  370. Anonymous

    why dont abstract methods have any implementations in an abstract class, but in subclass of the abstract class.?

  371. Mayur

    Interface couldn’t be implemented by any other Interface…..

  372. goutamreddy

    can u give me the difference between abstract class and interfaces

  373. Java Development Training

    It is an error to specify the static modifier for the abstract method.

  374. rakesh

    suppose there is only a one abstract method we have in a abstract class and in a interface…….which of them will be more benificial to implement or extend,a abstract class or a interface??///and why??? please tell

  375. Anonymous

    In what scenario we use abstract class and interface ??

  376. himanshu sharma

    in what scenario we use abstract class and interface ?

  377. Nitin Agrawal

    One more difference.
    Interface never participate in object creation while abstract class object is created with a child class object. Though developer can not create instance of abstract class by using new operator.

    Moreover, Interface are not used for multiple inheritance (What will you inherit from interface. It has nothing to be inherited).

    Interface are used in specifications like JDBC.

    Interface are used in remote accessebility (RMI). You can have 2 interfaces for one service. If you are providing one interface to client, he will not be able to call the methods defined in second interface.

  378. Anand

    can interface extends class?

  379. Debojit

    These differences are mostly syntactical and very specific to java. i found the most astounding differences:
    Interface is the peripheral identity of a class
    Abstract class is the central identity of a concrete class. You actually should not differentiate these two, however these two manifest the most powerful aspects of OOP

  380. Atikur Khan

    Can any one explain:-

    1.Why connection,resultset and statement are interfaces?

    2.When above are interfaces then how can we call their functions as they don’t have method implementation(By Interface definition).

  381. Introduced new blog on java.
    javacodeimpl.blogspot.com

    Keep visiting!!!

  382. by using Abstract class u cont extends multible inhertence,but by using interface we can achive multible inheritence

    by using Abstract class u cont extends multible inhertence,but by using interface we can achive multible inheritence

  383. shivani

    abstract classes can contain constructor

  384. shiv

    public abstract class AbstratctClassTest {
    public AbstratctClassTest(String str){
    System.out.println(“AbstratctClassTest is calling “+str);
    }

    public static void main(String str[]){
    Abst obj=new Abst(“hi”);
    obj.sayHello();
    }
    }

    class Abst extends AbstratctClassTest
    {
    public Abst(String str){
    super(str);
    System.out.println(“in abst constructore”);
    }
    void sayHello(){
    System.out.println(“Hello…”);
    }
    }

  385. Nice Blog!! The info will be very useful for a beginner in the java programming.

  386. As I understand one more difference:

    Interface describes ability of the class.
    Abstract class describes identity of the class.
    Therefore you can have many abilities but just one identity.

    Please add this to your post if found appropriate.

  387. can anyone tell me in which situation one can use abstract class and one can use interface????

    • raju

      if we dont know anything(for example:zero percentage) about implementation go for INTERFACE
      if we know something(for example:roughly 30 to 40 % percentage) about implementation go for abstract class as an abstract class can contain both defined(implemented) and undefined(abstract or unimplemented )methods
      if we know every thing(for example 100% not exactly but almost) about implementation then go for conrrete class that is class

      • Hi Raju,

        Then why we will use interface according to your answer. if we know more less than then also its better to go directly abstract classes. Then why this interface came to market. Can you please explain me this?

        Thanks & Regards
        Sailendra Narayan Jena

  388. vipesh

    please tell me what is the use of interface,when we can instead of interface can we use abstract class contain only abstract method(pure abstract class)

  389. vel murugan

    Scientific calculation using event driven programmi ng…
    i need this program,,plz send me soon..

  390. Gat Yogesh

    hi,
    The interfaces are not default final because it can be extended
    final class can not be sub-classed.

  391. Nayaz

    why do we actually need interface???when abstract class only can do what ever the things done by interface?apart from multiple inheritance?

  392. Anonymous

    Really your blog superb to learn about java

  393. Anonymous

    Really your blog is superb for learning java..

  394. Hi,

    I want to know the exact difference means in which scenarios we will use abstract class and in which scenarios we will use interface.

    My another question on this topic is:
    1. We know that we can’t create object for both interface and abstract class but can you tell me as you have mentioned there in case of main method how we will create the object for the abstract class with examples?

    2. In case of project scenarios if we are creating interface then what is the need for this interface in future cases, which class implements that interface and if we will add some new method inside this interface in future then also it will reflect to all those classes where its got implements. So can you please explain me?

    Thanks & Regards
    Sailendra Narayan Jena

  395. anupriya

    what is extra indirection….you have mentioned in the 8 point….plzz explain

  396. rakesh

    hi joe!…your every article is very admirable…Can u please explain 8th point

  397. naisargee

    thankyou sir , it helped me a lot!!!

  398. Govind Agrawal

    //* Program of implement the interface Keyword *//

    interface A
    {
    void sum();
    }
    class B implements A
    {
    public void sum()
    {
    int a=12,b=3,c;
    c=a+b;
    System.out.println(“Sum of the number =”+c);
    }
    }
    class Result
    {
    public static void main(String as[])
    {
    B s1=new B();
    s1.sum();
    }
    }

  399. sachin

    what is the real time difference between abstract and interface?
    Like When are you going to use abstract and interface in real time project development.

  400. Karthika

    I m totally confused. Can u explain me the difference between abstract class and interface

  401. Anonymous

    Really good stuff……

  402. Anonymous

    very good

  403. Sharma Amit

    Very nice sir … thanks a lot for sharing your knowledge with us…..

  404. Anonymous

    very nice

  405. Anonymous

    Nice Info.
    BTW if someone wants to read little bit about abstract classes check out this link then http://javaandme.com/core-java/abstract-classes
    I found it usefull, hope it will help you guys also.

  406. Payel Hazra

    Hi, the 5th point says that an interface can extend another interface only, but actually an interface can extend multiple interfaces. Correct me if I am wrong.

  407. Srikantareddy A

    hi joe,
    5th point is wrong, i.e an interface can extend any number of interfaces

  408. Sabyasachi

    Nice example and very helpful Sir.
    Example of Interface extends and implements:

    public interface Interface3 {
    public void test3();
    public void gett3();
    }

    public interface Interface2 extends Interface3{
    public void test2();
    public void gett2();
    }

    public interface Interface1 extends Interface2, Interface3 {
    public void test1();
    public String gett1();
    }

    //——- Now in class, implements the Interface(s).
    public class InterTest implements Interface1{
    // implement all unimplemented methods.
    }

    or

    public class InterTest implements Interface2, Interface3, Interface1{
    // implement all unimplemented methods.
    }

    Thanks

  409. Anonymous

    Thanks really good stuff.

  410. Gopal Gowda

    Hi Joe,

    What is the decision point I should be making when choosing interface or an abstract class? This is one question I don’t find proper answer and I always end up seeing the difference b/w them on the net.

    Thanks in Advance.

Your Comment