Java Packages & Interfaces
Packages are containers for classes that are used to keep the class name space compartmentalized. For example, a package allows you to create a class named List, which you can store in your own package without concern that it will collide with some other class named List stored elsewhere.Through the use of the interface keyword, Java allows you to fully abstract the interface from its implementation. Using interface, you can specify a set of methods which can be implemented by one or more classes. The interface, itself, does not actually define any implementation.
This is the general form of the package statement:
package package_name;
A Short Package Example
Keeping the preceding discussion in mind, you can try this simple package:// A simple package
package MyPack;
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n;
bal = b;
}
void show() {
if(bal<0 font="">0>
System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
class AccountBalance {
public static void main(String args[]) {
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3 current="" font="" i="" show="">3>
}
}
No comments:
Post a Comment