SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
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.

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java IO
Java IOJava IO
Java IO
 
JDBC
JDBCJDBC
JDBC
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
C# Constructors
C# ConstructorsC# Constructors
C# Constructors
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Polymorphism in java
Polymorphism in javaPolymorphism in java
Polymorphism in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Hibernate
Hibernate Hibernate
Hibernate
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
Packages,static,this keyword in java
Packages,static,this keyword in javaPackages,static,this keyword in java
Packages,static,this keyword in java
 
Java interface
Java interfaceJava interface
Java interface
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Files in java
Files in javaFiles in java
Files in java
 
Spring Core
Spring CoreSpring Core
Spring Core
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Java I/O
Java I/OJava I/O
Java I/O
 

Similar a Java 8 Default Methods

Similar 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
 

Más de Haim Michael

Más 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

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 

Último (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With SimplicityWSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
WSO2Con2024 - Enabling Transactional System's Exponential Growth With Simplicity
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
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 🔝✔️✔️
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

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.