Interface and Abstract Slide from Wu

Download Report

Transcript Interface and Abstract Slide from Wu

Interfaces
Are used to model weak inheritance relationships
Object-inheritance allows you to derive new classes from
existing classes known as inheritance
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Using Classes
Effectively with Polymorphism
Polymorphism allows a single variable to
refer to objects from different classes.
Also, polymorphism denotes the principle
that behavior (methods) can vary (be
called) depending on the actual type of
an object.
The actual type of the object determines
the method to be called.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Abstract Superclasses
and Abstract Methods
Abstract classes are like regular classes with
data and methods, but you cannot create
instances (objects) of abstract classes using
the new operator.
An abstract method cannot be placed in a
nonabstract class.
If a subclass of an abstract superclass does
not implement all the abstract methods, the
subclass must be declared abstract.
.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Abstract Superclasses
and Abstract Methods
A classes that contains abstract
methods must be abstract. However, it
is possible to declare an abstract class
that contains no abstract methods.
A subclass can be abstract even if its
superclass is concrete.
.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Abstract Superclasses
and Abstract Methods
When we define a superclass, we often
do not need to create any instances of
the superclass.
Depending on whether we need to
create instances (objects) of the
superclass, we must define the class
differently.
We will study examples based on the
Animal and Fruit super classes
defined later.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Abstract Superclasses
and Abstract Methods
An abstract class is a class defined with
the modifier abstract. No instances
can be created from an abstract class.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Inheritance versus Interface
Java interface and inheritance both model
an IS-A relationship:
class ButtonHandler implements
ActionListener
Class SavingsAccount extends Account
We say “ButtonHandler is an
ActionListener” and “SavingsAccount is
an Account.”
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Inheritance versus Interface
However, their uses are very different.
The Java interface is used to share
common behavior (only method headers)
among the instances of different classes.
Inheritance is used to share common code
(including both data members and
methods) among the instances of related
classes.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Inheritance versus Interface
In your program designs, remember to
use the Java interface to share
common behavior. Use inheritance to
share common code.
If an entity A is a specialized form of
another entity B, then model them by
using inheritance. Declare A as a
subclass of B.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Interfaces
•What is an interface?
•Creating an interface
•Implementing an Interface
•What is a Marker interface?
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Interface
An interface is a classlike construct. It contains
only constants and abstract methods.
An interface is treated like a a special class in
Java. Each interface is compiled into a
separate bytecode file same as a regular
class.
You cannot create an instance (object) from an
interface using the new operator.
Multiple classes can implement the same
interface type
The java.lang.Comparable interface defines
the compareTo method. Many classes in the
Java library implement Comparable.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
13.6 Define an Interface
The data must be constants.
Each method in an interface has only a signature without
implementation.
Syntax:
public interface NewInterface
{
/** Constants declarations */
public static final int CONSTANT_J = 2;
/** Method signatures*/
public abstract void AbstractMethod();
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Define an Interface
Java allows only single inheritance for
class extensions. Java allows multiple
extensions for interfaces.
public class SubClassName extends
SuperClass implements Interface1,
… InterfaceN
{}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Interface Comparable
//Interface for comparing objects, defined in java.lang
// The compareTo method determines the order of this
// object with the specified/ object o, and returns a
// negative integer, zero, or a positive integer if this
// object is less than, equal to, or greater than the specified
// object o
package java.lang;
public interface Comparable {
public int compareTo(Object o);
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Interface Comparable
// Max.java: Find the maximum object of the
//two objects. Note to use this method, you
// must first implement the Comparable interface for
// the class of these two objects.
public class Max {
/** Return the maximum between two objects */
public static Comparable max(Comparable
o1, Comparable o2) {
if (o1.compareTo(o2) > 0)
return o1;
else
return o2;
}
}
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
The Cloneable Interface –Marker Interface
An interface contains constants and abstract methods, but
the Cloneable interface is a special case:
public interface Cloneable
{
}
This interface is empty. An interface with an empty body is
known as a marker interface.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
TestEdible
(Driver)
<<Edible>>
Animal
Elephant
Tiger
Chicken
Fruit
Apple
Cherry
Orange
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Programs to Use
Programs are stored on the M: drive in the
Interface folder:
•
•
•
•
•
•
•
•
•
TestEdible (Driver)
Edible (an interface)
Animal (super class)
Chicken
Tiger
Elephant
Fruit (super class)
Apple
Orange
M:\Courses\OCP\CIS Classes\CIS 225
Advanced Java\FordeFolder\Interface
Folder
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Interfaces vs. Abstract Classes
Interface
Abstract
Constant
Data
Yes(only
kind)
Yes
All Types of
Data
No
Yes
Abstract
Methods
Yes (only)
Yes
(at least
one)
Concrete
Methods
No
Yes
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.