| 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
Bartender
Joined: Aug 16, 2005
Posts: 12921
|
|
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: 32674
|
|
|
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.
|
 |
 |
|
|
subject: What is the Difference between Abstract class and Interface?
|
|
|