Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the Difference between Abstract class and Interface?

 
Mak Smash
Greenhorn
Posts: 17
C++ Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me difference between Abstract class and Interface??

Thank you,
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have an FAQ about that: link.
 
Mak Smash
Greenhorn
Posts: 17
C++ Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This link contains good comparison table.
 
Saurabh Chauhn
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
     
    Jesper de Jong
    Java Cowboy
    Saloon Keeper
    Posts: 15272
    37
    Android IntelliJ IDE Java Scala Spring
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 10417
    63
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Anagha Mamtha wrote:2. Variables declared in a Java interface is by default final.

    And static.

    Winston
     
    Paweł Baczyński
    Bartender
    Posts: 1776
    33
    Firefox Browser IntelliJ IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic