Java Interfaces
Download
Report
Transcript Java Interfaces
Interfaces
Interfaces
• An interface in the Java programming
language is an abstract type that is used to
specify an interface (in the generic sense of
the term) that classes must implement.
Interfaces are declared using the interface
keyword and may only contain method
signatures and constant declarations (variable
declarations which are declared to be both
static and final).
Java Interfaces
– a java interface is just a collection of abstract
methods (i.e. we state the signatures, but not the
bodies)
states that
this is an
interface,
not a class
3
public interface MyStack {
public int size();
public boolean isEmpty();
public Object top();
public void push(Object elt);
public Object pop();
}
note no
bodies for
the methods
Continued
• As interfaces are implicitly abstract, they cannot
be directly instantiated. Object references in Java
may be specified to be of an interface type; in
which case they must either be null, or be bound
to an object which implements the interface.
• The keyword implements is used to declare that a
given class implements an interface. A class
which implements an interface must either
implement all methods in the interface, or be an
abstract class
Advantages of Interfaces
• Provide a standard set of methods for a group of classes.
– This is Java’s implementation of an Abstract Data Type (ADT)
• Objects of these classes can be accessed by the standard
set of methods without considering about their location in
class hierarchy.
• Support selective multiple inheritance.
• e.g. java.util.LinkedList
Benefit
• One benefit of using interfaces is that they
simulate multiple inheritance. All classes in
Java (other than java.lang.Object, the root
class of the Java type system) must have
exactly one base class; multiple inheritance of
classes is not allowed. However, a Java
class/interface may implement/extend any
number of interfaces.
Data Abstraction
– if the interface remains the same, clients don't need
to be changed, even if the implementation behind
the interface changes
public class Time {
private int timeInSecs;
//public methods
public class Time {
private int hours;
private int minutes
private int secs;
}
//same public methods
//but with different
//bodies
}
7
Defining an interface
• Interfaces are defined with the following
syntax
• [visibility] interface InterfaceName [extends
other interfaces]
• { constant declarations
• member type declarations
• abstract method declarations }
Abstract methods
• The body of the interface contains abstract
methods, but since all methods in an interface
are, by definition, abstract, the abstract
keyword is not required. Since the interface
specifies a set of exposed behaviours, all
methods are implicitly public.
Example
• public interface Predator {
• boolean chasePrey(Prey p);
• void eatPrey(Prey p); }
Implements
• Classes may implement an interface. For
example
• public class Cat implements Predator {
• public boolean chasePrey(Prey p) { //
programming to chase prey p (specifically for a
cat) }
• public void eatPrey (Prey p) { // programming
to eat prey p (specifically for a cat) } }
Abstract
• If a class implements an interface and does
not implement all its methods, it must be
marked as abstract.
• If a class is abstract, one of its subclasses is
expected to implement its unimplemented
methods.
Multiple Interfaces
• Classes can implement multiple interfaces
• public class Frog implements Predator, Prey {
... }
Example of when Interfaces apply
• Games Console
• Interface required for Button Press
• This is supplied by Device Driver
• Need to do Different things in response to
button press from Game to Game
interfaces vs classes
– a class definition can contain instance/class variables
and instance/class methods, including both the
signature and the body
– a java interface contains only the signatures of public
instance methods (and named constants)
– a java interface acts like a specification
• it says what it means to be e.g. a stack
• to be a stack, an object must offer at least those methods in
its public interface
16
using Java interfaces
– Java allows us to tell the compiler that a class will
implement an interface
– regard it as a contract stating that you will meet the
specification
– any class that implements an interface must provide
implementations of the public methods (or it must be
abstract, and its subclasses provide them)
– the compiler will check, and if the bodies are not
provided, it won't compile
17
Summary
– An interface is like an abstract class - it specifies what
its methods should look like, but gives no body
– if a class implements an interface, it is equivalent to
signing a contract saying that it will provide a body for
the specified method - Java will not compile the
program unless we provide the method definition
– we can refer to an object as though it were an object
of the interface, and invoke the interface methods
18
Java Interfaces
• Identify a common set of methods for a group
of classes implementing the interface
• Share constants (static and final) between
classes.
• See examples.
Interface Example - Shape
public interface Shape
{
double PI = 3.1425926; //static and final constant.
void draw();
void resize();
}
Shape
draw()
resize()
implements
Circle
Rectangle
draw()
resize()
draw()
resize()
public class Circle implements Shape
{
public void draw() { /* draw a circle*/ }
public void resize() { /* draw a circle*/ }
}
public class Rectangle implements Shape
{
public void draw() { /* draw a circle*/ }
public void resize() { /* draw a circle*/ }
}
Interface Example – Generator
• Goal
– Develop several implementations of an object, i.e.
generator, that generate different sequence of integers,
such as
• constants - ConstantGenerator
• random integers – RandomGenerator
• primes – PrimeGenerator
• Interface Generator
Simple Example
• Interface
• public interface IntExample1{
• public void sayHello(); }
•
•
•
•
Class which implements it
public class JavaInterfaceExample
implements IntExample1{
public void sayHello(){
System.out.println("Hello Visitor
!"); }
Control Program
•
•
public static void main(String args[]){
JavaInterfaceExample JavaIExample = new JavaInterfaceExample();
JavaIExample.sayHello(); }}
Simple Example
• Interface
• public interface IntExample1{
• public void sayHello(); }
•
Class which implements it 2
• public class
JavaInterfaceExample1
implements IntExample1{
•
public void sayHello(){
•
System.out.println("HI there
Visitor !"); }
Control Program
•
•
•
public static void main(String args[]){
JavaInterfaceExample1 JavaIExample = new JavaInterfaceExample1();
JavaIExample.sayHello(); }}