aspose file tools*
The moose likes Beginning Java and the fly likes What is the Difference between Abstract class and Interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is the Difference between Abstract class and Interface?" Watch "What is the Difference between Abstract class and Interface?" New topic
Author

What is the Difference between Abstract class and Interface?

Mak Smash
Greenhorn

Joined: May 15, 2011
Posts: 17

Can anyone tell me difference between Abstract class and Interface??

Thank you,
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

We have an FAQ about that: link.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mak Smash
Greenhorn

Joined: May 15, 2011
Posts: 17

Hi,
I visited that link, there is mentioned "use an interface if you're sure the API is stable for the long run" can you please tell me, whats the harm if we add method in interface?

Thank you,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

If lots of people have made classes that implement your interface, and you add a method to the interface, then you are forcing everybody to modify their classes - because all (non-abstract) classes that implement an interface must implement all the methods in the interface.

In an abstract class, you could add the method as a non-abstract method, which would not force all classes that extend the abstract class to implement the new method.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Himanshu V Singh
Greenhorn

Joined: May 06, 2011
Posts: 17
Jesper de Jong wrote:If lots of people have made classes that implement your interface, and you add a method to the interface, then you are forcing everybody to modify their classes - because all (non-abstract) classes that implement an interface must implement all the methods in the interface.

In an abstract class, you could add the method as a non-abstract method, which would not force all classes that extend the abstract class to implement the new method.




Jesper de Jong@ yea i am agree with you but in application of Loose Coupling, adding a non-abstract method in abstract class just to avoid modification in subclasses is actually making your abstract class (super class) more biased towards sub-classes n thats is not a good practice.

Adding a Interface, placing the method prototype their and than making your subclass abstract too can do the trick. Than you don't have to implement the method in class than and don't forget to make your utility method static because you cant create an instance now.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
You appear to have misunderstood Jesper. Adding methods to an interface is not loose coupling, but breaking the code. Go and find my old Engine and Plant examples. Change the Engine file to read like this: . . . and try recompiling all the code. Now add this class and change "implements Engine" to extends "BasicEngine" throughout. . . . and see what happens. Note you can override the breakDown method in any of your Engine classes, and you can miss out the Exception if you wish
Mykhailo Kozik
Greenhorn

Joined: May 12, 2011
Posts: 16

This link contains good comparison table.
Saurabh Chauhn
Greenhorn

Joined: May 19, 2014
Posts: 5
The idea to introduce the interface came with one of the OOPs concept which was missing in the Java i.e Multiple Inheritance . As we know that Java does not support the multiple inheritance which means a class can extend only one parent class, hence Interface came into existence.

Abstract class comes with he concept where we can have the implementation of some of its methods however we can not provide the implementation of the methods in the interface

We need to decide whether to use the Abstract class or the Interface based on their pros and cons.
For example If we are choosing an abstract class over an interface than we are loosing the chance to extend another class, however we can implement multiple interfaces to achieve the multiple capability. One of the best example for choosing the interface over abstract class is Thread vs Runnable.

Please see the below URL for very good details :

http://javatopics77.blogspot.com/p/difference-between-interface-and.html
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7816
    
  21

Saurabh Chauhn wrote:The idea to introduce the interface came with one of the OOPs concept which was missing in the Java i.e Multiple Inheritance.

Actually, I'm not at all sure that multiple inheritance is a cornerstone of OO at all; and I'm pretty darn sure that Java interfaces didn't "come" about as a result of it.
It seems far more likely that the developers of Java had a darn good look at C++ (at the time, probably the only mature OO language around outside the world of academia) and said:
  • Syntax? Tick
  • Abstraction? Tick
  • Virtual? Tick
  • Primitives? Tick
  • Pointers? CROSS
  • Multiple Inheritance? CROSS
  • ...
    and what we see now is what they decided on.

    Having made the decision not to allow multiple class inheritance didn't have any impact on whether interfaces could allow it though.

    My 2¢

    Winston

    PS: You do realise that you've replied to a 3-year old thread, don't you? There's no particular problem with it, but I doubt that any of the original posters are around to appreciate the ongoing discussion (apart from Campbell, of course).

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14156
        
      19

    Saurabh Chauhn wrote:The idea to introduce the interface came with one of the OOPs concept which was missing in the Java i.e Multiple Inheritance . As we know that Java does not support the multiple inheritance which means a class can extend only one parent class, hence Interface came into existence.

    Making multiple inheritance possible is not the main reason to have interfaces. It's one of the things that interfaces make possible in Java, but it is not the main reason why there are interfaces.

    The most important reason to use interfaces is to decouple the API of a class from its implementation. When you program against interfaces instead of specific classes, then it becomes easy to replace one implementation with another. For example, when you need to do something with a List in your program, then make the variable in which you store it of the interface List, and not of a specific implementation such as ArrayList:

    The rest of the program only needs to know that names is a List - it should not care about what specific implementation of List is used. Maybe after some time you discover that a LinkedList would be more efficient for your particular use case. It's very easy to change the implementation:

    Note that the type of the variable has not changed; it's still List<String>. So, you don't need to change anything in the rest of the program, because names is still a List<String>. If you would have changed the type of the variable from ArrayList to LinkedList, it is possible that your program would break, if you would have called methods on it that exist only in ArrayList but not in LinkedList.

    The OO principles related to this are information hiding and loose coupling.
    Anagha Khyathi
    Greenhorn

    Joined: May 19, 2014
    Posts: 4
    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 behaviour.
    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.
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7816
        
      21

    Anagha Mamtha wrote:2. Variables declared in a Java interface is by default final.

    And static.

    Winston
    Paweł Baczyński
    Bartender

    Joined: Apr 18, 2013
    Posts: 978
        
      16

    Winston Gutkowski wrote:
    Anagha Mamtha wrote:2. Variables declared in a Java interface is by default final.

    And static.

    And public.
    And yes, I know it is said in point 3


    Formely Pawel Pawlowicz
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: What is the Difference between Abstract class and Interface?