Object-Oriented Programming with Java Lecture Notes

26 February 2008 • 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

Graphs

Copying Class Instances

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

Summary


This page last modified on 26 February 2008.

This work's CC license.