Package and subpackages in java


A package is a group of similar types of class, interfaces and sub-packages.
Package can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java.lang, awt, javax, swing, net, io, util, sql etc.
package in java :: futureX

In this page, we will have the detailed learning of creating user-defined packages.

Advantage of Package

  • Package is used to categorize the classes and interfaces so that they can be easily maintained.
  • Package providEs access protection.
  • Package removes naming collision.

Example

To compile : javac -d . Simple.java
To run       : java mypack.Simple

package com;
class Simple{
public static void main(){
System.out.println("hello java");
}
}

The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The . represents the current folder.


How to access package from another package?

  • import package.*;
  • import package.classname;
  • fully qualified name.



The import keyword is used to make the classes and interface of another package accessible to the current package.



If you use package.* then all the classes and interfaces of this package will be accessible but not subpackages.

If you import package.classname then only declared class of this package will be accessible but not subpackages.

If you use fully qualified name then only declared class of this package will be accessible. Now there is no need to import. But you need to use fully qualified name every time when you are accessing the class or interface.

package math;
public class Calc{
public static int cube(int n){return n*n*n;}
}


//import all the classes of that package

package com;
import math.*;
class Simple{
public static void main(String args[]){
System.out.println(Calc.cube(5));
}
}

//only declared class is acessible

package com;
import math.Calc;
class Simple{
public static void main(String args[]){
System.out.println(Calc.cube(5));
}
}




//without import, full qualified name

package com;
class Simple{
public static void main(String args[]){
System.out.println(math.Calc.cube(5));
}
}


Subpackages:

Package inside the package is called the subpackage. It should be created to categorize the package further. Let's take an example, Sun Microsystems has defined a package named java that contains many classes like System, String, Reader, Writer, Socket etc. These classes represent a particular group e.g. Reader and Writer classes are for Input/Output operation, Socket and ServerSocket classes are for networking etc and so on. So, Sun has subcategorized the java package into subpackages such as lang, net, io etc. and put the Input/Output related classes in io package, Server and ServerSocket classes in net packages and so on.

standard declaration:
com.companyname.packagename.classname

package com.techshakti.math;
public class subpackage{
public static int cube(int n){return n*n*n;}
}


                                          JAVA TUTORIALS HOMEPAGE

0 comments:

Abstraction in java (interface class)

interface in java :: futureX

Interface: An interface is a blueprint of a class. It has static constants and abstract methods.
The interface is a mechanism to achieve abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritance in Java.

Why use Interface?

It is used to achieve fully abstraction.
By interface, we can support the functionality of multiple inheritance.
It can be used to achieve loose coupling
the java compiler converts methods of interface as public and abstract, data members as public,final and static by default.

Example :

interface Shape{
void draw();
}

class Circle implements Shape{
public void draw(){System.out.println("drawing circle");}
}

class Rectangle implements Shape{
public void draw(){System.out.println("drawing rectangle");}
}

class Test{
public static void main(String args[]){
Shape S=new Circle();
S.draw();

}}

multiple inheritance by interface :

interface Shape{
void draw();
}

interface Color{
String getColor();
}


class Rectangle implements Shape,Color{
public void draw(){System.out.println("drawing rectangle");}
public String getColor(){return("red");}
}

class Test{
public static void main(String args[]){
Rectangle s=new Rectangle();
s.draw();
System.out.println(s.getColor());
}}

                                            JAVA TUTORIALS HOMEPAGE

0 comments:

Copyright © 2013 futureX | Blogger Template by Clairvo