Lec23-Interface ClassComparable Interface
Lec23-Interface ClassComparable Interface
Lec23-Interface ClassComparable Interface
&
Comparable Interface
Lecture 23
Based on Slides of Dr. Norazah Yusof
1
Interface & Implements
• An interface is a classlike construct that
contains only constants variables and abstract
methods definition.
• An interface cannot contain instance variable
and solid methods.
• An interface specifies what methods will be
implemented by classes that implements an
interface.
• This is another way to design polymorphic
methods.
2
Interface
All classes share a single root, the Object class, but there
is no single root for interfaces.
Like a class, an interface also defines a type.
A variable of an interface type can reference any instance
of the class that implements the interface.
If a class extends an interface, this interface plays the
same role as a superclass. You can use an interface as a
data type and cast a variable of an interface type to its
subclass, and vice versa.
3
Interface
Interface1_2 Interface2_2
4
Interface declaration
• Declaration:
public interface InterfaceName
{
/* constant declaration */
/* method declarations (without
implementations.) */
}
5
Interface declaration
In an interface, by default all data fields are constant
variables and defined as public final static and all methods are
public abstract.
For this reason, these modifiers can be omitted, as shown
below:
public interface T1 { public interface T1 {
public static final int K = 1; Equivalent int K = 1;
6
Ex: Interface declaration
(constant variables)
public interface Conversionfactors
{
double inToMm = 25.3;
double ounToGram = 28.34952;
double poundToGram = 453.5924 ;
}
• Save as ConversionFactors.java
7
Ex: Interface declaration
(Abstract Methods)
public interface Conversions {
public double InToMm();
public double OunceToGram();
public double PoundToGram();
}
• Save as Conversions.java
8
Ex: Interface declaration
(variables and Methods)
public interface MyInterface {
public final int aConstant = 32;
public final double pi = 3.14159;
9
Implementing an Interface
A class may extends one parent (superclass), but it
can implement none or more interfaces
A class that implements an interface:
Can access directly all the variables declared in
the interface
Have to redefined all the methods declared in
the interface
Declaration:
class SomeClass extends Parent implements SomeInterface
{ ordinary class definition body }
10
• The body of the class definition is the
same as always.
• If any of the abstract methods (in the
interface) are not defined in the class that
implements the interface, the class will
become an abstract class
11
Example 1
public interface Speakable {
public String speak();
}
12
Example 1
public class Cat extends Animal implements Speakable {
public Cat() {
kind ="cat"; }
public String speak() {
return "meow"; }
}
13
class Main {
public static void main(String[] args) {
15
Interface vs Abstract
Variables Constructors Methods
16
Comparable Interface
// This interface is defined in
// java.lang package
package java.lang;
17
String and Date Classes
Many classes (e.g., String and Date) in the Java library
implement Comparable to define a natural order for the objects.
If you examine the source code of these classes, you will see the
keyword implements used in the classes, as shown below:
public class String extends Object public class Date extends Object
implements Comparable { implements Comparable {
// class body omitted // class body omitted
} }
18
Generic max Method
// Max.java: Find a maximum object // Max.java: Find a maximum object
public class Max { public class Max {
/** Return the maximum of two objects */ /** Return the maximum of two objects */
public static Comparable max public static Object max
(Comparable o1, Comparable o2) { (Object o1, Object o2) {
if (o1.compareTo(o2) > 0) if (((Comparable)o1).compareTo(o2) > 0)
return o1; return o1;
else else
return o2; return o2;
} }
} }
(a) (b)
The return value from the max method is of the Comparable type.
So, you need to cast it to String or Date explicitly.
19
Declaring Classes to Implement Comparable
Notation: GeometricObject «interface»
The interface name and the - java.lang.Comparable
method names are italicized.
The dashed lines and hollow Rectangle +compareTo(o: Object): int
triangles are used to point to -
the interface.
ComparableRectangle
-
You cannot use the max method to find the larger of two instances of Rectangle, because
Rectangle does not implement Comparable. However, you can declare a new rectangle
class that implements Comparable. The instances of this new class are comparable. Let
this new class be named ComparableRectangle.
ComparableRectangle rectangle1 = new ComparableRectangle(4, 5);
ComparableRectangle rectangle2 = new ComparableRectangle(3, 6);
System.out.println(Max.max(rectangle1, rectangle2));
20
Example of an Interface
// This interface is defined in
// java.lang package
package java.lang;
Person p[ ] = {p1,p2,p3};
System.out.println ("befor comparable") ;
for(Person person:p){
System.out.println(person);
}
java.util.Arrays.sort(p);
System.out.println ("after comparable") ;
for(Person person:p){
System.out.println(person);
}
}
}
class Person implements Comparable<Person>{
private String name;
private int age ;
private String school ;