SlideShare a Scribd company logo
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

What's hot

Introduction to java
Introduction to javaIntroduction to java
Introduction to java
Ajay Sharma
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular ExpressionMasudul Haque
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Rajat Busheheri
 
Method overriding
Method overridingMethod overriding
Method overriding
Azaz Maverick
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
Java2Blog
 
Spring Core
Spring CoreSpring Core
Spring Core
Pushan Bhattacharya
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
Jeevesh Pandey
 
Wrapper class
Wrapper classWrapper class
Wrapper class
kamal kotecha
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
Victer Paul
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Wrapper classes
Wrapper classes Wrapper classes
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
Madishetty Prathibha
 
Java interface
Java interfaceJava interface
Java interface
Md. Tanvir Hossain
 
Interface
InterfaceInterface
Interface
kamal kotecha
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Exception Handling
Exception HandlingException Handling
Exception Handling
Reddhi Basu
 

What's hot (20)

Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Java: Regular Expression
Java: Regular ExpressionJava: Regular Expression
Java: Regular Expression
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Method overriding
Method overridingMethod overriding
Method overriding
 
Polymorphism in Java
Polymorphism in JavaPolymorphism in Java
Polymorphism in Java
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Java - Exception Handling Concepts
Java - Exception Handling ConceptsJava - Exception Handling Concepts
Java - Exception Handling Concepts
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Java interface
Java interfaceJava interface
Java interface
 
Interface
InterfaceInterface
Interface
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 

Similar to Java 8 Default Methods

Functional programming in Java
Functional programming in Java  Functional programming in Java
Functional programming in Java
Haim Michael
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
Haim Michael
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106
Jim Shingler
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Haim Michael
 
Advanced topics in TypeScript
Advanced topics in TypeScriptAdvanced topics in TypeScript
Advanced topics in TypeScript
Haim Michael
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
Savio Sebastian
 
D-CAST Real Life TestOps Environment
D-CAST Real Life TestOps EnvironmentD-CAST Real Life TestOps Environment
D-CAST Real Life TestOps Environment
Adam Sandman
 
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Edureka!
 
Ongoing management of your PHP 7 application
Ongoing management of your PHP 7 applicationOngoing management of your PHP 7 application
Ongoing management of your PHP 7 application
Zend by Rogue Wave Software
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
Amin Shahnazari
 
Designing, Implementing, and Using Reactive APIs
Designing, Implementing, and Using Reactive APIsDesigning, Implementing, and Using Reactive APIs
Designing, Implementing, and Using Reactive APIs
VMware Tanzu
 
Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
Uchiha Shahin
 
Building interactive app
Building interactive appBuilding interactive app
Building interactive app
Omar Albelbaisy
 
Java 8 - Gateway Drug or End of Line?
Java 8 - Gateway Drug or End of Line?Java 8 - Gateway Drug or End of Line?
Java 8 - Gateway Drug or End of Line?
Garth Gilmour
 
Streamline Devops workflows
Streamline Devops workflows Streamline Devops workflows
Streamline Devops workflows
TREEPTIK
 
DevOps D-Day - Streamline DevOps workflows with APIs
DevOps D-Day - Streamline DevOps workflows with APIsDevOps D-Day - Streamline DevOps workflows with APIs
DevOps D-Day - Streamline DevOps workflows with APIs
Jerome Louvel
 
DevOps DDay - Streamline DevOps Workflows With APIs
DevOps DDay - Streamline DevOps Workflows With APIsDevOps DDay - Streamline DevOps Workflows With APIs
DevOps DDay - Streamline DevOps Workflows With APIs
Restlet
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test Automation
Rahul Verma
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test Automation
Rahul Verma
 
Java 8 Streams - in Depth
Java 8 Streams - in DepthJava 8 Streams - in Depth
Java 8 Streams - in Depth
Haim Michael
 

Similar to Java 8 Default Methods (20)

Functional programming in Java
Functional programming in Java  Functional programming in Java
Functional programming in Java
 
PHP7. Game Changer.
PHP7. Game Changer. PHP7. Game Changer.
PHP7. Game Changer.
 
Gg Code Mash2009 20090106
Gg Code Mash2009 20090106Gg Code Mash2009 20090106
Gg Code Mash2009 20090106
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Advanced topics in TypeScript
Advanced topics in TypeScriptAdvanced topics in TypeScript
Advanced topics in TypeScript
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
D-CAST Real Life TestOps Environment
D-CAST Real Life TestOps EnvironmentD-CAST Real Life TestOps Environment
D-CAST Real Life TestOps Environment
 
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
Django Rest Framework | How to Create a RESTful API Using Django | Django Tut...
 
Ongoing management of your PHP 7 application
Ongoing management of your PHP 7 applicationOngoing management of your PHP 7 application
Ongoing management of your PHP 7 application
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented Design
 
Designing, Implementing, and Using Reactive APIs
Designing, Implementing, and Using Reactive APIsDesigning, Implementing, and Using Reactive APIs
Designing, Implementing, and Using Reactive APIs
 
Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
 
Building interactive app
Building interactive appBuilding interactive app
Building interactive app
 
Java 8 - Gateway Drug or End of Line?
Java 8 - Gateway Drug or End of Line?Java 8 - Gateway Drug or End of Line?
Java 8 - Gateway Drug or End of Line?
 
Streamline Devops workflows
Streamline Devops workflows Streamline Devops workflows
Streamline Devops workflows
 
DevOps D-Day - Streamline DevOps workflows with APIs
DevOps D-Day - Streamline DevOps workflows with APIsDevOps D-Day - Streamline DevOps workflows with APIs
DevOps D-Day - Streamline DevOps workflows with APIs
 
DevOps DDay - Streamline DevOps Workflows With APIs
DevOps DDay - Streamline DevOps Workflows With APIsDevOps DDay - Streamline DevOps Workflows With APIs
DevOps DDay - Streamline DevOps Workflows With APIs
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test Automation
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test Automation
 
Java 8 Streams - in Depth
Java 8 Streams - in DepthJava 8 Streams - in Depth
Java 8 Streams - in Depth
 

More from Haim Michael

Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Haim Michael
 
Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
Haim Michael
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
Haim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
Haim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
Haim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
Haim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
Haim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
Haim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
Haim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
Haim Michael
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
Haim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
Haim Michael
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
Haim Michael
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
Haim Michael
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
Haim Michael
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
Haim Michael
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
Haim Michael
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
Haim Michael
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
Haim Michael
 

More from Haim Michael (20)

Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)Kotlin Jump Start Online Free Meetup (June 4th, 2024)
Kotlin Jump Start Online Free Meetup (June 4th, 2024)
 
Anti Patterns
Anti PatternsAnti Patterns
Anti Patterns
 
Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in Java
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design Patterns
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL Injections
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in Java
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design Patterns
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in Python
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScript
 
Java Jump Start
Java Jump StartJava Jump Start
Java Jump Start
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump Start
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHP
 
What is new in Python 3.9
What is new in Python 3.9What is new in Python 3.9
What is new in Python 3.9
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on Steroid
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib Library
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728
 

Recently uploaded

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 

Recently uploaded (20)

Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 

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.