Your SlideShare is downloading. ×
Chapter 09
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Chapter 09

79
views

Published on

Published in: Technology, Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
79
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Interfaces Chapter 9
  • 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.