Thursday, June 22, 2017

Java-packages

packages
--------
A package is collection
classes
interfaces
enum

It is a reusable component
It provides code modularization
It avoids naming conflict.
It provides code optimization.
Maintenance of project is easy using packages.


Types of packages
-----------------
i)Predefined package.
ii)Userdefined package.


Built-in/existing packages are predefined.

java.lang.*;
It is a General purpose package which consists of classes and interfaces like
System
String
Runnable
etc

It is also called default package because
it is automatically imported by a compiler if developer is not importing.

java.util.*;
It  consists of classes like
Stack,Queue,LinkedList etc.

java.io.*;
It is used to perform input/output operations(file operations)
FileInputStream.
FileOutputStream.

java.sql.*;
It is used to perform jdbc operations.
It consists of interfaces like
Connection
Statement
etc.

java.applet.*;
java.awt.*;
javax.swing.*;

The above packages are used to perform GUI programming.

Userdefined packages
--------------------
package defined or created by developer is known as userdefined package.


How to declare a package.
-------------------------
we declare a package by using a keyword "package".

syntax
------
package packagename;

ex:
package  pack1.*;

package declarartion must be the first statetment in a program.

packages must be declared in lowercase.


why packages
------------
Grouping related classes into  a single unit.

Reusability.

packages avoids naming conflict.

packages supports code modularization.

readability.


creating userdefined package;

syntax
------
package packagename;
accessmodifier class Class1
{
}
accessmodifier class Class2
{
}
.
.
accessmodifier interface Interface1
{
}
accessmodifier interface Interface2
{
}
.
.
accessmodifier enum EnumName1
{
}
.
.


Program to add a class to package
---------------------------------
package pack1;
public class A
{
  public void m1()
  {
    System.out.println("m1");
  }
}

How to save a package.

save as
ClassName.java
ex:
A.java

How to compile a package
------------------------
javac -d . Classname.java

-d is a flag which creates a directory(folder) with the name of a package declared in a program.

.(dot) represents current directory.

similary Add 2 more classes to a package
package pack1;
public class B
{
  public void m1()
  {
    System.out.println("m1");
  }
}
save as B.java


package pack1;
public class C
{
  public void m1()
  {
    System.out.println("m1");
  }
}
save as C.java

importing packages
------------------
We import a package by using a new keyword "import".

Types of import
---------------
implicit.
explicit.

implicit import
---------------
All the members of a package are automatically available for  a program,such an import is called implicit import.


syntax
------
import packagename.*;

ex:
import java.lang.*;
import pack1.*;

Program to demonstrate implicit import
--------------------------------------
import pack1.*;
class Test
{
  public static void main(String args[])
  {
      A a=new A();
      a.m1();

      B b=new B();
      b.n1();
 
      C c=new C();
      c.n1();

  }
}

Explicit import
---------------
Import a specific class/interface/enum to  a program is known as explicit import.

syntax
------
import packagename.ClassName;
or
import packagename.InterfaceName;
or
import packagename.EnumName;

ex:
import java.lang.Math;
import pack1.A;

Program to demonstrate explicit import
--------------------------------------
import pack1.A;
import pack1.B;

class Test
{
  public static void main(String args[])
  {
      A a=new A();
      a.m1();

      B b=new B();
      b.n1();
 
   
  }
}

Here only A,B classes are imported to Test class.


static import
-------------
A static import is used to invoke static methods of a class without using classname.


syntax
------
import static packagename.ClassName.*;

ex:
import static packagename.Math.*;

Program
-------
import static java.lang.Math.*;
public class Test  {
public static void main(String[] args) {

       System.out.println(sqrt(25.0));
       System.out.println(sqrt(45.0));
       System.out.println(sqrt(36.0));
       System.out.println(sqrt(64.0));
     }
}

In the above code we are calling sqrt() method of Math class without using classname,this is possible because we are using static import.

static import increases performance of an application but at the same time it is confusing to the developer.

Fully Qualified Name of a class
-------------------------------
we can directly use a class or member of a package in a program by using fully qualified name without importing a package.

syntax
------
packagename.membername

ex:
java.util.Scanner

Program
-------
public class Test  {
public static void main(String[] args) {
int a;
java.util.Scanner  scanner=new java.util.Scanner(System.in);
System.out.println("enter a");
a=scanner.nextInt();
System.out.println(a);
   
}
}

This is used for instant purpose.

creating packages in eclipse
----------------------------
open eclipse-->

file-->new--->java project-->

projectname--->packages

select-->use default jre.

goto package explorer-->Expand project-->src-->rightclick-->new-->package-->
                    name--->test

Adding a class to a package in eclipse
--------------------------------------
Goto Project-->packages-->src-->test-->right click on package-->new-->class-->
                   Name-->A-->ok

Program
-------
package test;

public class A {
public void m1()
{
System.out.println("m1");
}
}


Add 1 more classe to test package

package test;

public class B {
public void m1()
{
System.out.println("B");
}
}

importing classes  of one package in another package in eclipse
-------------------------------------------
create another package pack1.

Add a class Demo.

package pack1;

import test.A;
import test.B;
public class Demo {
public static void main(String[] args) {
A a=new A();
B b=new B();
a.m1();
b.m1();

}

}

accessmodifiers in packages
---------------------------
public members are accessible through out project.

default members are accessible within a package.

private members are accessible within a class itself.

protected members are accessible anywhere in same package and by sub class of other package.

Program
-------
package test;
public class A {
protected void m1()
{
System.out.println("A");
}
}


package pack1;
import test.A;
public class Demo extends  A{
public static void main(String[] args) {
        Demo d=new Demo();
        d.m1();
}
}

In the above program we are invoking the protected method m1() from test package in pack1 by extending class A to Demo as shown in the above code.

Nested packages
---------------
A package within another package is known as nested package.

syntax
------
package package1.package2.package3...;

ex:
package pack1.pack2;

Nested packages provides better modularization of related classes,interfaces,enum.

No comments: