Title: Chapter 2 ADTs and Java Classes
1Chapter 2ADTs and Java Classes
- CS 260 Data Structures
- Indiana University Purdue University Fort Wayne
2Abstract Data Type (ADT)
- An Abstract Data Type (ADT) consists of private
data together with methods that act on that data - A Java class can be used to represent an ADT
- The class definition for an ADT should have
- private instance variables
- public instance methods and constructor(s)
- Any internal helper method should be private
- The instance variables define the current state
- The instance methods give the behavior
3Example
public class Throttle private int
top // the max throttle position private
int pos // the current throttle
position private Gizmo giz // something
like cruise control public Throttle( int
size) public void shift( int amount
) // end class Throttle
4Reference variables
- Application code
- Throttle t1
- Throttle t2
- t1 new Throttle( 10 )
- t1.shift( 3 )
- t2 new Throttle( 7 )
- t2 t1
- t1 and t2 are aliases
- Aliases are poor practice
- t2.shift( 6 )
- Now t1.pos is 9
- t2 null
- Eliminates the alias
- t2.shift( 3 )
- causes NullPointerException
t1
t2
top pos giz
top pos giz
one instance
another instance
Each instance has a separate copy of each
instance variable
5Clones and the test for equality
- A clone is an exact copy of an instance
- t2 t1 does not create a clone
- This just gives an alias
- A special clone( ) method must be implemented
- Test for equality
- t2 t1 is true when t1 and t2 refer to the same
instance - What is wanted is a test for whether different
instances contain exactly the same data - A special equals( ) method must be implemented
6Inheritance
- Recall that classes form a hierarchy
- Class Object is the ancestor of all other classes
- A class inherits all the state and behavior from
its parent class - All instance variables
- All methods
- A class can add new instance variables to the
instance variables it inherits - A class may add new methods
- A class may redefine any method
7Inheritance
Object
Throttle
Vector
String
Stack
8A clone( ) method for class Throttle
public class Throttle implements Cloneable
public Object clone( ) Throttle
ans try ans ( Throttle )
super.clone( ) // clone( ) inherited from
class Object ans.giz ( Gizmo )
giz.clone( ) // clone( ) defined in class
Gizmo catch ( CloneNotSupportedException
) throw new RuntimeException( message )
return ans // end clone
// end class Throttle
9Clone( ) method
- t2 ( Throttle ) t1.clone( )
t1
ans
First step ans ( Throttle ) super.clone( )
top pos giz
Second step ans.giz ( Gizmo ) giz.clone( )
top pos giz
clone of t1
aGizmo
clone of aGizmo
10Using the clone( ) method
- To make a clone of Throttle t1 . . .
- Casting to class Throttle lets the compiler know
that the result is a specific Object of class
Throttle
Throttle t2 t2 ( Throttle )
t1.clone( )
11A equals( ) method for class Throttle
- The equals method should work even when you
compare a Throttle with some other kind of object - In that case it should return false
public boolean equals( Object obj )
Throttle t if ( obj instanceof Throttle
) t ( Throttle ) obj return (
t.top top ) ( t.pos pos )
t.giz.equals( giz )
else return false // end equals
12Clone( ) and equals( ) methods
- Note that there are special requirements when
instance variables contain objects as well as
primitive values in instance variables - Method clone( ) requires that class Gizmo
implements its own clone( ) method - Method equals( ) requires that class Gizmo
implements its own equals( ) method - If this is not so, then the needed methods must
be added to the Gizmo class
13Arrays
- All array objects have clone( ) and equals( )
methods
int a new int 100 int b b
( int ) a.clone( ) if ( a.equals( b ) )
14Static methods
- Static methods are not applied to objects as in .
. . - t.shift( 3 )
- Instead, static methods are called as in . . .
- Throttle.staticMethod( t2 )
- Integer.toString( n )
- There are no instance variables (like top, pos,
giz) directly available to a static method - When there is a Throttle parameter t2, then
instance variables are available in the form
t2.top, t2.pos, and t2.giz - Static methods are not appropriate for OOP
- Static method are needed in Java since . . .
- Java is not a pure object-oriented language
- Primitive variables are not objects