Inheritance, Interface and Package: K. K. Wagh Polytechnic, Nashik-3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 5

K. K.

Wagh Polytechnic, Nashik-3

Chapter 3

Inheritance, Interface and Package


Package
• Package is Java’s way of grouping the classes and interfaces
together. That is, a package is called as the collection of classes
and interfaces.
1.Classes enclosed in packages of other programs can be
reused.
2.Two classes in two different packages can have same name
but they should be referred by their ‘fully qualified name’.
3.Packages provide a way of hiding classes. So we can prevent
them from accessing outside of package.
4.It is also possible to change the implementation of any
method without affecting the rest of the design.
Package Cont.
Steps for Creating the package
1) Create a package in your current working directory with
following Syntax: package packagename;
Ex: package MyPackage;
2) Declare a class inside package as public
Example:
public class MyClass
{
//body of the class
}
3) Compile the class and store its .class file in the package
Creating Hierarchy:
package pkg1.pkg2.pkg3;
package java.awt.image;

4) Use of package:
import package.classname;
static import method
import static java.lang.Math.sqrt;
import static java.lang.Character.isUpperCase;
import static java.lang.System.out;
class StaticImport
{
public static void main(String args[ ])
{
double x = sqrt(52); //method1
out.print("Square root of 52: ");
out.println(x); //method2

char ch = 'D';
if(isUpperCase(ch)) //method3
out.println(ch+" is uppercase");
}
}

You might also like