Professional Documents
Culture Documents
&
Interface
Instructor: Wenjia Li
01/08/16
Still Remember
Inheritance?
01/08/16
samePay Method
Suppose that we decide that it will often be
necessary to determine if two Employees have
the same pay.
We decide to implement a method named samePay in
the Employee class.
This method should be able to compare the pays for any
kinds of Employees.
01/08/16
Solution:
Require that classes derived from Employee (who know
what type they are) implement a suitable getPay
method that can then be used from samePay.
Java provides this capability through the use of abstract
methods.
01/08/16
Abstract Class
If a class has at least one abstract method,
it must be declared as an abstract class.
Note that a class that has NO abstract
methods may also be declared as abstract, if
desired. For example, a class that contains only
instance variables.)
01/08/16
01/08/16
10
Sets the calendar with the specified year, month, and date. The month
parameter is 0-based, that is, 0 is for January.
Returns the maximum value that the specified calendar field could have.
Adds or subtracts the specified amount of time to the given calendar field.
+getTime(): java.util.Date
java.util.GregorianCalendar
+GregorianCalendar()
+GregorianCalendar(year: int,
month: int, dayOfMonth: int)
+GregorianCalendar(year: int,
Constructs a GregorianCalendar for the specified year, month, day of
month: int, dayOfMonth: int,
month, hour, minute, and second. The month parameter is 0-based, that
hour:int, minute: int, second: int)
is, 0 is for January.
01/08/16
11
Calendar Class
An instance of java.util.Date represents a
specific instant in time with millisecond precision.
java.util.Calendar is an abstract base class for
extracting detailed information such as year,
month, date, hour, minute and second from a
Date object.
Subclasses of Calendar can implement specific
calendar systems such as Gregorian calendar,
Lunar Calendar and Jewish calendar.
Currently, java.util.GregorianCalendar for the
Gregorian calendar is supported in the Java API.
01/08/16
12
The GregorianCalendar
Class
Use new GregorianCalendar() to
construct a default GregorianCalendar with
the current time
Use new GregorianCalendar(year,
month, date) to construct a
GregorianCalendar with the specified year,
month, and date. The month parameter is
0-based, i.e., 0 is for January.
01/08/16
13
01/08/16
14
Interfaces
What is an interface?
Why is an interface useful?
How do you define an interface?
How do you use an interface?
01/08/16
15
What is an interface?
An interface is a classlike construct that
contains only constants and abstract
methods.
In many ways, an interface is similar to an
abstract class, but the intent of an
interface is to specify behavior for objects.
For example, you can specify that the objects
are comparable, edible, cloneable using
appropriate interfaces.
01/08/16
16
Omitting Modifiers in
Interfaces
All data fields are public final static and all
methods are public abstract in an interface. For
this reason, these modifiers can be omitted, as
shown below:
public interface T1 {
public static final int K = 1;
public abstract void p();
}
Equivalent
public interface T1 {
int K = 1;
}
void p();
17
Public Interface
Objects define their interaction with
the outside world through the their
public interface
A class' public interface is the set of
public members that a user can access
Public non-static members & methods
Public static members & methods
01/08/16
18
Java Interface
Interfaces in Java are a set of behaviors
that are common to multiple classes.
The implementation of an Interface is
similar to a class, except that interfaces:
Use the keyword interface instead of class
Can only contain public methods, variables,
and constants
Methods do not contain a body.
All methods are implicitly abstract.
01/08/16
19
Define an Interface
To distinguish an interface from a class,
Java uses the following syntax to define an
interface:
public
interface InterfaceName {
}
constant declarations;
method signatures;
public
Example
:
interface
Edible {
/** Describe how to eat */
public abstract String howToEat();
}
01/08/16
20
An Example
For instance all Vehicles should be able to
Speed up
Slow down
Turn
01/08/16
21
Some observations
Each method defined in the interface
does not have a body.
Interfaces can only have initialized
variables.
Any class that has the same methods
defined in the interface may
implement Drivable.
01/08/16
22
Implementing Interfaces
23
24
Example: Comparable
Comparable is an interface defined in
the Java API that is used to provide an
ordering of objects of the same type.
A class that implements comparable
must define the compareTo() method.
When invoked, a.compareTo(b) returns
a negative number if a < b
0 if a == b
a positive number if a > b
01/08/16
25
Comparable
// This interface is defined in
// java.lang package
package java.lang;
public interface Comparable {
public int compareTo(Object o);
}
01/08/16
26
An Example of Comparable
01/08/16
27
Conventions
All interface methods must have thorough
javadoc comments. These must include the
intended purpose of the method.
compareTo is supposed to return specific
values when invoked. This is a convention of
the interface and is enforced by the Java
compiler.
The following implementation is syntactically
correct, but violates the intended usage.
01/08/16
28
new
new
new
new
01/08/16
29
(a)
String s1 = "abcdef";
String s2 = "abcdee";
String s3 = (String)Max.max(s1, s2);
30
package java.lang;
public interface Cloneable {
}
01/08/16
31
Interfaces V. S. Abstract
Classes
In an interface, the data must be constants; an abstract
class can have all types of data.
Each method in an interface has only a signature without
implementation; an abstract class can have concrete
methods.
Variables
Constructors
Methods
Abstract
class
No restrictions
No restrictions.
Interface
All variables
must be public
static final
01/08/16
32
Interfaces V. S. Abstract
Classes (Cont.)
Interface1_2
Interface1_1
Object
Interface2_2
Interface1
Class1
Interface2_1
Class2
33
Interface Hierarchy
Interfaces can be used as the base class
of other interfaces.
You can derive an interface from another
interface using the keyword extends.
A derived interface inherits all the methods of
the base interface.
To implement a derived interface, all methods
must be implemented by the class.
34
Class Hierarchy
01/08/16
35
Multiple Inheritance
Java does NOT support multiple
inheritance with classes.
You can not say Class X extends X, Y
01/08/16
36
Multiple Interfaces
37
Multiple Interfaces
38