Java 8 - Default Methods Tutorial

Java 8 - Default Methods Tutorial with Examples

Hello!

This post is from the Java 8 Series!

In Java 8 now we can create a non-abstract method implementation to interfaces. It sounds really strange in the first place, but makes sense after a few minutes

Let's see an example with PaymentService.

  • We must create an Interface to have PaymentService
  • We must create a PayPal implementation
  • We must create a Moip implementation
  • We must create a way to calculate the final order price

Area you ready?

Step 1 - Creating the Scenario for Payment Service using PayPal

The PaymentService Interface

The implementation class to use PayPal services

A test to just check if everything is working nice : )

Great! Let's create another PaymentService Provider in the next example

Step 2 - Creating the Payment Service using Moip

And its test

Hey! Probably you are asking me: Hmm why don't you just create a specific method to calculate the finalPrice?

It's time to use the Default Method from Java 8 \o/

Step 3 - Using Default Methods from Java 8

I would like to create a method in the PaymentService Interface but I wouldn't like to break all the implementations that are using this interface.

So, I will add a non-abstract method implementation to the PaymentService Interface by utilizing the default keyword.

Notice that, if we add the non-abstract method to the interface, each implementation will keep working nice!

And notice another thing: We crated a kind of template method that could be used to calculate the final price and left to the Implementation the job to define the discount price.

That's it! I hope that this Java 8 Tutorial would be useful to you!

Thanks!

Follow us to keep up to date! \o/

Alexandre Gama

Alexandre Gama

Hacking Code Founder

Hacking Code Founder and Writer, Passionate Senior Software Engineer and Team Leader at @Elo7, Teacher of several courses at Caelum, Speaker at many conferences and terrible guitar player.
Java 8 - Lambda Expression - Java Tutorial Hacking Code

Java 8 - Lambda Expression

Do you know how to use Java 8 Lambda Expressions? No? Let's see it!
Java 8 - Default Methods - Java Tutorial Hacking Code

Java 8 - Default Methods Tutorial

Did you know that now we can have non-abstract methods in Interfaces? Let's see why!
Java 8 - Functions - Java Tutorial Hacking Code

Java 8 - Functions

Do you want to try a more Functional Programming Style with Functions?
Java 8 - Method Reference - Java Tutorial Hacking Code

Java 8 - Method Reference

Method Reference is a really awesome feature from Java 8! Let's see why and how to use it!
Java 8 - Stream Count - Java Tutorial Hacking Code

Java 8 - Stream Match Tutorial

Stop suffering and let's use Java 8 Stream Count!
Java 8 - Stream Filter - Java Tutorial Hacking Code

Java 8 - Stream Filter

Filtering data with Stream Filter from Java 8
Java 8 - Stream Map - Java Tutorial Hacking Code

Java 8 - Stream Map

Updating data with Stream Map from Java 8
Java 8 - Stream Sorted - Java Tutorial Hacking Code

Java 8 - Stream Sorted

Sorted Collections with Java 8 Stream Sorted
Java 8 - Stream Match - Java Tutorial Hacking Code

Java 8 - Stream Match Tutorial

Let's see if any data Match with rules using Stream Match from Java 8!
Java 8 Stream Reduce Tutorial - Java Guide by Live Coding

Java 8 - Stream Reduce Tutorial

Let's move on and reduce some data with Stream Reduce from Java8!
Java 8 Consumer Tutorial - Java Guide by Live Coding

Java 8 - Consumers Tutorial

Java 8 Consumers - Making our life easier!
Java 8 Predicate Tutorial - Java Guide by Live Coding

Java 8 - Predicate Tutorial

Java 8 Predicate - An important built-in Functional Interface!
Java 8 Comparator Tutorial - Java Guide by Live Coding

Java 8 - Comparator Tutorial

Java 8 Comparator is known from old times...Let's see what has changed!
Java 8 Supplier Tutorial - Java Guide by Live Coding

Java 8 - Supplier Tutorial

Java 8 Suppliers become an easy Functional Interface to like!

0 Comments

Leave a Reply

Share This
Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

%d bloggers like this: