Monday, 28 November 2016

Program to import the class A from package p1

In previous program, we have crated a package named "p1" and added class "A" in that package. Now in this program we use that package "p1" to import the class "A" in class "PackageExample".
import keyword is used to import built-in and user-defined packages into your java source file. There are 3 different ways to refer to class that is present in different package
  1. Using fully qualified name
  2. class MyDate extends java.util.Date{
     // statement;
    }
  3. import the only class you want to use.
  4. import java.util.Date;
     
    class MyDate extends Date{
     // statement;
    }


  5. import all the classes from the particular package
  6. import java.util.*;
     
    class MyDate extends Date{
     // statement;
    }
Remember that, if there are both package and import statement in your source file then the import statement is after package statement.
In this program, we have imported the class A from package p1 usnig the import statement as below:
import  p1.A;

PROGRAM
import p1.A;
class PackageExample
{
 public static void main(String args[])
 {
  A a1 = new A();
  a1.dispA();
 }
}
OUTPUT
C:\>javac PackageExample.java
C:\>java PackageExample
Inside from Class A

No comments:

Post a Comment