Difference Between Interface and Abstract Class

Last modified on September 7th, 2014 by Joe.
  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.

Comments on "Difference Between Interface and Abstract Class"

  1. Joe says:

    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.

  2. Balraj kairamkonda says:

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

  3. Arun Pratap Singh says:

    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.

  4. rakesh says:

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

  5. abubakar says:

    good i learn very much from this webiste. Now I understand the difference between abstract class and interface. Last week this question asked in my Java interview.

  6. rajesh says:

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

  7. Sean, melbourne photographer says:

    Hi,
    some nice tips on this site!
    From the list of all differences in abstract class and interfaces, I don’t get difference 8 though – can you clarify please?

  8. raghu says:

    how to utilize interface in another class without implementing that interface

  9. Argha Basu says:

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

  10. maxmetallica says:

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

  11. Manish says:

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

  12. Manas says:

    Why interface cannot be instantiated? what is the difference?

  13. monika bansal says:

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

  14. Aryan Nardiganj says:

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

  15. Thadeuse says:

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

  16. Hari chowdary says:

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

  17. meena says:

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

  18. sam says:

    why abstract class does not support for the multiple inheritance?

  19. Asha.BM says:

    Please mail me this book.

  20. Lokanath says:

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

  21. Zeeshi says:

    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…

  22. kousalya says:

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

  23. satnam singh says:

    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

  24. guest says:

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

  25. Rajanikanth says:

    thanks

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

  26. Mansi Singhal says:

    well written

  27. ganesh says:

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

  28. venkatesan.g says:

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

  29. mohit says:

    nice difference between interface and abstract class

  30. shree says:

    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?

  31. Selva says:

    HI Joseph,

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

    Thanks in advance,
    Selva

  32. neeraj tanwar says:

    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?

  33. Shitansu says:

    Hi Joe,

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

  34. jessie143143 says:

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

  35. khushboo says:

    thank u sir

    plz mail me similarities between an interface and classes

  36. ramesh says:

    nice explanation for interface and abstract class

  37. Praveen says:

    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

  38. ram kumar says:

    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.

  39. Muruga G says:

    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.

  40. Arabinda says:

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

  41. Shaleen says:

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

  42. Jayaram says:

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

  43. Balachandiran says:

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

  44. shiv says:

    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

  45. Venkatesh Bestha says:

    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.

  46. indian freak says:

    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???

  47. yogesh says:

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

  48. Anonymous says:

    Thanks you for giving to this this answer.

  49. pazuzu says:

    Some simple examples would be really great :)

  50. Rajeev Kumar says:

    is an interface implicitly an abstract class?

    Thanks…

  51. Joe says:

    No. An interface is not an absrract class implicitly. Those two are interpreted in a different way.

  52. Arjun says:

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

  53. Musaddiquie Husain says:

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

  54. Bhaskar says:

    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

  55. Manoj Singh says:

    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 ?

  56. karthik says:

    where should we use interface and Abstract?

  57. ravi says:

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

  58. Anish says:

    Very helpful site. Will share it on facebook :)

  59. Bimal Modak says:

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

  60. lalit says:

    abstract class does not have a body

  61. chirag pandey says:

    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.

  62. pawan kumar says:

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

  63. Nagarjuna Reddy.M says:

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

  64. malli says:

    i think 5 point i have doubt

  65. Srikanth K says:

    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

  66. Vijay says:

    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.

  67. Sreejith says:

    hmmm have to improve

  68. Rawinder dhillon says:

    thx

  69. Ramaseshan says:

    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

  70. akanksha mathur says:

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

  71. Mallikarjuna says:

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

  72. […] wrote an article describing difference between abstract class and interface. Lot of people are asking for example with a nice usage scenario. JDBC is a direct and excellent […]

  73. Mayur Kumar says:

    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

  74. Shailendra kumar shail says:

    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.

  75. Shahjahan says:

    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.

  76. Vishaka says:

    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.

  77. mano says:

    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?

  78. droni says:

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

  79. Atul kumar singh says:

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

    }

    }

  80. P.Vijayakumar says:

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

  81. Anonymous says:

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

  82. Kumar says:

    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.

  83. Anonymous says:

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

  84. sou says:

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

  85. Venike ViswanathReddy says:

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

  86. Dharmendra says:

    in which condition abstract class failure but interfaces are used.

  87. Hari Krishna says:

    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 ?

  88. Hari Krishna says:

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

  89. Akhilesh says:

    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?

  90. Revan says:

    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.

  91. suresh says:

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

  92. vicky says:

    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.

  93. Puja Kumari says:

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

  94. suresh says:

    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?

  95. Jayapal says:

    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.

  96. ashish says:

    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 …

  97. vivek says:

    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

  98. anil says:

    In Interface slow because we need to search the implementation of methods in another class so it takes indirection..

  99. selvakumar says:

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

  100. selvakumar says:

    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.

  101. Gayathri Chitnhala says:

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

  102. Bhavesh says:

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

  103. Mani says:

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

  104. Gopal says:

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

  105. chinju says:

    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.

  106. vaibhav says:

    vary nice difference is easy understandable

  107. Ranjith says:

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

  108. Rajeswari says:

    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?

  109. rajkumar says:

    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.

  110. Manjinder singh says:

    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…

  111. Kamlesh says:

    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

  112. Arvind says:

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

  113. Noorul says:

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

  114. jignesh says:

    thank you for this diffrence

  115. Gyanendra Singh says:

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

  116. Pradeep Kumar Verma says:

    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

  117. Manish says:

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

  118. Anonymous says:

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

  119. Anonymous says:

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

  120. Mayur says:

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

  121. goutamreddy says:

    can u give me the difference between abstract class and interfaces

  122. rakesh says:

    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

  123. satish says:

    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

  124. Nitin Agrawal says:

    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.

  125. Ankit Kumar says:

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

  126. Ankit Kumar says:

    default constractor provided by compiler nor jre.

  127. chinna says:

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

  128. Rushik Sutariya says:

    First, thanks for this wonderful list on difference between interface and abstract class.

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

  129. Debojit says:

    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

  130. moni says:

    Abstract classes can contain constructors but interface cannot

  131. Atikur Khan says:

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

  132. Kapil says:

    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.

  133. Manoj says:

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

  134. Manoj says:

    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.

  135. Parag says:

    There should be at least one abstract method in the class

  136. aman says:

    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 :-)

  137. raju says:

    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

  138. Anonymous says:

    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

  139. Rajkumar Chaudhary says:

    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.

  140. Sailendra Narayan Jena says:

    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

  141. Panchanan mohapatra says:

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

  142. Alok says:

    Hi Panchanan,

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

    Thanks

  143. Sailendra Narayan Jena says:

    Joe, what a tutorial about the difference between abstract class and interface, thanks.

    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.

  144. sachin says:

    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.

  145. Mohammad Umar Ali Karimi says:

    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.

  146. Sabyasachi says:

    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

  147. harshad says:

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

Comments are closed for "Difference Between Interface and Abstract Class".