Interfaces: CS 310 Lecture notes

Object-Oriented Programming with Java Lecture Notes

24 February 2009 • Interfaces


Outline

Polymorphism

Inheritance

Problems

Abstract Classes

Interfaces

Interface Examples

Interface Specifications

Interfaces and Classes

Constant-Defining Interfaces

Trait-Defining Interfaces

Trait Behaviors

Trait Specifications

Interfaces v Abstract Classes

Interfaces & Abstract Classes

Class-Instance Danger

Class-Instance Safety

Class-Instance Copying

Class-Instance Graphs

Shallow Copies

Why Copy?

Deep Copies

Deep-Copy Example

Object.clone()

No-Clone Example

$ cat t.java
class blob { }

class t {
  public static void main(String args[]) {
    blob b = new blob();
    blob cpy = (blob) b.clone();
    }
  }

$ javac -Xlint t.java
t.java:7: clone() has protected access 
          in java.lang.Object
    blob cpy = (blob) b.clone();
                       ^
1 error

$ 

Default Clone

Shallow-Clone Example

$ cat t.java
class blob 
implements Cloneable
  public blob clone() 
  throws CloneNotSupportedException
    return (blob) super.clone();

class t
  public static void main(String args[])
    blob b = new blob();
    try blob cpy = b.clone();
    catch (Exception e) { }

$ javac -Xlint t.java

$ 

Deep-Copying Clones

Deep-Copy Clone Example

The Clone Contract

Summary

References


This page last modified on 24 February 2009.

Creative
    Commons License