String name;
double bal;
public Balance(String n, double b) {
name = n;
bal = b;
public void show() {
System.out.print("--> ");
System.out.println(name + ": $" + bal);
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 MyPack package. For example, here TestBalance imports MyPack and is then able to
make use of the Balance class:
import MyPack.*;
class TestBalance {
public static void main(String args[]) {
/* Because Balance is public, you may use Balance
class and call its constructor. */
Balance test = new Balance("J. J. Jaspers", 99.88);; // you may also call show()
As an experiment, remove the public specifier from the Balance class and then try
compiling TestBalance. As explained, errors will result.
Using the keyword interface, you can fully abstract a class' interface from its implementation.
That is, using interface, you can specify what a class must do, but not how it does it. Interfaces
are syntactically similar to classes, but they lack instance variables, and their methods are
declared without any body. In practice, this means that you can define interfaces that don't
make assumptions about how they are implemented. Once it is defined, any number of
classes can implement an interface. Also, one class can implement any number of interfaces.
To implement an interface, a class must create the complete set of methods defined by
the interface. However, each class is free to determine the details of its own implementation.
By providing the interface keyword, Java allows you to fully utilize the "one interface,
multiple methods" aspect of polymorphism.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks