6 Packages and Interfaces
6 Packages and Interfaces
class Balance {
String name;
double bal;
Balance(String n, double b) {
name = n; bal = b;
}
void show() {
if (bal<0) System.out.print("-->> ");
System.out.println(name + ": $" + bal);
} }
Example: Package
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; i++) current[i].show();
}
}
Example: Package
• Save, compile and execute:
1) call the file AccountBalance.java
2) save the file in the directory MyPack
3) compile; AccountBalance.class should be also in
MyPack
4) set access to MyPack in CLASSPATH variable, or
make the parent of MyPack your current directory
5) run: java MyPack.AccountBalance
• Make sure to use the package-qualified class name.
Importing of Packages
• Since classes within packages must be fully-
qualified with their package names, it would
be tedious to always type long dot-separated
names.
• The import statement allows to use classes or
whole packages directly.
• Importing of a concrete class:
import myPackage1.myPackage2.myClass;
• Importing of all classes within a package:
import myPackage1.myPackage2.*;
Import Statement
• The import statement occurs immediately after the package
statement and before the class statement:
package myPackage;
• import otherPackage1;otherPackage2.otherClass;
class myClass { … }
• The Java system accepts this import statement by default:
import java.lang.*;
• This package includes the basic language functions. Without
such functions, Java is of no much use.
Example: Packages 1
• A package MyPack with one public class Balance.
The class has two same-package variables: public constructor
and a public show method.
package MyPack;
public class Balance {
String name;
double bal;
public Balance(String n, double b) {
name = n; bal = b;
}
public void show() {
if (bal<0) System.out.print("-->> ");
System.out.println(name + ": $" + bal);
}
}
Example: Packages 2
Supported Methods A class can have both an abstract as well Interface can have only abstract methods. Java 8
1 as concrete methods. onwards, it can have default as well as static
methods.
2 Multiple Inheritance Multiple Inheritance is not supported. Interface supports Multiple Inheritance.
Supported Variables final, non-final, static and non-static Only static and final variables are permitted.
3
variables supported.
Implementation A class can implement an interface. Interface can not implement an interface, it can
4
extend an interface.
5 Keyword A class is declared using class keyword. Interface is declared using interface keyword.
Inheritance A class can inherit another class using Interface can inherit only an interface.
6 extends keyword and implement an
interface.
Inheritance A class can be inherited using extends Interface can only be implemented using
7
keyword. implements keyword.
Access A class can have any type of members like Interface can only have public members.
8
private, public.
9 Constructor A class can have constructor methods. Interface can not have a constructor.
Defining an interface
B extends A:
interface B extends A {
void meth3();
}
Example: Interface Inheritance 2