SlideShare una empresa de Scribd logo
1 de 11
Interfaces
Chapter 9
9
Creating Interfaces
An interface is a contract.
Every class that implements the interface
must provide the interface’s defined
methods.
Each class implements the methods
however it sees fit.
A class can implement multiple
interfaces.
9
Declaring the Interface
Similar to a class declaration but uses the
interface keyword
The extends keyword can be used to
extend interfaces.
9
Interface Restrictions
Interfaces:
Cannot have member fields
Can define constants
Cannot have methods with
implementation; all methods in an
interface are implicitly abstract
Cannot be instantiated
Cannot define constructors
9
Implementing Multiple Interfaces:
The Debuggable Interface
Debugging is an important step in the
programming cycle.
One way to debug a program is to display
information about objects and variables
to ensure their validity.
9
Debugging an Interface
In the ball program, seeing information
about each of the objects as the
program runs is helpful.
Ball and Wall classes do not derive from
the same base class (Java does not
support multiple inheritance).
Ball and Wall can implement common
interfaces.
9
The Debuggable Interface
The Debuggable interface defines two
public methods:
displayStatus(String identifier);
displayError(String error);
The interface does not define how to
implement these methods.
Implementation details are left to the
classes.
9
Interfaces vs. Abstract Classes
Use an abstract base class if:
You are trying to create an is a
relationship:
A tree is a plant
A fly is an insect
You do not want to instantiate the base
class.
9
Interfaces vs. Abstract Classes
Use an interface if:
You are not trying to create an is a
relationship.
You are stating that your class has these
capabilities.
A Ball and a Wall have the capability of being
colorable and debuggable.
You need a way to handle multiple
inheritance.
9
Extending Interfaces
Interfaces can be extended just like
classes.
Allows the programmer to provide new
functionality without rewriting existing
code
Use the keyword extends: interface
DebugLogging extends Debuggable
9
Polymorphic Interfaces
Interfaces can be treated
polymorphically, as a type.
A method that accepts an object which
implements the Debuggable interface will
also accept any object which implements
any interface derived from the
Debuggable interface.

Más contenido relacionado

La actualidad más candente

Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Kostja Osipov
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
ilakkiya
 

La actualidad más candente (20)

15 interfaces
15   interfaces15   interfaces
15 interfaces
 
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
Good coding-style, a talk made in 2008 to encourage changes in MySQL coding s...
 
Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)Advanced Programming _Abstract Classes vs Interfaces (Java)
Advanced Programming _Abstract Classes vs Interfaces (Java)
 
Bt0082, visual basic
Bt0082, visual basicBt0082, visual basic
Bt0082, visual basic
 
Lab13
Lab13Lab13
Lab13
 
Behavioral design patterns presentation
Behavioral design patterns presentationBehavioral design patterns presentation
Behavioral design patterns presentation
 
Total oop in c# dot net
Total oop in c# dot netTotal oop in c# dot net
Total oop in c# dot net
 
Analyzing Java Program
Analyzing Java Program Analyzing Java Program
Analyzing Java Program
 
Object Calisthenics in Objective-C
Object Calisthenics in Objective-CObject Calisthenics in Objective-C
Object Calisthenics in Objective-C
 
Design dimensions of DSLs
Design dimensions of DSLsDesign dimensions of DSLs
Design dimensions of DSLs
 
computer programming and utilization
computer programming and utilization computer programming and utilization
computer programming and utilization
 
Java ocjp level_2
Java ocjp level_2Java ocjp level_2
Java ocjp level_2
 
JetBrains MPS: Typesystem Aspect
JetBrains MPS: Typesystem AspectJetBrains MPS: Typesystem Aspect
JetBrains MPS: Typesystem Aspect
 
Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
From Good to SOLID: How to become a better PHP developer
From Good to SOLID: How to become a better PHP developerFrom Good to SOLID: How to become a better PHP developer
From Good to SOLID: How to become a better PHP developer
 
13. interfaces
13. interfaces13. interfaces
13. interfaces
 
Projectional editing
Projectional editingProjectional editing
Projectional editing
 
Clean Code, Resumen Capítulo 1.
Clean Code, Resumen Capítulo 1. Clean Code, Resumen Capítulo 1.
Clean Code, Resumen Capítulo 1.
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 

Destacado (6)

Chapter 01
Chapter 01Chapter 01
Chapter 01
 
Chapter 10
Chapter 10Chapter 10
Chapter 10
 
Chapter 04
Chapter 04Chapter 04
Chapter 04
 
Idea's1 only
Idea's1 onlyIdea's1 only
Idea's1 only
 
Challenges
ChallengesChallenges
Challenges
 
Managing A Start Up And Ethics M5
Managing A Start Up And Ethics M5Managing A Start Up And Ethics M5
Managing A Start Up And Ethics M5
 

Similar a Chapter 09

Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
AdilAijaz3
 

Similar a Chapter 09 (20)

C# interview
C# interviewC# interview
C# interview
 
Lecture 18
Lecture 18Lecture 18
Lecture 18
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
Interface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar SinghInterface in java By Dheeraj Kumar Singh
Interface in java By Dheeraj Kumar Singh
 
Java interface
Java interfaceJava interface
Java interface
 
OOFeatures_revised-2.pptx
OOFeatures_revised-2.pptxOOFeatures_revised-2.pptx
OOFeatures_revised-2.pptx
 
C# interview questions
C# interview questionsC# interview questions
C# interview questions
 
C#
C#C#
C#
 
Inheritance
InheritanceInheritance
Inheritance
 
Design patterns in brief
Design patterns in briefDesign patterns in brief
Design patterns in brief
 
C sharp
C sharpC sharp
C sharp
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
OOP interview questions & answers.
OOP interview questions & answers.OOP interview questions & answers.
OOP interview questions & answers.
 
Interface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementationInterface in java ,multiple inheritance in java, interface implementation
Interface in java ,multiple inheritance in java, interface implementation
 
Java 06
Java 06Java 06
Java 06
 
Java interview questions
Java interview questionsJava interview questions
Java interview questions
 
Dtacs
DtacsDtacs
Dtacs
 
Lecture 5 interface.pdf
Lecture  5 interface.pdfLecture  5 interface.pdf
Lecture 5 interface.pdf
 
04 inheritance
04 inheritance04 inheritance
04 inheritance
 
C#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developersC#.net interview questions for dynamics 365 ce crm developers
C#.net interview questions for dynamics 365 ce crm developers
 

Más de Graham Royce (20)

Chapter 18
Chapter 18Chapter 18
Chapter 18
 
Chapter 17
Chapter 17Chapter 17
Chapter 17
 
Chapter 16
Chapter 16Chapter 16
Chapter 16
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
Chapter 14
Chapter 14Chapter 14
Chapter 14
 
Chapter 13
Chapter 13Chapter 13
Chapter 13
 
Chapter 12
Chapter 12Chapter 12
Chapter 12
 
Chapter 11
Chapter 11Chapter 11
Chapter 11
 
Chapter 08
Chapter 08Chapter 08
Chapter 08
 
Chapter 07
Chapter 07Chapter 07
Chapter 07
 
Chapter 06
Chapter 06Chapter 06
Chapter 06
 
Chapter 05
Chapter 05Chapter 05
Chapter 05
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Chapter 02
Chapter 02Chapter 02
Chapter 02
 
13 java in oracle
13 java in oracle13 java in oracle
13 java in oracle
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
My 3 min pitch pack
My 3 min pitch packMy 3 min pitch pack
My 3 min pitch pack
 
Things to do
Things to doThings to do
Things to do
 
House in order
House in orderHouse in order
House in order
 
Blank 10 all you need
Blank 10 all you needBlank 10 all you need
Blank 10 all you need
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

Chapter 09

  • 2. 9 Creating Interfaces An interface is a contract. Every class that implements the interface must provide the interface’s defined methods. Each class implements the methods however it sees fit. A class can implement multiple interfaces.
  • 3. 9 Declaring the Interface Similar to a class declaration but uses the interface keyword The extends keyword can be used to extend interfaces.
  • 4. 9 Interface Restrictions Interfaces: Cannot have member fields Can define constants Cannot have methods with implementation; all methods in an interface are implicitly abstract Cannot be instantiated Cannot define constructors
  • 5. 9 Implementing Multiple Interfaces: The Debuggable Interface Debugging is an important step in the programming cycle. One way to debug a program is to display information about objects and variables to ensure their validity.
  • 6. 9 Debugging an Interface In the ball program, seeing information about each of the objects as the program runs is helpful. Ball and Wall classes do not derive from the same base class (Java does not support multiple inheritance). Ball and Wall can implement common interfaces.
  • 7. 9 The Debuggable Interface The Debuggable interface defines two public methods: displayStatus(String identifier); displayError(String error); The interface does not define how to implement these methods. Implementation details are left to the classes.
  • 8. 9 Interfaces vs. Abstract Classes Use an abstract base class if: You are trying to create an is a relationship: A tree is a plant A fly is an insect You do not want to instantiate the base class.
  • 9. 9 Interfaces vs. Abstract Classes Use an interface if: You are not trying to create an is a relationship. You are stating that your class has these capabilities. A Ball and a Wall have the capability of being colorable and debuggable. You need a way to handle multiple inheritance.
  • 10. 9 Extending Interfaces Interfaces can be extended just like classes. Allows the programmer to provide new functionality without rewriting existing code Use the keyword extends: interface DebugLogging extends Debuggable
  • 11. 9 Polymorphic Interfaces Interfaces can be treated polymorphically, as a type. A method that accepts an object which implements the Debuggable interface will also accept any object which implements any interface derived from the Debuggable interface.