Chapter 13
Interfaces
Abstract Class
• A class that cannot instantiate objects.
Message
Text Voice Fax
Message Message Message
Public abstract class Message {
}
11/19/202
2 4 Abstract classes & Interface
Abstract Class
• An abstract class used as supertype
• An object cannot be created from an abstract
class
• An array of the abstract type is used to contain
objects of the concrete subclasses
11/19/202
3 4 Abstract classes & Interface
From abstract class
To Interface
What is an Interface?
• An interface is a collection of constants and
method declarations
• An interface describes a set of methods that can
be called on an object
• The method declarations do not include an
implementation
– there is no method body
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
11/19/202
7 4 Abstract classes & Interface
When A Class Definition
Implements An Interface:
• It must implement each method in the
interface
• Each method must be public (even though the
interface might not say so)
• Constants from the interface can be used as if
they had been defined in the class (They
should not be re-defined in the class)
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.
11/19/202
11 4 Abstract classes & Interface
Implement an Interface
• An interface defines a protocol of behavior.
• A class that implements an interface adheres
to the protocol defined by that interface.
• To declare a class that implements an
interface, include an implements clause in the
class declaration.
11/19/202
12 4 Abstract classes & Interface
Rules
11/19/202
13 4 Abstract classes & Interface
Why Use Interfaces
• Java has single inheritance, only
• This means that a child class inherits from only
one parent class
• Sometimes multiple inheritance would be
convenient
• Interfaces give Java some of the advantages of
multiple inheritance without incurring the
disadvantages
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
11/19/202
17 4 Abstract classes & Interface
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
Copyright © 2012 Pearson
13-18 Addison-Wesley. All rights
reserved.
Extending an Interface
Copyright © 2012 Pearson
13-19 Addison-Wesley. All rights
reserved.
Multiple Inheritance
Class A Class B Class C
Class ABC
Class ABC inherits all variables and methods from
Class A, Class B, and Class C.
Java does NOT support multiple inheritances.
However, you can use interface to implement the
functionality of multiple inheritance.
11/19/202
Abstract classes & Interface 4 20
The Comparable Interface
• The Comparable interface is in the [Link]
package, and so is automatically available to any
program
• It has only the following method heading that must
be implemented:
public int compareTo(Object other);
• It is the programmer's responsibility to follow the
semantics of the Comparable interface when
implementing it
Copyright © 2012 Pearson
13-21 Addison-Wesley. All rights
reserved.
The Comparable Interface Semantics
• The method compareTo must return
– A negative number if the calling object "comes before" the
parameter other
– A zero if the calling object "equals" the parameter other
– A positive number if the calling object "comes after" the
parameter other
• If the parameter other is not of the same type as
the class being defined, then a
ClassCastException should be thrown
Copyright © 2012 Pearson
13-22 Addison-Wesley. All rights
reserved.
The Comparable Interface Semantics
• Almost any reasonable notion of "comes
before" is acceptable
– In particular, all of the standard less-than relations
on numbers and lexicographic ordering on strings
are suitable
• The relationship "comes after" is just the
reverse of "comes before"
Copyright © 2012 Pearson
13-23 Addison-Wesley. All rights
reserved.
The Serializable Interface
• An extreme but commonly used example of
an interface is the Serializable interface
– It has no method headings and no defined
constants: It is completely empty
– It is used merely as a type tag that indicates to the
system that it may implement file I/O in a
particular way
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 13-24
The Cloneable Interface
• The Cloneable interface is another unusual
example of a Java interface
– It does not contain method headings or defined
constants
– It is used to indicate how the method clone
(inherited from the Object class) should be used
and redefined
Copyright © 2012 Pearson
13-25 Addison-Wesley. All rights
reserved.
The Cloneable Interface
• The method [Link]() does a bit-by-
bit copy of the object's data in storage
• If the data is all primitive type data or data of
immutable class types (such as String), then
this is adequate
– This is the simple case
• The following is an example of a simple class
that has no instance variables of a mutable class
type, and no specified base class
– So the base class is Object
Copyright © 2012 Pearson
13-26 Addison-Wesley. All rights
reserved.
Implementation of the Method clone:
Simple Case
Copyright © 2012 Pearson
13-27 Addison-Wesley. All rights
reserved.
Example
import [Link]; public class Test {
class A implements Cloneable public static void main(String[] args)
{ throws CloneNotSupportedException
{
int i; A a = new A(20,
String s; "GeeksForGeeks");
// A class constructor
public A(int i, String s) A b = (A)[Link]();
{ [Link](b.i);
[Link](b.s);
this.i = i; this.s = s; }
} }
// Overriding clone() method by simply
//calling Object class clone() method.
@Override
protected Object clone() throws
CloneNotSupportedException
{
return [Link]();
} 13-28
}
The Cloneable Interface
• If the data in the object to be cloned includes instance
variables whose type is a mutable class, then the simple
implementation of clone would cause a privacy leak
• When implementing the Cloneable interface for a
class like this:
– First invoke the clone method of the base class Object (or
whatever the base class is)
– Then reset the values of any new instance variables whose
types are mutable class types
– This is done by making copies of the instance variables by
invoking their clone methods
Copyright © 2012 Pearson
13-29 Addison-Wesley. All rights
reserved.
The Cloneable Interface
• Note that this will work properly only if
the Cloneable interface is
implemented properly for the classes to
which the instance variables belong
– And for the classes to which any of the
instance variables of the above classes
belong, and so on and so forth
• The following shows an example
Copyright © 2012 Pearson
13-30 Addison-Wesley. All rights
reserved.
Implementation of the Method clone: Harder
Case
Copyright © 2012 Pearson
13-31 Addison-Wesley. All rights
reserved.
Example
import [Link];
// An object reference of this class is contained by Test2
class Test {
int x, y;
}
class Test2 implements Cloneable {
int a, b;
Test c;
public Object clone() throws CloneNotSupportedException
{
Test2 t = (Test2)[Link]();
// t.c = (Test)[Link](); or
t.c = new Test();
return t;
}
}
13-32
Cont..
public class Main {
public static void main(String args[]) throws CloneNotSupportedException
{
Test2 t1 = new Test2();
t1.a = 10;
t1.b = 20;
t1.c.x = 30;
t1.c.y = 40;
Test2 t3 = (Test2)[Link]();
t3.a = 100;
// Change in primitive type of t2 will not be reflected in t1 field
t3.c.x = 300;
// Change in object type field of t2 will not be reflected in t1
[Link](t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
[Link](t3.a + " " + t3.b + " " + t3.c.x + " " + t3.c.y);
}
}
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 13-33
Summary ( Abstract vs Interface)
11/19/202
34 4 Abstract classes & Interface
Design guidelines
• Use classes for specialization and
generalization
• Use interfaces to add properties to classes.
11/19/202
35 4 Abstract classes & Interface