1

I know that there is already a question related to this, Interface with default methods vs Abstract class in Java 8 but that question was asked way before Java 8 was released and it is just a gist of what is the difference between Abstract classes and interfaces with default methods, and it doesn't provide any practical examples at all, rather it is just full of wordily explanation.

I am posting this question because I am more interested in a practical example that clears the difference between an Abstract Class and Interface with a default method in Java 8.

Have a look at this example,

Abstract Class,

abstract class AbstractClass {

    public int var = 0;

    // These methods must be Overridden
    abstract int x();
    abstract int y();

    // This method is optional
    int z() {
        return 0;
    }

}

Interface (Java 8)

interface Interface {

    public int var = 0;

    // These methods must be Overridden
    int x();
    int y();

    // This method is optional
    default int z(){
        return 0;
    }
}

How could I differentiate in above two if I want to explain its practical difference to a newbie Java programmer (like myself) ?

this article says,

The benefit that default methods bring is that now it’s possible to add a new default method to the interface and it doesn’t break the implementations.

This couldn't possibly be the only benefit of interfaces with default methods, or is it ?

Community
  • 1
  • 1
bhuvesh
  • 729
  • 1
  • 9
  • 21
  • down voter, please explain the reason of down voting ? please don't just down vote and leave; I am here to learn and the only way i could avoid asking unclear questions if you will explain the reason of your down vote. – bhuvesh Feb 03 '15 at 17:39
  • I didn't downvote the question, but note that explaining downvotes is explicitly discouraged by the SE management. Take a downvote to mean what it says on the button: *"This question does not show any research effort; it is unclear or not useful"* (or that the person downvoting did so for a reason they probably shouldn't, which happens too). – T.J. Crowder Feb 03 '15 at 17:40
  • @T.J.Crowder you mean that a user shouldn't explain why he down voted a question ? – bhuvesh Feb 03 '15 at 17:41
  • This feature virtually mimics Scala traits!!!! –  Feb 03 '15 at 17:43
  • That is *why* they have been introduced. Other differences: you may not make a default method final, and a default method may not access fields, since an interface doesn't have any. – JB Nizet Feb 03 '15 at 17:43
  • 2
    It's a very slippery subject; see meta for the full back-and-forth on it. Posting a comment with suggestions for improving a post is always a good idea. Saying "-1 [some kind of explanation here]" is discouraged because, I'm told, it generates lots of back-and-forth without real value. Note this is not *my* policy. :-) – T.J. Crowder Feb 03 '15 at 17:43

1 Answers1

6

First of all, you should understand the difference between

abstract class AbstractClass {

    public int var = 0;

…

and

interface Interface {

    public int var = 0;

    …

The latter is equivalent to

   public static final int var = 0;

which is entirely different to writing

   public int var = 0;

in an abstract class. This difference is already explained in the question you have linked by saying “Abstract classes can have state”. Interfaces can only define constants. This implies that you can’t provide default methods for an interface performing any action with instance fields as an interface can’t define instance fields. default methods can only work atop other interface methods.

Sometimes it really helps trying to understand the “wordily explanation” rather than asking for “a practical example”. In other words, the question and answers you have linked are still valid.

Regarding the purpose of default methods in Java 8, that question has been asked before as well.

To summarize some differences between abstract classes and interfaces:

  • interface members are all public, you can’t define private, protected or package-private members; when you omit the public modifier it’s implied (starting with Java 9, private methods are allowed)
  • interface fields are only for defining constants; they are implicitly static final
  • abstract classes can have static class initializer code and instance constructors
  • abstract classes may have state, i.e. define instance fields and provide methods working with them
  • abstract classes can provide implementations for the methods equals, hashCode and toString defined in java.lang.Object while default methods specified in interfaces can’t override these methods
  • abstract classes can define final, strictfp, synchronized, or native methods
  • a class can extend only one abstract or concrete class but implement more than one interface
Holger
  • 285,553
  • 42
  • 434
  • 765