- Forward


Class-Enum Hybrids
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Classes:
    • The set is defined using intension
  • Enums:
    • The set is defined using extension
  • Hybrids:
    • We need to describe some elements and explicitly list others
Motivation (cont.)
Back SMYC Forward
  • A Common Situation:
    • A class contains commonly-used instances that are not "special" in any way (e.g., the Color class contains instances like Color.RED, Color.GREEN, ...)
  • A Less-Common Situation:
    • A class contains instances that represent "special" situations (e.g., a Color class that includes an instance that couldn't be represented by the color model being used; an upper/lower bound on an ordered set that is not an element of the set)
An Example of the Less-Common Situation
Back SMYC Forward
  • The Setting:
    • We need Temperature objects for equipment that can be used for heating and/or cooling
  • The Issue:
    • A heating/cooling unit yet can be heating/cooling to any temperature (including 0) and can also be off
Properties of the Enumerated Instances
Back SMYC Forward
  • public:
    • So they are accessible from all other objects
  • final:
    • So they can't be "corrupted"
  • static:
    • So they are a property of the set not of individual instances
Comparing Instances
Back SMYC Forward
  • Instances of Enums:
    • Are "normally" compared using == (though can be compared with the equals() method)
  • Instances of Classes:
    • Are "normally" compared using the equals() method
  • Instances of Hybrids:
    • Are "normally" compared using the equals() method
An Example (cont.)
Back SMYC Forward
javaexamples/oopbasics/Temperature.java
 
The equals() Method Revisited
Back SMYC Forward

An Implementation that Works in a Wider Variety of Situations

public boolean equals(Temperature other) { if ((other == OFF) && (this == OFF)) return true; else if ((other == OFF) && (this != OFF)) return false; else if ((other != OFF) && (this == OFF)) return false; else return stringValue.equals(other.stringValue); }
Something to Think About
Back SMYC Forward
  • A Fairly Obvious Question:
    • Given that we can do this kind of thing why does Java have an enum
  • The Answer:
    • Convenience
Something to Think About (cont.)
Back SMYC Forward

The Month Enum as a Class

javaexamples/oopbasics/months/class/Month.java (Fragment: 0)
 
Something to Think About (cont.)
Back SMYC Forward
  • What's Missing?
    • compareTo()
    • ordinal()
    • valueOf()
    • values()
  • Can We Add Them?
    • Yes, but it's inconvenient to do so for every enumerated type
Something to Think About (cont.)
Back SMYC Forward

The Improved Month Class

javaexamples/oopbasics/months/class/Month.java
 
Something to Think About (cont.)
Back SMYC Forward
  • A Question:
    • Can't we make it easier using specialization/inheritance
  • An Answer (for Now):
    • Yes, but it is still inconvenient and the need for static methods makes it messy (as we will discuss when we consider abstract classes)
There's Always More to Learn
Back -