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

Comments

Popular posts from this blog

Develop an Android application to display “Hello World!” on screen

Program to design an applet which draws a circle (having color BLUE) inside a triangle (having color YELLOW)

Build a Simple Android App to Display Student Details