Java Reference
In-Depth Information
Let's answer the second question. What can you do with the variable of an interface type? All rules for a reference
type variable equally apply to a variable of an interface type. A few important things that you can do with a variable of
a reference type are as follows:
•
You can assign a reference of an object in memory including a
null
reference value.
Swimmable sw2 = null;
•
You can access any constant fields declared in an interface using a variable of the interface
type or directly using the interface name. It is preferred to access the constants of an interface
using the interface name. Consider the
Choices
interface with two constants called
YES
and
NO
. You can access the value of the two constants using the simple name of the interface as
Choices.YES
,
Choices.NO
, and using the interface reference variable
sw2.YES
, and
sw2.NO
.
•
You can use a variable of an interface type to invoke any methods declared in the interface.
For example, a variable of the
Swimmable
type can invoke the
swim()
method as
Swimmable sw3 = get an object instance of the Swimmable type...
sw3.swim();
java.lang.Object
class. This
rule is not very obvious. However, if you think carefully, it is a very simple and important rule.
A variable of an interface type can refer to an object in memory. No matter what object in
memory it refers to, the object will always be of a class type. All classes in Java must have the
Object
class as their direct/indirect superclass. As a result, all objects in Java have access to
all methods of the
Object
class. Therefore, it is logical to allow an interface type variable to
access all methods of the
Object
class. The following snippet of code calls the
hashCode(),
getClass()
, and
toString()
methods of the
Object
class using a
Swimmable
type variable:
•
A variable of an interface type can invoke any method of the
Swimmable sw4 = get a Swimmable type object...
int hc = sw4.hashCode();
Class c = sw4.getClass();
String str = sw4.toString();
•
Another important rule to remember is that an instance or static variable of an interface type
is initialized to
null
by default. As is the case with all types of local variables, a local variable
of an interface type is not initialized by default. You must explicitly assign it a value before you
can use it.
Implementing an Interface
An interface defines a specification for objects about the ways they will communicate with other objects. A specification
is a contract or agreement for an object's behavior. It is very important that you understand the difference between the
two terms
specification
(or
contract
) and
implementation
. A specification is a set of statements and implementation is
the realization of those statements.
Let's take a real-world example. The statement “Jack will give ten dollars to John on June 8, 2014.” is a
specification. When Jack gives ten dollars to John on June 8, 2014, the specification is executed. You can restate it as
when Jack gives ten dollars to John on June 8, 2014, the specification is implemented. Sometimes, while discussing
interface, a specification is also referred to as a contract, a protocol, an agreement, a plan, or a draft. No matter which
term you use to refer to a specification, it is always abstract. The implementation of a specification could be partial
or complete. Jack may give seven dollars to John on June 8, 2014, and, in that case, the specification has not been
implemented completely.