Interfaces Updated
Interfaces Updated
Interfaces
Abstract Class
• A class that cannot instantiate objects.
Message
To Interface
What is an Interface?
5
What is an Interface?
• A child class that extends a parent class can
also implement an interface to gain some
additional behavior
• Implementing an interface is a “promise” to
include the specified method(s)
• A method in an interface cannot be made
private
6
Example
8
Declaring Constants with Interfaces
• Interfaces can be used to declare constants
used in many class declarations
– These constants are implicitly public, static
and final
9
Creating and Using Interfaces
• Declaration begins with interface keyword
• Classes implement an interface (and its
methods)
• Contains public abstract methods
– Classes (that implement the interface) must
implement these methods
10
Interface Body
• The interface body contains method
declarations for ALL the methods included in
the interface.
• A method declaration within an interface is
followed by a semicolon (;) because an
interface does not provide implementations
for the methods declared within it.
• All methods declared in an interface are
implicitly public and abstract.
14
Why Use Interfaces
• Provide capability for unrelated classes to
implement a set of common methods
• Define and standardize ways people and
systems can interact
• Interface specifies what operations must be
permitted
• Does not specify how performed
15
Case Study: A Payable Hierarchy
• Payable interface
– Contains method getPaymentAmount
– Is implemented by the Invoice and Employee
classes
16
Abstract classes & Interface 07/08/202 17
4
Derived Interfaces
• Like classes, an interface may be derived from a base
interface
– This is called extending the interface
– The derived interface must include the phrase
extends BaseInterfaceName
• A concrete class that implements a derived interface
must have definitions for any methods in the derived
interface as well as any methods in the base interface
Class ABC