This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of React Cookbook: Recipes for Mastering the React Framework and have David Griffiths & Dawn Griffiths on-line!
See this thread for details.
Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Difference between interfaces and abstract classes

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See: InterfaceVsAbstractClass
 
Sowm Herur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic