0% found this document useful (0 votes)
17 views32 pages

Interfaces Updated

Uploaded by

sumrun sahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views32 pages

Interfaces Updated

Uploaded by

sumrun sahab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Chapter 13

Interfaces
Abstract Class
• A class that cannot instantiate objects.
Message

Text Voice Fax


Message Message Message

Public abstract class Message {


}

Abstract classes & Interface 07/08/202 2


4
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

Abstract classes & Interface 07/08/202 3


4
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

Abstract classes & Interface 07/08/202 7


4
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.

Abstract classes & Interface 07/08/202 11


4
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.

Abstract classes & Interface 07/08/202 12


4
Rules

Abstract classes & Interface 07/08/202 13


4
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
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

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.

Abstract classes & Interface 07/08/202 20


4
The Comparable Interface
• The Comparable interface is in the java.lang
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 Object.clone() 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.
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-28 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-29 Addison-Wesley. All rights
reserved.
Implementation of the Method clone: Harder
Case

Copyright © 2012 Pearson


13-30 Addison-Wesley. All rights
reserved.
Summary ( Abstract vs Interface)

Abstract classes & Interface 07/08/202 31


4
Design guidelines

• Use classes for specialization and


generalization
• Use interfaces to add properties to classes.

Abstract classes & Interface 07/08/202 32


4

You might also like