SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
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.

Mais conteúdo relacionado

Mais procurados

Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in javaRamakrishna Joshi
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsNewCircle Training
 
Collections In Java
Collections In JavaCollections In Java
Collections In JavaBinoj T E
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued Hitesh-Java
 
Java collections concept
Java collections conceptJava collections concept
Java collections conceptkumar gaurav
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in JavaSonya Akter Rupa
 
Java Presentation
Java PresentationJava Presentation
Java Presentationpm2214
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java Janu Jahnavi
 

Mais procurados (20)

Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
How Hashmap works internally in java
How Hashmap works internally  in javaHow Hashmap works internally  in java
How Hashmap works internally in java
 
Java collections
Java collectionsJava collections
Java collections
 
Java 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & StreamsJava 8 Lambda Expressions & Streams
Java 8 Lambda Expressions & Streams
 
Collections In Java
Collections In JavaCollections In Java
Collections In Java
 
Java IO
Java IOJava IO
Java IO
 
Php array
Php arrayPhp array
Php array
 
Java 8 lambda
Java 8 lambdaJava 8 lambda
Java 8 lambda
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
OOP with Java - Continued
OOP with Java - Continued OOP with Java - Continued
OOP with Java - Continued
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
Java 8 features
Java 8 featuresJava 8 features
Java 8 features
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Polymorphism in java
Polymorphism in java Polymorphism in java
Polymorphism in java
 

Semelhante a 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 20090106Jim Shingler
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda ExpressionsHaim Michael
 
Advanced topics in TypeScript
Advanced topics in TypeScriptAdvanced topics in TypeScript
Advanced topics in TypeScriptHaim Michael
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Savio Sebastian
 
D-CAST Real Life TestOps Environment
D-CAST Real Life TestOps EnvironmentD-CAST Real Life TestOps Environment
D-CAST Real Life TestOps EnvironmentAdam 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!
 
Introduction to Object oriented Design
Introduction to Object oriented DesignIntroduction to Object oriented Design
Introduction to Object oriented DesignAmin Shahnazari
 
Designing, Implementing, and Using Reactive APIs
Designing, Implementing, and Using Reactive APIsDesigning, Implementing, and Using Reactive APIs
Designing, Implementing, and Using Reactive APIsVMware 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 appOmar 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
 
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 APIsRestlet
 
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 APIsJerome Louvel
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test AutomationRahul Verma
 
Meaningful UI Test Automation
Meaningful UI Test AutomationMeaningful UI Test Automation
Meaningful UI Test AutomationRahul Verma
 
Java 8 Streams - in Depth
Java 8 Streams - in DepthJava 8 Streams - in Depth
Java 8 Streams - in DepthHaim Michael
 

Semelhante a 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?
 
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
 
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
 
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
 

Mais de Haim Michael

Virtual Threads in Java
Virtual Threads in JavaVirtual Threads in Java
Virtual Threads in JavaHaim Michael
 
MongoDB Design Patterns
MongoDB Design PatternsMongoDB Design Patterns
MongoDB Design PatternsHaim Michael
 
Introduction to SQL Injections
Introduction to SQL InjectionsIntroduction to SQL Injections
Introduction to SQL InjectionsHaim Michael
 
Record Classes in Java
Record Classes in JavaRecord Classes in Java
Record Classes in JavaHaim Michael
 
Microservices Design Patterns
Microservices Design PatternsMicroservices Design Patterns
Microservices Design PatternsHaim Michael
 
Structural Pattern Matching in Python
Structural Pattern Matching in PythonStructural Pattern Matching in Python
Structural Pattern Matching in PythonHaim Michael
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in PythonHaim Michael
 
OOP Best Practices in JavaScript
OOP Best Practices in JavaScriptOOP Best Practices in JavaScript
OOP Best Practices in JavaScriptHaim Michael
 
JavaScript Jump Start 20220214
JavaScript Jump Start 20220214JavaScript Jump Start 20220214
JavaScript Jump Start 20220214Haim Michael
 
Bootstrap Jump Start
Bootstrap Jump StartBootstrap Jump Start
Bootstrap Jump StartHaim Michael
 
What is new in PHP
What is new in PHPWhat is new in PHP
What is new in PHPHaim 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.9Haim Michael
 
Programming in Python on Steroid
Programming in Python on SteroidProgramming in Python on Steroid
Programming in Python on SteroidHaim Michael
 
The matplotlib Library
The matplotlib LibraryThe matplotlib Library
The matplotlib LibraryHaim Michael
 
Pandas meetup 20200908
Pandas meetup 20200908Pandas meetup 20200908
Pandas meetup 20200908Haim Michael
 
The num py_library_20200818
The num py_library_20200818The num py_library_20200818
The num py_library_20200818Haim Michael
 
Jupyter notebook 20200728
Jupyter notebook 20200728Jupyter notebook 20200728
Jupyter notebook 20200728Haim Michael
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Haim Michael
 

Mais de Haim Michael (20)

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
 
Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start) Node.js Crash Course (Jump Start)
Node.js Crash Course (Jump Start)
 

Último

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 

Último (20)

A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 

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.