| Author |
Difference between interfaces and abstract classes
|
Sowm Herur
Ranch Hand
Joined: Jul 07, 2008
Posts: 32
|
|
What are the benefits of interface over abstract classes? For me both look alike.
How will using an interface give me the edge over using abstract classes?
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
Interfaces define functionality, abstract classes define functionality and can define behaviour. Think if an interface as a contract and an abstract class as a template.
Also, you cannot extends two abstract classes.
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Sowm Herur
Ranch Hand
Joined: Jul 07, 2008
Posts: 32
|
|
What do you mean by interface as a contract?
We can use classes to implement the methods of both abstract classes and interfaces, then how is it different?
In what scenario will we prefer interfaces and in what scenarios will we prefer abstract classes?
Please explain with an example of both so that i can understand it better
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
A contact is a formal definition of what something can do. Interfaces define the funtionality of a "thing", for example the java.lang.Comparable interface in the JDK: every class that implements it will implement a method compareTo so you know a funtion of every Comparable thing is you can compare it to something else.
We can use classes to implement the methods of both abstract classes and interfaces, then how is it different?
LikeAbstract classes can define both functionality and implementation. For example, the java.text.DateFormat class in the JDK provides a lot of functionality. Is only abstact methods are parse(String source, ParsePosition pos) and format(Date date, StringBuffer toAppendTo, FieldPosition fieldPosition). So every class that extends DateFormat gets all the methods DateFormat implements as well as being required to provide implementations for these two methods.
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
Sowm Herur wrote:
What are the benefits of interface over abstract classes? For me both look alike.
The major difference is that an abstract class can carry implementation which an interface cannot.
The Java inheritance model stipulates that implementation can only be inherited from one source. If an abstract class is inherited then this one shot is used up. Interfaces don't have this limitation. Any number of interfaces can be inherited.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12911
|
|
|
See: InterfaceVsAbstractClass
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Sowm Herur
Ranch Hand
Joined: Jul 07, 2008
Posts: 32
|
|
I dint understand this explanation
The Java inheritance model stipulates that implementation can only be inherited from one source. If an abstract class is inherited then this one shot is used up. Interfaces don't have this limitation. Any number of interfaces can be inherited.
Please explain that with a small example
i can use abstract methods in different implementations rite and same with interfaces
|
 |
M K Rayapudi
Ranch Hand
Joined: Feb 19, 2007
Posts: 157
|
|
In one perspective I will differentiate Abstract classes and Interfaces as follows:
Abstract classes can have concrete methods(& abstract methods), so that you can add concrete methods to an abstract class when ever you want during its life time.
Interfaces can have only abstract methods. But you can't add abstract methods for the interface when ever you want, if it happens your clients will fail if they unaware about the newly added methods.
|
R6i
|
 |
Abubacker Siddik
Ranch Hand
Joined: Aug 12, 2009
Posts: 90
|
|
Hi,
Maybe you take a look at this. Helpful to you Sowm..
Abstract vs Interface
Abu.A
|
 |
Embla Tingeling
Ranch Hand
Joined: Oct 22, 2009
Posts: 237
|
|
Sowm Herur wrote:
Please explain that with a small example
It's basic Java inheritance really.
1. A class can extend one other class (abstract or non-abstract) at the most, but it can implement as many interfaces as it wish.
2. An interface can extend as many interfaces as it wish but no class (abstract or non-abstract) because it cannot carry implementation.
Examples:
class A {}
abstract class B{}
interface I {}
interface J {}
interface K {}
//
class C extends A {} // ok
class C extends B {} // ok
class C extends A implements I, J, K {} // ok
class C extends B implements I, J, K {} // ok
class C implements I, J, K {} // ok
class C extends A, B {} // bad - cannot inherit implementation from two sources
//
interface D extends I, J , K {} // ok
interface D extends A // bad - an interface cannot carry implementation
interface D extends B // bad - an interface cannot carry implementation
|
 |
 |
|
|
subject: Difference between interfaces and abstract classes
|
|
|