Advanced Java - 06
Advanced Java - 06
Chapter 6
13.5 To define interfaces and define classes that implement interfaces (§13.5).
Shape
Circle Square
• Java’s Solutions
• Abstract Classes
• Interfaces
GeometricObject
Circle
Rectangle
TestGeometricObject
LargestNumbers
Example:
– If the class does not provide definitions of all methods, the class
would not compile. We have to declare it as an abstract class in
order to get it compiled.
<<Interface>>
Speaker
speak()
Edible TestEdible
Copyright © 2024 Pearson Education, Inc. All Rights Reserved
Example: The Comparable Interface
// This interface is defined in
// java.lang package
package java.lang;
2. System.out.println("ABC".compareTo("ABE"));
5. System.out.println(date1.compareTo(date2));
<<Interface>>
Speaker
speak()
Speaker sp = null;
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.