This site javatpoint is very useful for programming languages like JAVA,PYTHON,C,C++, Data Structures etc.Java learning Concept also provides technical materials , which are related to latest technologies. We provides very easy lectures to our viewers from beginning to advance level.We focus logical content instead of theoretical content.

Thursday, 26 October 2017

JAVA PACKAGES

JAVA PACKAGES:

A major feature of oop is the ability to reuse the code which has already been created. One way of achieving this is by extending the classes and implementing the interfaces. That is, inheritance and interfaces, which have been already discussed.  This two ways is reusing the code is limited to the program. If there is a need to repeated.
Package can be divided into two types, built package and user define package there are many built packages such as Java, Lang, awt,  io, util etc.
               Import Java. Lang. *;
In the above statement, Java is the base or core package and lang is a subpackage. * symbol denotes the built classes, methods and data members available within the subpackage of Lang. Import is the keyword to retrieve all the built classes and methods of lang package  to be used in a program.

Creating packages

If the class or interface is created inside a package, then the declaration of the particular package name should be used. To create a package,  first declare the name of the package using package keyword followed by the package name. Then the class is defined as just a normal class. The general form of package declaration is

Package package -name; 
Public class first
{
            Body of class

}

Benefits of a Java package 

Java packages are used to categorize theclasses and interfaces so that can be easily maintained.  Java packages provides access protection. Java packages removes naming collisions. 
  • The classes in the packages of other programs can be easily accessed. 
  • If two classes are in two different packages,  they can have the same name, they may be refered by their fully qualified name, comparising the package name and class name
  • Packages provide a way to hide classes, thus preventing other programs or packages from accessing classes that are meant for internal use only. 
  • Packages are also used to separate the design from coding. First design classes and their relationships are define, and then this will implement the code needed for the methods, without affecting the design. 

Example:



package vm;
   class A
{
public static void main(String args[])
{
System.out.println("welcome java learning point");
}
}
compile: d:\pc> javac -d .  A.java
run : d:/pc> java   vm.A



op:welcome java learning point

Adbox