Interface & Abstract Class

advertisement
Interface & Abstract Class
Interface Definition
All method in an interface are abstract
methods.
 Methods are declared without the
implementation part.
 Methods be implemented in the
subclasses that use them.

Multiple Inheritance Using Interface

The interface construct in Java is used
with single inheritance to provide some
form of multiple inheritance.
Multiple Inheritance Using Interface

In order that a sales manager has the
ability to manage, we add to the
SalesManager class, appropriate behavior
in an interface which is then inherited by
the SalesManager class. We shall call that
interface Manage as follows:
Multiple Inheritance Using Interface
Multiple Inheritance Using Interface
In using the Manage interface, the subclass
SalesManager must implement the
abstract methods of the interface.
 This is reflected in the class declaration of
SalesManager:

Multiple Inheritance Using Interface

A sales manager is basically a salesperson
with an additional behavior to authorize
payments (via the authorize() method).
Attributes in an Interface
Data attributes declared in an interface
construct are always static and final.
 They are static as there can only be one
copy of the data available and final since they
are not modifiable.
 By declaring data attributes in an interface,
constant declarations for use in methods is
possible.
 Constants are names for values with a
specific meaning.

Attributes in an Interface

As all data attributes are implicitly
declared as static and final in an interface
definition, these keywords need not
precede their declaration:
Methods in an Interface
All methods in an interface are abstract methods
and any class that uses the interface must provide
an implementation for them.
 An interface does not have to explicitly declare
its methods abstract using the keyword abstract.
 Interface methods are always public, and the
access modifier public keyword is not required
since it is implied in the interface declaration.
 In contrast with data attributes in an interface,
methods may not be static since static methods,
being class specific, are never abstract.

Abstract Class & Interface
A class implementing an interface must
implement all the abstract methods
declared in an interface.
 The class is considered as an abstract
class and must be declared using the
abstract keyword.

Abstract Class & Interface
Abstract Class & Interface
Abstract Class
Interface
May have some methods declared
abstract.
Can only have abstract methods.
May have protected properties and
static methods.
Can only have public methods
with no implementation.
May have final and nonfinal data
attributes.
Limited to only constants.
Abstract Class & Interface
An abstract class can enhance inheritance
as some or all parts of the class can be
implemented and inherited by subclasses.
 An interface, on the other hand, is
generally used for achieving multiple
inheritance in Java.
 An abstract class cannot be used to
instantiate objects since it may contain
parts that are not implemented.

Extending Interface

A subclass of a class that implements an
interface also inherit the methods of the
interface.
Extending Interface
Limitation of interface for Multiple
Inheritance

Although the interface feature in Java
provides an alternative solution to achieving
multiple inheritance in class hierarchies, it
has its limitations:
a. An interface does not provide a natural means of
realizing multiple inheritance in situations where
there is no inheritance conflict.
b. While the principal reason for inheritance is code
reusability, the interfacefacility does not
encourage code reuse since duplication of code is
inevitable.
Download