• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Packages » Adding Classes From A Package to Your Program
Next →
← Prev

Adding Classes From A Package to Your Program

By Dinesh Thakur

The packages are used for categorization of the same type of classes and interface in a single unit. There is no core or in-built classes that belong to unnamed default package. To use any classes or interface in other class, we need to use it with their fully qualified type name. But some time, we’ve the need to use all or not all the classes or interface of a package then it’s a tedious job to use in such a way discussed. Java supports imports statement to bring entire package, or certain classes into visibility. It provides flexibility to the programmer to save a lot of time just by importing the classes in his/her program, instead of rewriting them.

In a Java source file, import statements occur immediately following the package statement (if it exists) and must be before of any class definitions. This is the general form is as follows:

 

import pkg1 (.pkg2). [classname|*l;

For example

import java.util.Vector;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

 

The star (*) increases the compilation time, if the package size is very large. A good programming methodology says that give the fully qualified type name explicitly, i.e. import only those classes or interface that you want to use instead of importing whole packages. Yet the start will not increase the overhead on run-time performance of code or size of classes.

 

Example:

//Student.java

package student;

class Student

{

private int rollno;

private String name;

private String address;

public Student(int rno, String sname, String sadd}

{

 

rollno = rno;

name = sname;

address = sadd;

}

public void show()

{

System.out.println(“Roll No :: ” + rollno);

System.out.println(“Name :: ” + name);

System.out.println(“Address :: ” + address);

}

}

// Test.java

 

package student;

class Test extends Student

 

{

protected int marksSubjecti;

protected int marksSubject2;

protected int marksSubject3;

protected int marksSubject4;

public Test(int rno, String sname, String sadd,int mi, int m2, int m3, int m4)

{

super(rno,sname,sadd);

marksSubjecti = mi;

marksSubject2 = m2;

marksSubject3 = m3;

marksSubject4 = m4;

}

public void show()

{

super.show();

System.out.println(“Marks of Subject1 :: ” + marksSubject1);

System.out.println(“Marks of Subject2 :: ” + marksSubject2);

System.out.println(“Marks of Subject3 :: ” + marksSubject3);

System.out.println(“Marks of Subject4 :: ” + marksSubject4);

}

}

 

//Result.java

 

package student;

public class Result ex..tends Test

{

private int totalMarks;

private float percentage;

private char grade;

public Result(int rno, String sname, String sadd,int mi, int m2, int m3, int m4)

{

super(rno,sname,sadd,ml,m2,m3,m4);

 

totalMarks = marksSubject1 + marksSubject2 + marksSubject3 + marksSubject4;

percentage = (totalMarks*100.00F/600.00F);

if (percentage >=50.00F)

grade=’D’;

else

if(percentage >=55.00F && percentage<=60.00F)

grade = ‘C’;

else

if (percentage >=6l.00F && percentage<=70.00F)

grade = ‘B’;

else

if(percentage >=7l.00F && percentage<=75.00F)

grade = ‘A’;

else

if (percentage >=76.00F && percentage<=85.00F)

grade = ‘H’;

else

grade = ‘S’;

}

public void show()

{

super.show();

System.out.println(“Total Marks :: ” + totalMarks);

System.out.println(“Percentage :: ” + percentage);

System.out.println(“Grade :: ” + grade);

}

}

//ImportPackageDemo.java

import student.Result;

public class ImportPackageDemo

{

public static void main(String ar[])

{

Result ob = new Result (1001, “Alice”, “New York”,135,130,132,138);

ob.show ();

}

}

In the source file ImportPackageDemo, import student. Result is the first statement; it will import the class Result from the student package.

You’ll also like:

  1. Write A C++ Program For Adding Rational Numbers & Display OUTPUT As Numerator / Denominator.
  2. Write A C++ Program For Pointer To Classes Example.
  3. Write a C++ program for Container Classes.
  4. Basic Structure of A Complete C++ Program With Classes
  5. Write A C++ Program How: Structures And Classes Are Related.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW