Java Module-3 Part-B (interface) book notes
Java Module-3 Part-B (interface) book notes
bal - b:
As you can see, the Balance class is now public. Also, its constructor and its show()
method are public, too. This means that they can be accessed by any type of code outside
the MyPackpackage.For example, here TestBalanceimports MyPack and is then able to
make use of the Balance class:
import MyPack. *i
class TestBalance
public static void main(String args [J) {
As an experiment,remove the public specifier from the Balance class and then try
Anterfaces
Using the keyword interface, you can fully abstract a class' intetface from its imiplementation.
That is, using interface, you can specity what a class must do, but not howit does it. Interfaces
are syntactically similar to classes, but theylackinstance variables, and their methods are
at compile time
compatible. This
so the Java
requirement
compiler can check to ensure
by itself makes for astatic
in a system like this,
functionality
and nonextensible
gets pushed up higher
classing
and higher
I
PART
Definingan Interface
much a class. This is the general form of an interface:
An interface is defined like
return-type method-nane2(parameter-list);
return-type method-nameN(parameter-list);
type final-varnameN=value;
{
interface Callback
;
void callback (int param)
94 Part I: The Java
Language
ImplementingInterfaces
Once an interface has been defined, one or more classes
implementan interface, include theimplements canimplement that interface.[lo
the methods defined bythe clause in a class definition, and ther
create
interface. The general form of a class that includes
clause looks like this: the implements
class classname
1/class-body
[extendssuperclass] [implements
interface [,interface..] |
It implements more than one interface, the interfaces are separated
aclass
with a comma.If
a class implementstwo interfaces that declare the same method, then the same
method will
be used by clients of either interface. The methods that implement an interface
must be
declared public. Also,the type signature of the implementing method must
match exactly
the type signature specified in the interface definition.
Here is a small example class that implements the Callback interface shownearlier.
void nonIfaceMeth () {
System.out.println ("Classes that implement interfaces "
" may also define other members, too. ");
Chapter 9: Packages and Interfaces 195
References
Accessing Implementations Through Interface
that use an interface rather than a class type.
You can declare variables as object references to by such
the declared interface can be referred
Any instance of any class that implements the correct version will
through one of these references,
a variable. When you call a method
the being referred to. This is one of the PARTI
be called based on the actual instanceof
interface
when
CAUTION Becausedynamic lookup of a method at run time incurs a significant overhead
not to use
compared with the normal method invocation in Java, you should be careful
interfaces
The following example calls the callback()method via an interface reference variable:
class TestIface {
shown here:
of callback");
System. out.println ("Another version
System. out.println ("p squared
is " + (p*p)) ;
96 Part I: The Java Language
c.callback (42);
C= ob; //
c now refers to AnotherClient
object
c.callback (42);
As youcan see, the version of callback() that is called is determined by the type of object
that c refers to at run time. While this is a very simple example,you will see another, more
practical one shortly.
Partial Implementations
Ifa class includes an interface but does not fully implement the methods definedby that
interface, then that class must be declared as abstract. For example:
Here,the class Incomplete does not implement callback( )and must be declared as abstract.
Any class that inherits Incomplete must implement callback( ) or be declared abstract itself.
Nested Interfaces
Aninterface can be declared a member of a class or another interface. Such an interface is
called a muember interfacelora nested interface.JA nested interface can be declared as public,
private, or protected. This differs from top-level interface, which must either be declared
used outside of its enclosing scope, it must be qualified by the name of the class or interface
of which it is a membe. Thus, outside of the class or interface in which a nested interface is
declared, its name must be fully qualified.
Here is arn example that demonstrates a nested interface:
class NeštedIFDemo {
public static void ma in
(String args []) {
// use a nested interface reference
A,NestedIF nif new B();
if (nif.isNotNegat ive(10))
System. out.println("10 is not negative");
if (nif.isNotNegative (-12))
System.out .println("this won't be
displayed" );
Notice that A defines a member interface called NestedIF and that it is declared public.
Next, B implementsthe nested interface by specifying
implements A.NestedIF
it must provide implementations for all methods defined within the interface inheritance
)
chain. Following is an example:
() ."):
System.out.println ("Implement meth2
PARTI
class IFExtend {
public static void main (Sting arg []) {
ob.methi () ;
ob.meth2 ()
ob.meth3 () ;
must implement methods defined by that interface, including any that are
an interface all
Although the examples we've included in this bookdo not make frequent use of packages
or interfaces, both of these tools are an important part of the Java programmingenvironnment.
all real programs that you write in Java will be contained within packages. A number
Virtually
will probably implement interfaces as well. It is important, therefore, that you be comfortable
with their usage.
interlaco Neonteface
vel he lloc)
stodic
sttc
Syotemoteprid(" kello,dsom
3
vaio eyeni eMetthoo(Sting
lass ntefqce
Ssdeodpinslalstr)
lessDeme
Itenfqce rtfqce