• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between interfaces and abstract classes

 
Sowm Herur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sowm Herur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15278
39
Android IntelliJ IDE Java Scala Spring
 
Sowm Herur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abubacker Siddik
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Maybe you take a look at this. Helpful to you Sowm..
Abstract vs Interface


Abu.A
 
Embla Tingeling
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic