3.6 - CBSD.org

Wrapper Classes
• ints, doubles, and chars are known as primitive types,
or built-in types.
• There are no methods associated with these types of
variables.
• Strings, on the other hand, are not primitive: there is a
class called String that has methods, such as .length( ),
.charAt( ), etc.
• So, when you create a String variable, you are actually
creating an object of the String class.
• Using primitive types is more efficient, in terms of code
and computer memory. But what if you want to use
certain methods on an int, a double, or a char?
Wrapper Classes
• A wrapper class allows you to “wrap” or “box”
a primitive type into an object, so that you
can use methods associated with that object.
• Example:
Integer age = new Integer(34);
• The value of age is 34, but you can do more
with this than you could with a normal,
primitive int.
What is the point of a Wrapper Class?
• The 3 most common uses of a wrapper class are:
1) To use in an ArrayList (we’ll learn about ArrayLists soon –
for now, you should know that they hold Objects, not
primitive types. So, you need wrapper classes [which are
subclasses of Object] when using an ArrayList. )
2) When you want to use a null reference (to deliberately set
a variable value to null. When would you do this? One
example: When you can’t be 100% sure that a method will
return a valid integer.)
3) When you want to use an Integer, Double, or Char
polymorphically
example: Object num = new Integer(23);
Interfaces
• An abstract method is a method with a first line (also called a
signature or a header), but without a body
• An interface is a collection of abstract methods and
(sometimes) constants
• An abstract method can be declared using the modifier
abstract, but because all methods in an interface are
abstract, usually it is left off
• An interface is used to establish, as a “formal contract”, a set
of methods that a class will implement.
• In simple terms: an interface is a blueprint/template/protocol
that shows programmers how to write a certain type of class.
interface is a reserved word
public interface Sample
An interface can contain constants
{
public final double PI = 3.14;
public void times2(int x);
public double getPI();
}
A semicolon immediately
follows each method header
Notice: none of the methods in
an interface are given
a definition (body)
public class Example implements Sample
{
public void times2(int x)
{
System.out.println(x*2);
}
public double getPI()
{
return PI;
}
Each method listed
in Sample must be
implemented (given a
definition)
}
Example has access to the constant PI (from Sample)
• Open Sample, Example
• In a class diagram, a dotted arrow points from
any class that implements an interface to the
interface. Example:
• An interface cannot be instantiated (meaning you
can’t create an object of it from a client program).
• Methods in an interface cannot be private. It is
nonsensical to have a private method in an interface
– where would this method be called from?
Remember, all methods in an interface are abstract
(empty).
• A class formally implements an interface by
– stating so in the class header (using the word implements)
– providing implementations (writing code) for each
abstract method in the interface
• If a class implements an interface, then it MUST
define/implement (provide code for) all methods
that are in the interface
• A class that implements an interface can
contain other methods as well
• In addition to (or instead of) abstract methods,
an interface can contain constants
• When a class implements an interface, it gains
access to all its constants
• A class can implement multiple interfaces. (Note
that this is different from inheritance: a class can
only extend (inherit from) ONE parent.)
• The interfaces are listed in the implements
clause
• The class MUST implement (write code for) all
methods in all interfaces listed in the header
public class Mult implements Interface1, Interface2
{
// all methods of both interfaces
// MUST be defined here
}
• Also, many different classes can implement the
same interface.
• It is possible for a class to both extend a parent
class and implement interfaces:
public class Car extends Vehicle implements Interface1
{
}
• Note: extends must occur before implements
• The Java standard class library contains many
helpful interfaces
• The Comparable interface contains an abstract
method called compareTo, which is used to
compare two objects lexicographically (comparing
their ASCII values).
• The String class implements Comparable,
giving us the ability to put strings in lexicographic
order: compares the letters using their ASCII (aka
Unicode) values
• Demo: Compare
• When a programmer writes a class that implements
the Comparable interface, it’s up to him/her to
determine what makes one object greater or less
than another
• Not all objects are compared numerically or
lexicographically
• Confusing terminology:
– A class that “connects” to an interface is said to
implement the interface, by using the keyword
implements
– Sometimes, you will see exams and text books
mention that a certain method has not been
implemented – what they mean is that the method
has not been given any code in its body. In other
words, the method has a signature, but is
otherwise empty.
Assignments
1) Everyone - open “Litvin Practice Test A2” in the “AP
Practice Exams” folder. Only do the problems listed on the
board. Write down all answers (and work if necessary) on
paper. Hand in when done.
2) Open pages 291-293 from the Unit 3 folder.
Answer the following questions. Save answers in a Word
doc called p291-3Qs, in your Programs folder:
• p. 291-3 multiple choice #4, 6, 7, 9, 10
• P. 293 true/false #9 – 10
2) Continue working on Battleship.