Java 8 Default Methods
Haim Michael
September 19th
, 2017
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
lifemichael
https://youtu.be/MvwUYHPDDzQ
© 1996-2017 All Rights Reserved.
Haim Michael Introduction
● Snowboarding. Learning. Coding. Teaching. More
than 18 years of Practical Experience.
lifemichael
© 1996-2017 All Rights Reserved.
Haim Michael Introduction
● Professional Certifications
Zend Certified Engineer in PHP
Certified Java Professional
Certified Java EE Web Component Developer
OMG Certified UML Professional
● MBA (cum laude) from Tel-Aviv University
Information Systems Management
lifemichael
© 1996-2017 All Rights Reserved.
Introduction
 The interfaces we define are kind of contracts
that specify which methods should be defined
in each and every class that implements them.
lifemichael
interface IBrowser
{
public void browse(URL address);
public void back();
public void forward();
public void refresh();
}
IBrowser browser = new Firefox();
© 1996-2017 All Rights Reserved.
Introduction
 The interface in Java is a reference type,
similarly to class.
 The interface can include in its definition only
public final static variables (constants), public
abstract methods (signatures), public default
methods, public static methods, nested types
and private methods.
lifemichael
© 1996-2017 All Rights Reserved.
Introduction
 The default methods are defined with the
default modifier, and static methods with the
static keyword. All abstract, default, and static
methods in interfaces are implicitly public. We
can omit the public modifier.
 The constant values defined in an interface are
implicitly public, static, and final. We can omit
these modifiers.
lifemichael
© 1996-2017 All Rights Reserved.
Public Final Static Variables
lifemichael
 Although Java allows us to define enums,
many developers prefer to use interfaces that
include the definition of public static variables.
public interface IRobot {
public final static int MAX_SPEED = 100;
public final static int MIN_SPEED = 10;
© 1996-2017 All Rights Reserved.
Public Abstract Methods
 The public abstract methods we define are
actually the interface through which objects
interact with each other.
lifemichael
public interface IRobot {
public final static int MAX_SPEED = 100;
public final static int MIN_SPEED = 10;
public abstract void moveRight(int steps);
public abstract void moveLeft(int steps);
public abstract void moveUp(int steps);
public abstract void moveDown(int steps);
}
© 1996-2017 All Rights Reserved.
Default Methods
 As of Java 8 the interface we define can
include the definition of methods together with
their implementation.
 This implementation is known as a default one
that will take place when the class that
implements the interface doesn't include its
own specific implementation.
lifemichael
© 1996-2017 All Rights Reserved.
Default Methods
lifemichael
public interface IRobot {
public final static int MAX_SPEED = 100;
public final static int MIN_SPEED = 10;
public abstract void moveRight(int steps);
public abstract void moveLeft(int steps);
public abstract void moveUp(int steps);
public abstract void moveDown(int steps);
public default void turnAround() {
moveRight(1);
moveRight(1);
moveRight(1);
moveRight(1);
}
}
© 1996-2017 All Rights Reserved.
Default Methods
 When extending an interface that contains a
default method, we can let the extended
interface inherit the default method, we can
redeclare the default method as an abstract
method and we can even redefine it with a
new implementation.
lifemichael
© 1996-2017 All Rights Reserved.
Static Methods
 The interface we define can include the
definition of static methods.
lifemichael
© 1996-2017 All Rights Reserved.
Static Methods
lifemichael
public interface IRobot {
public static String getIRobotSpecificationDetails() {
return "spec...";
}
public final static int MAX_SPEED = 100;
public final static int MIN_SPEED = 10;
public abstract void moveRight(int steps);
public abstract void moveLeft(int steps);
public abstract void moveUp(int steps);
public abstract void moveDown(int steps);
public default void turnAround() {
moveRight(1);
moveRight(1);
moveRight(1);
moveRight(1);
}
}
© 1996-2017 All Rights Reserved.
Nested Types
 The interface we define can include the
definition of new static inner types.
 The inner types we define will be implicitly
static ones.
lifemichael
© 1996-2017 All Rights Reserved.
Nested Types
lifemichael
public interface IRobot {
public static String getIRobotSpecificationDetails() {
return "spec...";
}
public final static int MAX_SPEED = 100;
public final static int MIN_SPEED = 10;
public abstract void moveRight(int steps);
public abstract void moveLeft(int steps);
public abstract void moveUp(int steps);
public abstract void moveDown(int steps);
public default void turnAround() {
moveRight(1);
moveRight(1);
moveRight(1);
moveRight(1);
}
public static class Engine {
public void doSomething() {
getIRobotSpecificationDetails();
}
}
}
© 1996-2017 All Rights Reserved.
Nested Types
 We can find an interesting example for
defining abstract inner type inside an interface
when developing a remote service on the
android platform.
lifemichael
© 1996-2017 All Rights Reserved.
Nested Types
lifemichael
public interface ICurrencyService extends android.os.IInterface
{
public static abstract class Stub extends android.os.Binder
implements ICurrencyService
{
}
public double getCurrency(java.lang.String currencyName)
throws android.os.RemoteException;
}
© 1996-2017 All Rights Reserved.
Private Methods
 As of Java 9, the interface we define can
include the definition of private methods.
 Highly useful when having more than one
default methods that share parts of their
implementations. We can place the common
parts in separated private methods and make
our code shorter.
lifemichael
© 1996-2017 All Rights Reserved.
Private Methods
lifemichael
package com.lifemichael;
public interface IRobot {
...
public abstract void moveDown(int steps);
public default void turnAround() {
moveRight(1);
step();
moveRight(1);
step();
...
}
private void step(){
//...
}
}
© 1996-2017 All Rights Reserved.
Interfaces as APIs
 Companies and organizations that develop
software to be used by other developers use
the interface as API.
 While the interface is made public, its
implementation is kept as a closely guarded
secret and it can be revises at a later date as
long as it continues to implement the original
interface.
lifemichael
© 1996-2017 All Rights Reserved.
Interfaces as Types
 The new defined interface is actually a new
type we can use whenever we define a
variable or a function parameter.
 Using the interface name as a type adds more
flexibility to our code.
lifemichael
© 1996-2017 All Rights Reserved.
Evolving Interfaces
 When we want to add more abstract methods
to an interface that was already defined, all
classes that implement the old interface will
break because they no longer implement the
old interface. Developers relying on our
interface will protest.
lifemichael
© 1996-2017 All Rights Reserved.
Evolving Interfaces
 We can either define a new interface that
extend the one we already have, or define the
new methods as default ones.
 The default methods allow us to add new
functionality to interfaces we already defined,
while keeping the binary compatibility with
code written for older versions of those
interfaces.
lifemichael
© 1996-2017 All Rights Reserved.
Interface as a Dictating Tool
 We can use interfaces for dictating specific
requirements from other developers for using
functions we developed.
lifemichael
© 1996-2017 All Rights Reserved.
Interfaces as Traits
 Using the default methods we can develop
interfaces that will be used as traits in order to
reduce the amount of code in our project.
lifemichael
Car
SportCar
RacingCar
Bag
SportiveBag ElegantBag
Sportive
© 1996-2017 All Rights Reserved.
lifemichael
Questions & Answers
If you enjoyed my lecture please leave me a comment
at http://speakerpedia.com/speakers/life-michael.
Thanks for your time!
Haim.

More Related Content

PDF
Java 8 Lambda Built-in Functional Interfaces
PPTX
Java string handling
PDF
Collections In Java
PPTX
Strings in Java
PPTX
Java string handling
PPTX
Type casting in java
PPTX
History Of JAVA
PPS
String and string buffer
Java 8 Lambda Built-in Functional Interfaces
Java string handling
Collections In Java
Strings in Java
Java string handling
Type casting in java
History Of JAVA
String and string buffer

What's hot (20)

PPT
Abstract class in java
PDF
07 java collection
PDF
Java IO
PPTX
Constructor in java
PPTX
Java abstract class & abstract methods
PPTX
Java - Collections framework
PDF
Java keywords
PPT
Java Collections Framework
PPTX
Constructor in java
PPS
Wrapper class
PPTX
Java Method, Static Block
PDF
Constructors and destructors
PDF
Basic Java Programming
PPT
Java interfaces
PPTX
Introduction to Java Strings, By Kavita Ganesan
PPTX
Arrays in Java
PPTX
Arrays in java
PPTX
Operator overloading
PDF
String handling(string class)
Abstract class in java
07 java collection
Java IO
Constructor in java
Java abstract class & abstract methods
Java - Collections framework
Java keywords
Java Collections Framework
Constructor in java
Wrapper class
Java Method, Static Block
Constructors and destructors
Basic Java Programming
Java interfaces
Introduction to Java Strings, By Kavita Ganesan
Arrays in Java
Arrays in java
Operator overloading
String handling(string class)
Ad

Similar to Java 8 Default Methods (20)

PDF
Lecture 5 interface.pdf
PDF
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
PPTX
Lecture 18
PDF
21UCAC31 Java Programming.pdf(MTNC)(BCA)
PPTX
Interface in java
PDF
Advanced Programming _Abstract Classes vs Interfaces (Java)
PDF
Java/J2EE interview Qestions
PPT
Inheritance
PPTX
Java presentation
PPTX
Java basics
PPTX
Object Oriented Programming - Polymorphism and Interfaces
PPTX
Unit II Inheritance ,Interface and Packages.pptx
PPTX
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
PDF
Exception handling and packages.pdf
PDF
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
PPTX
Core java oop
PPTX
abstract,final,interface (1).pptx upload
PPT
9 abstract interface
PDF
LectureNotes-02-DSA
PPTX
Objects and classes in OO Programming concepts
Lecture 5 interface.pdf
Open Problems in Automatically Refactoring Legacy Java Software to use New Fe...
Lecture 18
21UCAC31 Java Programming.pdf(MTNC)(BCA)
Interface in java
Advanced Programming _Abstract Classes vs Interfaces (Java)
Java/J2EE interview Qestions
Inheritance
Java presentation
Java basics
Object Oriented Programming - Polymorphism and Interfaces
Unit II Inheritance ,Interface and Packages.pptx
10-Lecture10_Leeeeeeeeeeeeeeeecture.pptx
Exception handling and packages.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
Core java oop
abstract,final,interface (1).pptx upload
9 abstract interface
LectureNotes-02-DSA
Objects and classes in OO Programming concepts
Ad

More from Haim Michael (20)

PDF
Getting Started with Typer: Building Modern CLIs in Python [Free Meetup]
PDF
Prompt Engineering Jump Start [Free Meetup]
PDF
IntelliJ Debugging Essentials for Java Developers
PDF
The Visitor Classic Design Pattern [Free Meetup]
PDF
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
PDF
Introduction to Pattern Matching in Java [Free Meetup]
PDF
Mastering The Collections in JavaScript [Free Meetup]
PDF
Beyond Java - Evolving to Scala and Kotlin
PDF
JavaScript Promises Simplified [Free Meetup]
PDF
Scala Jump Start [Free Online Meetup in English]
PDF
The MVVM Architecture in Java [Free Meetup]
PDF
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
PDF
Anti Patterns
PDF
Virtual Threads in Java
PDF
MongoDB Design Patterns
PDF
Introduction to SQL Injections
PDF
Record Classes in Java
PDF
Microservices Design Patterns
PDF
Structural Pattern Matching in Python
PDF
Unit Testing in Python
Getting Started with Typer: Building Modern CLIs in Python [Free Meetup]
Prompt Engineering Jump Start [Free Meetup]
IntelliJ Debugging Essentials for Java Developers
The Visitor Classic Design Pattern [Free Meetup]
Typing in Python: Bringing Clarity, Safety and Speed to Your Code [Free Meetup]
Introduction to Pattern Matching in Java [Free Meetup]
Mastering The Collections in JavaScript [Free Meetup]
Beyond Java - Evolving to Scala and Kotlin
JavaScript Promises Simplified [Free Meetup]
Scala Jump Start [Free Online Meetup in English]
The MVVM Architecture in Java [Free Meetup]
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Anti Patterns
Virtual Threads in Java
MongoDB Design Patterns
Introduction to SQL Injections
Record Classes in Java
Microservices Design Patterns
Structural Pattern Matching in Python
Unit Testing in Python

Recently uploaded (20)

PDF
OpenEXR Virtual Town Hall - August 2025
PDF
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
PDF
Mobile App for Guard Tour and Reporting.pdf
PDF
MaterialX Virtual Town Hall - August 2025
PDF
Module 1 - Introduction to Generative AI.pdf
PPTX
Folder Lock 10.1.9 Crack With Serial Key
PDF
Difference Between Website and Web Application.pdf
PPTX
MCP empowers AI Agents from Zero to Production
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
PDF
Top AI Tools for Project Managers: My 2025 AI Stack
PPTX
Chapter_05_System Modeling for software engineering
PDF
How to Set Realistic Project Milestones and Deadlines
PDF
solman-7.0-ehp1-sp21-incident-management
PPTX
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
PDF
Top 10 Project Management Software for Small Teams in 2025.pdf
PPTX
Post-Migration Optimization Playbook: Getting the Most Out of Your New Adobe ...
PPTX
Relevance Tuning with Genetic Algorithms
PPTX
Greedy best-first search algorithm always selects the path which appears best...
PDF
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
PPT
3.Software Design for software engineering
OpenEXR Virtual Town Hall - August 2025
Streamlining Project Management in Microsoft Project, Planner, and Teams with...
Mobile App for Guard Tour and Reporting.pdf
MaterialX Virtual Town Hall - August 2025
Module 1 - Introduction to Generative AI.pdf
Folder Lock 10.1.9 Crack With Serial Key
Difference Between Website and Web Application.pdf
MCP empowers AI Agents from Zero to Production
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Top AI Tools for Project Managers: My 2025 AI Stack
Chapter_05_System Modeling for software engineering
How to Set Realistic Project Milestones and Deadlines
solman-7.0-ehp1-sp21-incident-management
Bandicam Screen Recorder 8.2.1 Build 2529 Crack
Top 10 Project Management Software for Small Teams in 2025.pdf
Post-Migration Optimization Playbook: Getting the Most Out of Your New Adobe ...
Relevance Tuning with Genetic Algorithms
Greedy best-first search algorithm always selects the path which appears best...
Building an Inclusive Web Accessibility Made Simple with Accessibility Analyzer
3.Software Design for software engineering

Java 8 Default Methods

  • 1. Java 8 Default Methods Haim Michael September 19th , 2017 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael https://youtu.be/MvwUYHPDDzQ
  • 2. © 1996-2017 All Rights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 18 years of Practical Experience. lifemichael
  • 3. © 1996-2017 All Rights Reserved. Haim Michael Introduction ● Professional Certifications Zend Certified Engineer in PHP Certified Java Professional Certified Java EE Web Component Developer OMG Certified UML Professional ● MBA (cum laude) from Tel-Aviv University Information Systems Management lifemichael
  • 4. © 1996-2017 All Rights Reserved. Introduction  The interfaces we define are kind of contracts that specify which methods should be defined in each and every class that implements them. lifemichael interface IBrowser { public void browse(URL address); public void back(); public void forward(); public void refresh(); } IBrowser browser = new Firefox();
  • 5. © 1996-2017 All Rights Reserved. Introduction  The interface in Java is a reference type, similarly to class.  The interface can include in its definition only public final static variables (constants), public abstract methods (signatures), public default methods, public static methods, nested types and private methods. lifemichael
  • 6. © 1996-2017 All Rights Reserved. Introduction  The default methods are defined with the default modifier, and static methods with the static keyword. All abstract, default, and static methods in interfaces are implicitly public. We can omit the public modifier.  The constant values defined in an interface are implicitly public, static, and final. We can omit these modifiers. lifemichael
  • 7. © 1996-2017 All Rights Reserved. Public Final Static Variables lifemichael  Although Java allows us to define enums, many developers prefer to use interfaces that include the definition of public static variables. public interface IRobot { public final static int MAX_SPEED = 100; public final static int MIN_SPEED = 10;
  • 8. © 1996-2017 All Rights Reserved. Public Abstract Methods  The public abstract methods we define are actually the interface through which objects interact with each other. lifemichael public interface IRobot { public final static int MAX_SPEED = 100; public final static int MIN_SPEED = 10; public abstract void moveRight(int steps); public abstract void moveLeft(int steps); public abstract void moveUp(int steps); public abstract void moveDown(int steps); }
  • 9. © 1996-2017 All Rights Reserved. Default Methods  As of Java 8 the interface we define can include the definition of methods together with their implementation.  This implementation is known as a default one that will take place when the class that implements the interface doesn't include its own specific implementation. lifemichael
  • 10. © 1996-2017 All Rights Reserved. Default Methods lifemichael public interface IRobot { public final static int MAX_SPEED = 100; public final static int MIN_SPEED = 10; public abstract void moveRight(int steps); public abstract void moveLeft(int steps); public abstract void moveUp(int steps); public abstract void moveDown(int steps); public default void turnAround() { moveRight(1); moveRight(1); moveRight(1); moveRight(1); } }
  • 11. © 1996-2017 All Rights Reserved. Default Methods  When extending an interface that contains a default method, we can let the extended interface inherit the default method, we can redeclare the default method as an abstract method and we can even redefine it with a new implementation. lifemichael
  • 12. © 1996-2017 All Rights Reserved. Static Methods  The interface we define can include the definition of static methods. lifemichael
  • 13. © 1996-2017 All Rights Reserved. Static Methods lifemichael public interface IRobot { public static String getIRobotSpecificationDetails() { return "spec..."; } public final static int MAX_SPEED = 100; public final static int MIN_SPEED = 10; public abstract void moveRight(int steps); public abstract void moveLeft(int steps); public abstract void moveUp(int steps); public abstract void moveDown(int steps); public default void turnAround() { moveRight(1); moveRight(1); moveRight(1); moveRight(1); } }
  • 14. © 1996-2017 All Rights Reserved. Nested Types  The interface we define can include the definition of new static inner types.  The inner types we define will be implicitly static ones. lifemichael
  • 15. © 1996-2017 All Rights Reserved. Nested Types lifemichael public interface IRobot { public static String getIRobotSpecificationDetails() { return "spec..."; } public final static int MAX_SPEED = 100; public final static int MIN_SPEED = 10; public abstract void moveRight(int steps); public abstract void moveLeft(int steps); public abstract void moveUp(int steps); public abstract void moveDown(int steps); public default void turnAround() { moveRight(1); moveRight(1); moveRight(1); moveRight(1); } public static class Engine { public void doSomething() { getIRobotSpecificationDetails(); } } }
  • 16. © 1996-2017 All Rights Reserved. Nested Types  We can find an interesting example for defining abstract inner type inside an interface when developing a remote service on the android platform. lifemichael
  • 17. © 1996-2017 All Rights Reserved. Nested Types lifemichael public interface ICurrencyService extends android.os.IInterface { public static abstract class Stub extends android.os.Binder implements ICurrencyService { } public double getCurrency(java.lang.String currencyName) throws android.os.RemoteException; }
  • 18. © 1996-2017 All Rights Reserved. Private Methods  As of Java 9, the interface we define can include the definition of private methods.  Highly useful when having more than one default methods that share parts of their implementations. We can place the common parts in separated private methods and make our code shorter. lifemichael
  • 19. © 1996-2017 All Rights Reserved. Private Methods lifemichael package com.lifemichael; public interface IRobot { ... public abstract void moveDown(int steps); public default void turnAround() { moveRight(1); step(); moveRight(1); step(); ... } private void step(){ //... } }
  • 20. © 1996-2017 All Rights Reserved. Interfaces as APIs  Companies and organizations that develop software to be used by other developers use the interface as API.  While the interface is made public, its implementation is kept as a closely guarded secret and it can be revises at a later date as long as it continues to implement the original interface. lifemichael
  • 21. © 1996-2017 All Rights Reserved. Interfaces as Types  The new defined interface is actually a new type we can use whenever we define a variable or a function parameter.  Using the interface name as a type adds more flexibility to our code. lifemichael
  • 22. © 1996-2017 All Rights Reserved. Evolving Interfaces  When we want to add more abstract methods to an interface that was already defined, all classes that implement the old interface will break because they no longer implement the old interface. Developers relying on our interface will protest. lifemichael
  • 23. © 1996-2017 All Rights Reserved. Evolving Interfaces  We can either define a new interface that extend the one we already have, or define the new methods as default ones.  The default methods allow us to add new functionality to interfaces we already defined, while keeping the binary compatibility with code written for older versions of those interfaces. lifemichael
  • 24. © 1996-2017 All Rights Reserved. Interface as a Dictating Tool  We can use interfaces for dictating specific requirements from other developers for using functions we developed. lifemichael
  • 25. © 1996-2017 All Rights Reserved. Interfaces as Traits  Using the default methods we can develop interfaces that will be used as traits in order to reduce the amount of code in our project. lifemichael Car SportCar RacingCar Bag SportiveBag ElegantBag Sportive
  • 26. © 1996-2017 All Rights Reserved. lifemichael Questions & Answers If you enjoyed my lecture please leave me a comment at http://speakerpedia.com/speakers/life-michael. Thanks for your time! Haim.