Interface with default methods vs Abstract class in Java 8

Hi Experts,

when to use default methods of interface?
why default method is introduced?
we can achieve same with abstract class?
can some one provide explanation with example?

Thanks
LVL 2
srikoteshAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
I personally would not use default methods on a day to day basis. Its something Oracle introduced to cater for backward compatibility in the collections framework. Essentially, it allows your interface to have a default implementation if one is not provided in the implementing class.

For example the forEach method does not exist on the  List interface but Oracle defined a default implementation in the Iterable interface which is a super interface of List. So List basically inherits this functionality.

If they introduced the method on the List interface without a default implementation, you would have havoc across your codebase.
0
dpearsonCommented:
Interface means just method definitions - no implementations.
interface with default methods means definitions with some implementations - no data
abstract class means methods with definitions and some implementations plus data

So each level does a bit more than the one before it.

As gurpsbassi said, default methods were introduced to let Java interfaces be extended to support new methods in Java 8 without breaking millions of existing programs.

I think you should generally use the highest one on the list that meets your criterion (e.g. no implementations needed - just use an interface etc.)

Doug
0
srikoteshAuthor Commented:
Hi dpearson,

can you please provide bit clarity on the below statements
interface with default methods means definitions with some implementations - no data
implementation will be there with out data?
abstract class means methods with definitions and some implementations plus data
0
dpearsonCommented:
So we can have:

// Pure interface
public interface MyInterface {
   int getValue() ;
}

//  Interface with default method
public interface MyDefaultInterface {
   int getValue() ;
   default int getOtherValue() { return 1 ; }   // This is a default method implementation
}

// Abstract class
public abstract class MyAbstractClass {
    private int m_Value ; // This is data associated with the class - can't do this in either interface

    abstract int getValue() ;
    int getOtherValue()  { return m_Value ; } // Implementation using the data
}

In each case you can do:
public class MyConcreteClass1 implements MyInterface { ... }
public class MyConcreteClass2 implements MyDefaultInterface { ... }
public class MyConcreteClass3 extends MyAbstractClass { ... }
and the concrete class can contain data (fields) as well as methods.

Does that help explain the difference.  There's more here too:
https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.