Ch-5 Java
Ch-5 Java
Packages:
A package is a group of classes and interfaces.
Package can be categorized in two form:
built-in package such as java, lang, awt, javax, swing, net, io, util, sql etc.
user-defined package which is created by user.
Advantages of Packages:
Package is used to categorize the classes and interfaces so that they can be easily
maintained.
Package provides access protection.
Package removes naming collision.
package java.awt.image;
Needs to be stored in java\awt\image in a Windows environment.
You cannot rename a package without renaming the directory in which the classes are stored
Example :
Save program in your directory(e.g.-D:\Java_prog\Package\A.java)
Compile using D:\>Java_prog\Package\javac –d . A.java
Then pack directory has been generate with A.class file.
Then run program D:\>Java_prog\Package\java pack.A
package pack;
public class A
{
public static void main(String args[])
{
System.out.println("B. S. Patel Polytechnic");
}
}
Access Description
protection
No modifier The classes and members specified in the same package are accessible to
(default) all the classes inside the same package.
public The classes, methods and member variables under this specifier can be accessed
from anywhere.
protected The classes, methods and member variables under this modifier are accessible by
all subclasses,
and accessible by code in same package.
private The methods and member variables are accessible only inside the class.
Importing package
In a Java source file, import statements occur immediately following the package statement (if it
exists) and before any class definitions.
This is the general form of the import statement:
import pkg1[.pkg2].(classname|*);
Here, pkg1 is the name of a top-level package, and pkg2 is the name of a subordinate package
inside the outer package separated by a dot (.).
There is no practical limit on the depth of a package hierarchy, except that imposed by the file
system.
Finally, you specify either an explicit classname or a star (*), which indicates that the Java
compiler should import the entire package.
This code fragment shows both forms in use:
import java.util.Date;
import java.io.*;
Example:
package pack;
public class A
{
int a;
int b;
A(int i,int j)
{
a=i;
b=j;
}
public void display()
{
System.out.println(a + "&" + b);
}
}
Interfaces
Interface is just like a class.
Interface declares with interface keyword.
Interface does not have any implementation of method.
In Java Interface defines the methods but does not implement them.
Interface is collection of methods and variables.
In interface variable consider as a constant and method consider as abstract.
More than one class can be implement interface.
Interface extends two or more interfaces.
It means using interface we can create multiple inheritance.
Syntax:
[access] interface InterfaceName [extends OtherInterface]
{
return-type method-name1(parameter-list);
return-type method-name2(parameter-list);
type final-varname1 = value;
type final-varname2 = value;
// ...
return-type method-nameN(parameter-list);
type final-varnameN = value;
}
Implementing Interfaces
Once an interface has been defined, one or more classes can implement that interface.
To implement an interface, include the implements keyword in a class definition, and then
create the methods defined by the interface.
The general form of a class that includes the implements clause looks like this:
class classname [extends superclass] [implements interface [,interface...]]
{
// class-body
}
If a class implements more than one interface, the interfaces are separated with a comma.
If a class implements two interfaces that declare the same method, then the same method
will be used by clients of either interface.
The methods that implement an interface must be declared public.
Example:-1
interface I1
{
public int a=10;
public int b=20;
public void sum();
}
class one implements I1
{
public void sum()
{
System.out.print("Sum-->"+(a+b));
}
}
class two
{
public static void main(String[] args)
{
one ob1=new one();
ob1.sum();
}
}
Example:-2
interface I1
{
public void show();
}
interface I2
{
public void show1();
}
class A implements I1,I2
{
public void show() {
System.out.println("i am the implentation of interface first"); }
public void show1() {
System.out.println("I am the implentation of interface second"); }
}
class interface1
{
public static void main(String[] args) {
A a=new A();
a.show();
a.show1();
}
}