COMP 303 - Lecture Notes for Week 7 - Java Object Model

Types

Java Types and Values

Types

Java Values

Subtype Relationship

S is a subtype of T if

The ArrayStoreException

Wrapper Classes

Enumerated Types

Typesafe Enumerations

Type Inquiry

The Class Class

An Employee Object vs. the Employee.class Object

.

Type Inquiry

Array Types

Object: The Cosmic Superclass

The toString Method

Overriding the toString Method

Overriding toString in Subclass

The equals Method

Overriding the equals Method

Overriding equals in Subclass

Not all equals Methods are Simple

The Object.equalsMethod

Requirements for equals Method

Fixing Employee.equals

The Perfect equals Method

Hashing

Hashing

Shallow and Deep Copy

Cloning

.

Cloning

The Cloneable Interface

The clone Method

public class Employee 
  implements Cloneable 
{ 
   public Object clone() 
   { 
      try 
      { 
         return super.clone(); 
      } 
      catch(CloneNotSupportedException e) 
      { 
         return null; // won't happen 
      }
   } 
   ... 
}

Shallow Cloning

Deep Cloning

public class Employee 
  implements Cloneable 
{ 
   public Object clone() 
   { 
      try 
      { 
         Employee cloned = (Employee)super.clone();
         cloned.hireDate = (Date)hiredate.clone();
         return cloned; 
      } 
      catch(CloneNotSupportedException e) 
      { 
         return null; // won't happen 
      }
   } 
   ... 
}

Deep Cloning

.

Cloning and Inheritance

Serialization

Serialization

How Serialization Works

.

Serializing Unserializable Classes

Reflection

Reflection

Example: Enumerating static fields Math class

Field[] fields = Math.class.getDeclaredFields(); 
for (int i = 0; i < fields.length; i++) 
   if (Modifier.isStatic(fields[i].getModifiers())) 
      System.out.println(fields[i].getName()); 

Enumerating Constructors

Getting A Single Method Descriptor

Invoking a Method

Inspecting Objects

Inspecting Objects

Inspecting Array Elements

Components

A Builder Environment

.

Java Beans

.

A Calendar Bean

.

A Property Sheet

.

Facade Class

Facade Pattern

Context
  1. A subsystem consists of multiple classes, making it complicated for clients to use
  2. Implementor may want to change subsystem classes
  3. Want to give a coherent entry point
Solution
  1. Define a facade class that exposes all capabilities of the subsystem as methods
  2. The facade methods delegate requests to the subsystem classes
  3. The subsystem classes do not know about the facade class

Facade Pattern

.

Facade Pattern

Name in Design Pattern
Actual Name (Beans)
Client
Builder tool
Facade
Main bean class with which the tool interacts
SubsystemClass
Class used to implement bean functionality

Bean Properties

Property Syntax

Java Naming Conventions

Editing Beans in a Builder Tool

.

Editing Beans in a Builder Tool

.

Packaging a Bean

Composing Beans

.

Bean Information

The BeanInfo Interface

Image getIcon(int iconKind) 
BeanDescriptor getBeanDescriptor() 
MethodDescriptor[] getMethodDescriptors() 
PropertyDescriptor[] getPropertyDescriptors() 
EventSetDescriptor[] getEventSetDescriptors() 
int getDefaultEventIndex() 
int getDefaultPropertyIndex() 
BeanInfo[] getAdditionalBeanInfo() 

Removing Spurious Properties

class MyBeanBeanInfo extends SimpleBeanInfo 
{ 
   public PropertyDescriptor[] getPropertyDescriptors() 
   { 
      try 
      { 
         return new PropertyDescriptor[] 
         { 
            new PropertyDescriptor("x", CarBean.class); 
            new PropertyDescriptor("y", CarBean.class); 
         }; 
      } 
      catch (IntrospectionException exception) 
      { return null; } 
   } 
}

Property Editors

Custom Property Editors

Editing Text Properties

Editing Choice Properties

Editing Arbitrary Properties

Registering Property Editors

Example: CarBean

Example: CarBean

.

Implementing a Property Sheet

.

Implementing a Property Sheet