Course List: http://www.c-jump.com/bcc/
A Printable interface defining a print() method:
public interface Printable { public abstract void print(); }
An interface may declare only static constants and abstract methods.
A Product class implementing the Printable interface:
import java.text.NumberFormat; public class Product implements Printable { //... // implement the Printable interface public void print() { System.out.println( "Code: " + code); System.out.println( "Description: " + description); System.out.println( "Price: " + this.getFormattedPrice()); } }//class Product
Code that uses the print method of the Product class
Printable product = new Product( "123", "Gadget", 15.95 ); product.print();
Resulting output:
Code: 123 Description: Gadget Price: $15.95
An abstract class compared to an interface
|
|
|
|
Some interfaces in the java.lang package
----------------------------------- Interface Methods ---------- ----------------------- Cloneable None Comparable int compareTo( Object ) -----------------------------------
The syntax for declaring an interface
public interface InterfaceName
{
type CONSTANT_NAME = value; // field
returnType methodName( [parameterList] ); // method
}
An interface that defines a method
public interface Printable
{
void print();
}//interface Printable
An interface that defines constants
public interface DepartmentConstants
{
int ADMIN = 1;
int EDITORIAL = 2;
int MARKETING = 3;
}
Note: all constants declared by the interface are static final. The above is identical to
public interface DepartmentConstants { static final int ADMIN = 1; static final int EDITORIAL = 2; static final int MARKETING = 3; }
For clarity reasons, the shorter version is prefered.
A "tagging" interface has no members
public interface Cloneable { }
A tag Interface is a Java term. It is an empty interface which a class implements to claim membership in a set of classes. For example, Cloneable, Serializable. The interface itself declares no methods.
Note: with Java Annotations , tag interfaces might become obsolete, since the Annotations bring more attribute-level power with their syntax.
A class may implement more than one interface:
import java.text.NumberFormat; public class Employee implements Printable, DepartmentConstants { //... }
The syntax for inheriting a class and implementing an interface:
public class SubclassName extends SuperclassName implements Interface1[, Interface2] { //... }
The Book class inherits Product and implements Printable:
public class Book extends Product implements Printable { private String author; public Book( String code, String description, double price, String author ) { super( code, description, price ); this.author = author; } //... // implement the Printable interface public void print() { System.out.println("Code:\t" + super.getCode()); System.out.println( "Title:\t" + super.getDescription()); System.out.println("Author:\t" + this.author); System.out.println( "Price:\t" + super.getFormattedPrice()); } }//class Book
A method that accepts a Printable object
private static void printReport( Printable p ) { p.print(); }
Code that passes a Product object to the method
Product product = new Product( "123", "Gadget", 15.95 ); printReport( product );
The syntax for declaring an interface that inherits other interfaces
public interface InterfaceName
extends InterfaceName1[, InterfaceName2]...
{
// the constants and methods of the interface
}
|
|
A ProductConstants interface
public interface ProductConstants
{
int CODE_SIZE = 4;
int DESCRIPTION_SIZE = 40;
}
A ProductAccess interface inherits multiple interfaces
public interface ProductAccess
extends ProductReader, ProductWriter,
ProductConstants
{
}
The ProductAccessFactory class insulates interface from the implementation.
public class ProductAccessFactory { public static ProductAccess getProductAccess() { ProductAccess pa = new ProductAccessImplementation(); return pa; } }//class ProductAccessFactory
The benefit of separating interface from implementation is that the implementation becomes replaceable: it can be swapped with another implementation by changing a single line of code in the ProductAccessFactory class.
A Product class implementing the Cloneable interface:
public class Product implements Cloneable
{
private String code;
private String description;
private double price;
// the code for the constructor and methods
@Override
public Object clone()
throws CloneNotSupportedException
{
// invoke Object.clone()
return super.clone();
//NOTE:
// Object.clone() method performs a "shallow copy" of the
// object, not a "deep copy" operation.
// Therefore, it may be necessary to modify one or more fields
// of the object returned by super.clone() before returning it.
}
}
Code that uses the clone() method of the Product class:
// create a new product Product p1 = new Product(); // clone the product Product p2 = (Product) p1.clone();
A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a bit-by-bit copy of instances of that class, known as member-wise shallow copy.
Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.
By convention, classes that implement Cloneable interface should override Object.clone() (which is protected) with a public method.
When implementing your own clone(), the idea is to start with the object created by super.clone(), which is guaranteed to be of the correct class. Then populate any additional fields of the object to make a deep copy.
Cloning is a potentially dangerous action, because it can cause unintended side effects.
For example, if the object being cloned contains a field referencing another object, then, after the clone is made, the field in the clone refers to the same object as does the field in the original.
Copy constructors provide an attractive alternative to the Object.clone():
public class Book extends Product implements Printable
{
public static void main (String[] args )
{
Book first = new Book( "12345", "Irish stories and legends", 14.50, "folklore" );
Book second = new Book( first );
first.print();
second.print();
}
private String author;
// Constructor
public Book(
String code,
String description,
double price,
String author )
{
super( code, description, price );
this.author = author;
}
// Copy constructor.
public Book( Book another )
{
this( another.code, another.description, another.price, another.author );
}
// implement the Printable interface
@Override
public void print()
{
System.out.println(
"Code: " + code);
System.out.println(
"Description: " + description);
System.out.println(
"Price: " + price);
System.out.println(
"Autor: " + author);
}
}//class Book
For complete example, see CIS-257 Java Samples