Sunday 25 November 2012

PACKAGES..... CONT.

 HOW TO USE PACKAGE CLASSES AND INTERFACE IN ANOTHER JAVA PROGRAM:
      To be able to relate features classes and connections in JAVA we have two techniques they are using properly certified name strategy.

USING IMPORT STATEMENT:
      import is keyword and key phrase which is used to import either individual category or interface or set of classes and interface all at once.

SYNTAX -1:
     import pack1[.pack2[......[.packn]]].*;

FOR EXAMPLE:
import p1.*;->1
import p1.p2.*;->2
import p1.p2.p3.*;->3


  When affirmation 1 is performing we can import or we can availability all the classes and connections of package p1 only but its sub offers p2 and p3 classes and connections.
When declaration 2 is performing can import as the classes and connections of package p2 only but not p1 and p3 classes and connections.
   When declaration 3 is performing can import as the classes and connections of package p3 only but not p1 and p2 classes and connections.

SYNTAX:
     impoty pack1[.pack2[.......[.packn]]].class name/ interface name;
FOR EXAMPLE:
           import p1.c1;---4
           import p1.p2.c3;----5


    When declaration 4 is performing can import c1 kind of package p1 only but not other classes and connections of p1 package  p2 package and p3 package.

Q.Write a JAVA program which demonstrates the use of package classes?
ANS:
        import approach:
       import tp. Check ;
       category packDemo
        {
             community fixed gap main(string [] args)
              {
                  Check t1 = new Check ();
                   t1.show ();
               }
          };

    When we gather the above program we get the following mistake "package tp does not exit". To prevent the above mistake we must set the classpath as.,
               SET CLASSPATH = %CLASSPATH%;.;
This is the different strategy for import statement:
      p1.c2 o2=new p1.c2 ();
      p1.p2.p3.c4 o4=new p1.p2.p3.c4 ();
      p1.p2.i3 o3=new p1.p2.p3.c4 ();

FULLY QUALIFIED APPROACH:
class packdemo
{
     community fixed gap primary (string[] args)
       {
          tp.Test t1=new tp.Test ();
          t1.show ();
         }
};


    Whenever we create customer defined offers to use the classes and interface of  customer described offers in some other program we must set category before there consumption.
    To be able to set the classpath for predetermined offers we must use the following statement:

D:\core\set classpath=C: \program file\java\jdk1.5.0\lib\rt.jar;.;
[rt.jar contains all the category data files for the predetermined classes which are provided as a slot of predetermined offers by SUN small program.]
   
              When two classes or an interface connected to the same package and if they want to relate those or connections need to be known by its package name.

FOR EXAMPLE:

      //I1.java
      //  javc -d . I1.java
      package interface I1
      {
           community gap f1();
           community gap f2();
        };
 
      //c01.java
      //javac -d . c01.java
      paclage cp;
      community summary category c01 utilizes ip.I1
        {
             community gap f2()
                {
                      Program.out.println ("F1 -C01");
                 }
           };

   //C02.java
   // javac -d . C02.java
    package cp;
    community category C02 expands C01
     {
              community gap f2 ()
               {
                    Program.out.println ("F2 - C02");
                }
      community gap f1 ()
        {
            extremely. f1 ();
             Program.out.println (F1 - C02 - OVER RIDDEN");
          }
};

  //packDemo.java
 // javac packDemo.java
 import ip.I1;
Class packDemo
{
       community fixed gap primary (string [] args)
         {
              Program.out.println ("CP.C02");
              cp.C02 o2=new cp.C02 ();
              o2.f1 ();
               o2.f2 ();
               Program.out.println ("CP.C01");

                cp.Co1 o1=new cp.C02 ();
                o1.f1 ();
                o1.f2 ();

                I1 io;
                io=o1;  //new cp.C02 ();
                io.f1 ();
                io.f2 ();
            }
};

In purchase to run the above program we must run with regard to package name.

     javac -d.packDemo1.java
     java mp.packDemo 1 or java mp/PackDemo1

In purchase to use the idea of availability specifiers. In JAVA we have four kinds of availability specifiers. They are personal standard (not a keyword) secured and community Access specifiers creates us to comprehend how to availability the category interface to interface and connections to category and across the package (class to category interface to connections to class).In other terms availability specifier signify exposure of information or availability if information.

Syntax for announcing a varying along with
         [Access specifier] [static] [final] details kind v1 [=val1], v2[=val2]...vn[=valn];

No comments:

Post a Comment