Sie sind auf Seite 1von 10

Packages And Interfaces

1.0 Introduction:

Packages :are containers for classes. Packages help in avoiding naming conflicts. For example, if
you store a class named List in one package, another person can store a class again named as List,
but in another package. In other words a package is nothing but a name space(where names are
stored, such as names of a class etc).

Interfaces :So far we have seen methods that act as interfaces to the data in the class.

Through the use of the key word interface, one can abstract the, interface with out
implementation.

Using interface key word, we can specify a set of methods which can be implemented by one or
more classes.

The interface, itself, does not actually define any implementation.

Although an interface, is similar to an abstract class but the advantage with interface is, a class
can implement more than one interface but where as a class can only inherit, one abstract class as,
super class.

By now, we should have known that a java source file can contain any (or all) of the following:

• A single package statement (optional).


• Any number of import statements(optional).
• A single public class declaration(required).
• Any number of classes private to the package(optional).

2.0 Packages :

We can define classes inside a package that are not accessible by code outside that package.

One can also define class members that are only exposed to other members of the same package.
This allows your classes to have intimate knowledge of each other, but not exposing that
knowledge to the rest of the world.

2.1 Defining a Package:

• To create a package just simply include a package command as the first statement in a Java
Source file. Any classes declared within that file will belong to the specified package.
• This is the general form of the package statement :

package pkg;

here, pkg is the name of the package.

For example, the following statement creates a package called MyPackage.


package MyPackage;

• The package statement simply specifies to which package the classes defined the file
belong.
• Java uses file system directories to store packages. That is, those .class files which we want
them to be part of MyPackage must be stored in a directory called MyPackage.

Remember :

ü The directory name must match the package name exactly.

ü More than one file can include the same package statement.

ü One cannot rename a package without renaming the directory in which the classes are stored.

ü One can create a hierarchy of packages. To do so, simply separate each package name from
then one above with a period. The general form of a multileveled package statement is shown
below

package pkg1[.pkg2[.pkg3[.pkg4]]];

for example a package declared as

package java.awt.image;

needs to be stored in java\awt\image.

2.2 Finding Packages and CLASSPATH

As we already know that packages known with their, respective directories. Then how does java
run time system know where to look for the packages that we created?

There are two answers :

• First ,if your package is in the current directory or a sub directory of the current directory, it
will be found because by default, the java run-time system uses the current working
directory.
• Second, we can specify a directory path or paths by setting the CLASSPATH environment
variable.

The second alternative lets us find package no matter what directory you are in. finally the option
is yours.

// 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)

System.out.println(" ---> ");

System.out.println(name+" Rs "+bal);

class AccountBalance

public static void main(String args[])

Balance ob[] = new Balance[3];

ob[0] = new Balance("A L Raju",123.3);

ob[1] = new Balance("Rajesh",234.56);

ob[2] = new Balance("Sangeet",-12.33);


for (int i=0;i

ob[i].show();

Name this file as AccountBalance.java, and put it in a directory called MyPack.

Compile the file with in the directory MyPack, so that the .class should be in MyPack.

Now move to the directory just above, the MyPack directory and since, AccountBalance is now
a part of the package MyPack, Accountbalance should be qualified with its package, to
getexecuted, as shown below

java MyPack.AccountBalance

3.0 Interfaces

• Using the keyword interface, one can fully abstract a class's interface with out
implementation.
• Using interface, one can specify what a class must do but not how it does.
• interfaces are syntactically, similar to classes, but they lack instance variables and their
methods are declared without any body.
• Any number of classes can implement an interface.
• A class can implement any number of interfaces.
• When a class implements an interface, class should override all the methods in the
interface. However, each class is free to implement its own details, with in a method.

3.1 Defining an Interface

An interface is defined much like a class. The general form is as shown below

access interface name

type final-varname1 = value;

type final-varname2 = value;

….

….
type final-varnameN = value;

return-type method-name1(parameter-list);

return-type method-name2(parameter-list);

…..

…..

return-type method-nameN(parameter-list);

Remember

ü Here, access is either public or not used .

ü When no access is specified, default access results, then the interface is only, available to
other members of the package, when the access specifier is public, the interface can be used any
code.

ü Default implementation of the methods, with in an interface is not possible.

ü Any class that implements an interface must implement all of its methods.

ü Variablesdeclaredinside an interface are impl icitly final and static, meaning they cannot be
changed by the implementing class.

ü All methods and variables are implicitly public if the interface, itself, is declared as public.

Here is a simple example of an interface definition.

Interface CallBack

void callback(int param);

3.2 Implementing Interfaces

• To implement an interface use implements clause, with the class definition and implement
all the methods defined by the interface, with in the class.
• While implementing the methods of the interface, with in the class, use public as the access
specifier. Also the type signature's should also match, as specified in the interface
definition.
• The general form of a class that implements an interface is as shown below

access class classname [extends superclass][ implements interface1[,interface2…]]

//class-body

//Here is a small example

interface Callback

void callback(int param);

class Client implements Callback

//observe

public void callback(int p)

System.out.println("callback called with "+p);

return;

class CallBack
{

public static void main(String args[])

Client c = new Client();

c.callback(10);

3.3 Accessing Implementations Through Interface References

• One declare variables as object references that use an interface rather than a class type

//The following example calls the callback() method via an interface reference variable:

interface Callback

void callback(int param);

class Client implements Callback

//observe

public void callback(int p)

System.out.println("callback called with "+p);

return;

}
class TestIface

public static void main(String args[])

Callback c = new Client();

c.callback(10);

• Remember

ü Though, c is declared as reference variable and c can access the callback() method but it
cannot access those members of the Client class which are not mentioned in the interface
CallBack.

ü An interface reference variable only has knowledge of the methods declared in that interface.

3.4 Partial Implentations

If a class includes an interface but does not fully implement the methods defined by that interface,
then that class must be declared as abstract.

3.5 Interfaces Can Be Extended

• One interface can inherit another by use of the keyword extends.


• The general form is shown as below

access interface interface1 [extends interface2[,interface3[,….]]]

//methods declaration

Remember

ü When a class implements an interface that inherits another interface, it(the class) must
provide implementations for all methods defined within the interface Inheritance chain.

• Lets consider the example below


// interface A

interface A

void meth1( );

void meth2( );

// interface B inheriting interface A. now B includes meth1( ),meth2( ) and its own method

// meth3 ( )

interface B extends A

Void meth3( );

//class implementing interface B. now this class must implement all of A and B

class MyClass implements B

public void meth1( )

System.out.println("Implement meth1(). ");

public void meth2()

System.out.println("Implement meth2(). ");

public void meth3()


{

System.out.println("Implement meth3(). ");

class IFExtend

public static void main(String args[ ])

MyClass obj = new MyClass();

obj.meth1();

obj.meth2();

obj.meth3();

Das könnte Ihnen auch gefallen