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

Popular posts from this blog

Program to define a class 'employee' with data members as empid, name and salary. Accept data for 5 objects using Array of objects and print it.

Define a class Student with four data members such as name, roll no.,sub1, and sub2. Define appropriate methods to initialize and display the values of data members. Also calculate total marks and percentage scored by student.

Program to input age from user and throw user-defined exception if entered age is negative