BY
G.KARTHIGA
M.SC INFOTECH
DEPARTMENT OF CS&IT
NADAR SARASWATHI COLLEGE OF ARTS &
SCIENCE,THENI.
 Packages are nothing more than the way we organize
files into different directories according to their
functionality and usability.
 A Java package is a Java programming language
mechanism for organizing classes into namespaces.
 Java source files belonging to the same category or
providing similar functionality can include a package
statement at the top of the file to designate the
package for the classes the source file defines.
 To use a package inside a Java source file, it is
convenient to import the classes from the package
with an import statement.
 import java.awt.event.*;
 The above statement imports all classes from the
java.awt.event package.
 Packages are usually defined using a hierarchical
naming pattern, with levels in the hierarchy separated
by periods (.) .
 Although packages lower in the naming hierarchy are
often referred to a "subpackages" of the
corresponding packages higher in the hierarchy, there
is no semantic relationship between packages.
 The package is both naming and visibility control
mechanism.
 It includes a package command as the first statement
in a java source file.
 Any classes declared within that file will belong to
the specified package.
 The package statement defines a name space in which
classes are stored.
 This is the general form of the package statement:
Package pkg;
 Class path environmental variable.
 The use of class path option with java and javac to
specify the path to your classes.
 The following packages specification:
Package mypack
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+”:s”+bal);
}}
Class accountbalance{
Public static void main(string args[]){
Balance current[]=new balance[3];
Current[0]=new balance(“k.j.fielding”,123.23);
}
For(int i=0;i<3;i++)
current[i].show();
}}
 Classes and package are both means of encapsulating
containing the name space and scope of variable and
methods.
 Packages act as containers for classes and others
subordinate packages.
 Classes act as a container for data and code.
Private No modifier Protected Public
Same class Yes Yes Yes Yes
Same package
subclass
No Yes Yes Yes
Same package non
subclass
No Yes Yes Yes
Different package
subclass
No No Yes Yes
Different package
non subclass
No No No Yes
 In a java source file, import statement occur
immediately following the package statement and
before any class definition.
 This is the general form of the import statement:
 Example:
Import java.io.*;
Import java.util.Scanner;
Import pkg1[pkg2].classname .*;
 An interface is a reference type in Java, it is similar to
class, it is a collection of abstract methods.
 A class implements an interface, thereby inheriting
the abstract methods of the interface.
 Along with abstract methods an interface may also
contain constants, default methods, static methods,
and nested types.
 Method bodies exist only for default methods and
static methods. Method bodies exist only for default
methods and static methods.
 The interface keyword is used to declare an interface.
Below given is an example of an interface:
/* File name : NameOfInterface.java
*/ import java.lang.*;
//Any number of import statements
public interface Name Of Interface
{ //Any number of final, static fields
//Any number of abstract method
declarations
}
 The general form of interface:
 When a class implements an interface, you can think
of the class as signing a contract, agreeing to perform
the specific behaviors of the interface.
 A class uses the implements keyword to implement
an interface. The implements keyword appears in the
class declaration following the extends portion of the
declaration.
 Example class that implements the callback interface:
Class client implements callback
{
//implements callback’s interface
Public void callback(int p)
{
System.out.println(“callback called
with”+p);
}
}
 An interface can be declared a member of a class or
another interface.such interface called a member
interface or a nested interface.
 A nested interface can be declared as public,private
or protected.
 When a nested interface is used outside of its
enclosing scope, it must be qualified by the name of
the class or interface of which it is a member.
 An interface can extend another interface, similarly to
the way that a class can extend another class.
 The extends keyword is used to extend an interface,
and the child interface inherits the methods of the
parent interface.
 Example of one interface:
//one interface can extended another
Interface A{
Void meth1();
void meth2();
}
//B includes meth1() and meth2()..it adds
meth3()
Interface B extends A{
Void meth3();}

Packages and interface

  • 1.
    BY G.KARTHIGA M.SC INFOTECH DEPARTMENT OFCS&IT NADAR SARASWATHI COLLEGE OF ARTS & SCIENCE,THENI.
  • 2.
     Packages arenothing more than the way we organize files into different directories according to their functionality and usability.  A Java package is a Java programming language mechanism for organizing classes into namespaces.  Java source files belonging to the same category or providing similar functionality can include a package statement at the top of the file to designate the package for the classes the source file defines.
  • 3.
     To usea package inside a Java source file, it is convenient to import the classes from the package with an import statement.  import java.awt.event.*;  The above statement imports all classes from the java.awt.event package.
  • 4.
     Packages areusually defined using a hierarchical naming pattern, with levels in the hierarchy separated by periods (.) .  Although packages lower in the naming hierarchy are often referred to a "subpackages" of the corresponding packages higher in the hierarchy, there is no semantic relationship between packages.  The package is both naming and visibility control mechanism.
  • 5.
     It includesa package command as the first statement in a java source file.  Any classes declared within that file will belong to the specified package.  The package statement defines a name space in which classes are stored.  This is the general form of the package statement: Package pkg;
  • 6.
     Class pathenvironmental variable.  The use of class path option with java and javac to specify the path to your classes.  The following packages specification: Package mypack
  • 7.
    Package mypack; Class balance{ String name; Double bal; Balance (String n,double b){ Name=n; Bal=b; } Void show(){ If (bal<0)
  • 8.
    System.out.println(“-->”); System.out.println(name+”:s”+bal); }} Class accountbalance{ Public staticvoid main(string args[]){ Balance current[]=new balance[3]; Current[0]=new balance(“k.j.fielding”,123.23); } For(int i=0;i<3;i++) current[i].show(); }}
  • 9.
     Classes andpackage are both means of encapsulating containing the name space and scope of variable and methods.  Packages act as containers for classes and others subordinate packages.  Classes act as a container for data and code.
  • 10.
    Private No modifierProtected Public Same class Yes Yes Yes Yes Same package subclass No Yes Yes Yes Same package non subclass No Yes Yes Yes Different package subclass No No Yes Yes Different package non subclass No No No Yes
  • 11.
     In ajava source file, import statement occur immediately following the package statement and before any class definition.  This is the general form of the import statement:  Example: Import java.io.*; Import java.util.Scanner; Import pkg1[pkg2].classname .*;
  • 12.
     An interfaceis a reference type in Java, it is similar to class, it is a collection of abstract methods.  A class implements an interface, thereby inheriting the abstract methods of the interface.  Along with abstract methods an interface may also contain constants, default methods, static methods, and nested types.  Method bodies exist only for default methods and static methods. Method bodies exist only for default methods and static methods.
  • 13.
     The interfacekeyword is used to declare an interface. Below given is an example of an interface: /* File name : NameOfInterface.java */ import java.lang.*; //Any number of import statements public interface Name Of Interface { //Any number of final, static fields //Any number of abstract method declarations }
  • 14.
     The generalform of interface:
  • 15.
     When aclass implements an interface, you can think of the class as signing a contract, agreeing to perform the specific behaviors of the interface.  A class uses the implements keyword to implement an interface. The implements keyword appears in the class declaration following the extends portion of the declaration.
  • 16.
     Example classthat implements the callback interface: Class client implements callback { //implements callback’s interface Public void callback(int p) { System.out.println(“callback called with”+p); } }
  • 17.
     An interfacecan be declared a member of a class or another interface.such interface called a member interface or a nested interface.  A nested interface can be declared as public,private or protected.  When a nested interface is used outside of its enclosing scope, it must be qualified by the name of the class or interface of which it is a member.
  • 18.
     An interfacecan extend another interface, similarly to the way that a class can extend another class.  The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface.  Example of one interface: //one interface can extended another Interface A{ Void meth1(); void meth2(); } //B includes meth1() and meth2()..it adds meth3() Interface B extends A{ Void meth3();}