Monday, 4 February 2013

PACKAGE AND INTERFACE | Java J2EE Tutorial pdf

PACKAGE AND INTERFACE

PACKAGE:-
There is a large set of predefined classes, grouped into packages. The full name of one of these predefined classes includes the name of the package as prefix. For example, the library class java.util.Random is in package java.util, and a program may use the class with code like this:
java.util.Random r = new java.util.Random();
The import statement allows we to omit the package name from one of these classes. A Java program that includes the line
import java.util.Random;
can abbreviate the use of Random to
Random r = new Random();
We can import all the classes in a package at once with a notation like
import java.util.*;
The package java.lang is special; every program behaves as if it started with
import java.lang.*;
whether it does or not. We can define our own packages, but defining packages is an advanced topic beyond the scope of what's required for this course.
The import statement doesn't really "import" anything. It just introduces a convenient abbreviation for a fully-qualified class name. When a class needs to use another class, all it has to do is use it. The Java compiler will know that it is supposed to be a class by the way it is used, will import the appropriate .class file, and will even compile a .java file if necessary. (That's why it's important for the name of the file to match the name of the class). 
For example, here is a simple program that uses two classes:
public class HelloTest {
public static void main(String[] args) {
Hello greeter = new Hello();
greeter.speak();
}
}
public class Hello {
void speak() {
System.out.println("Hello World!");
}
}

Put each class in a separate file (HelloTest.java and Hello.java). Then try this:
javac HelloTest.java
java HelloTest

We should see a cheery greeting. If we type ls we will see that we have both HelloTest.class and Hello.class even though we only asked to compile HelloTest.java. The Java compiler figured out that class HelloTest uses class Hello and automatically compiled it. Try this to learn more about what's going on:
rm -f *.class
javac -verbose HelloTest.java
java HelloTest
 
INTERFACE
The interface keyword takes the abstract concept one step further. We could think of it as a “pure” abstract class. It allows the creator to establish the form for a class: method names, argument lists, and return types, but no method bodies. An interface can also contain fields, but these are implicitly static and final. An interface provides only a form, but no implementation.
To create an interface, use the interface keyword instead of the class keyword. Like a class, we can add the public keyword before the interface keyword (but only if that interface is defined in a file of the same name) or leave it off to give “friendly” status so that it is only usable within the same package. To make a class that conforms to a particular interface (or group of interfaces) use the implements keyword. Once we’ve implemented an interface, that implementation becomes an ordinary class that can be extended in the regular way. 
A simple example is as follows:
interface Area{
float pi=3.14f;
float compute (float x, float y);
}
class rect implements Area{
public float compute(float x, float y){
return x*y;
}
}
class cir implements Area{
public float compute(float x, float y){
return pi*x*y;
}
}
class intfacetest {
public static void main(String arg[]) [
rect r=new rect();
cir c=new cir();
System.out.println(“Rect= “+r.compute(10.20));
System.out.println(“Cir= “+c.compute(10.20));
}
}

No comments: