Packages and
Interfaces
• Packages are containers for classes. They are used to keep the class
name space compartmentalized.
• Defining a Package: package keyword
• This is the general form of the package statement:
• package pkg; Here, pkg is the name of the package.
• For example, the following statement creates a package called
mypackage:
• package mypackage;
• Packages help organize your Java code and prevent naming conflicts.
You can create a hierarchy of
packages.
• To do so, simply separate each package name from the one above it
by use of a period.
• The general form of a multileveled package statement is shown here:
package pkg1[.pkg2[.pkg3]];
• A package hierarchy must be reflected in the file system of your Java
development system.
• For example, a package declared as package a.b.c;
• Be sure to choose your package names carefully. You cannot rename
a package without renaming the directory in which the classes are
stored
Accessing package
• 1.it can access with proper name in main directory.
• 2.it can access through subdirectory.
• 3. CLASSPATH
• For example, in a Windows environment, if the path to mypack is C:\
MyPrograms\Java\mypack
• then the class path to mypack is C:\MyPrograms\Java
10.Develop a java program to create a package
named mypack and import and implement in a
suitable class
• Public class mypack{
• Public void display()
• {
• System.out.println(“this a method from package class”);
• }
• }
• import myproject.mypack;
• Public class MyClass
• {
• Public static void main(String[] args)
• {
• mypack obj= new mypack();
• Obj.display();
• }
Packages and Member Access
• As it relates to the interplay between classes and packages, Java
addresses four categories of visibility for class members:
• Subclasses in the same package
• Non-subclasses in the same package
• Subclasses in different packages
• Classes that are neither in the same package nor subclasses
The three access modifiers, private,
public, and protected, provide a
variety of ways
Importing Packages
• This is the general form of the import statement:
• import pkg1 [.pkg2].(classname | *);
• pkg1 is the name of a top-level package, and pkg2 is the name of a
subordinate package inside the outer package separated by a dot (.).
• There is no practical limit on the depth of a package hierarchy, except
that imposed by the file system.
• package mypack;
• /* Now, the Balance class, its constructor, and its show() method are
public. This means that they can be used by non-subclass code outside their
package. */
• public class Balance {
• String name; double bal;
• public Balance(String n, double b)
• { name = n; bal = b; }
• public void show() {
• if(bal<0){
• System.out.print("--> ");
• System.out.println(name + ": $" + bal);
• }}
TestBalance imports mypack and is then
able to make use of the Balance class
• import mypack.*;
• class TestBalance {
• public static void main(String[] args) {
• /* Because Balance is public, you may use Balance class and call its
constructor. */
• Balance test = new Balance(“AMCEC", 99);
• test.show(); // you may also call show()
•}
•}
Interfaces
• Using the keyword interface, you can fully abstract a class’ interface
from its implementation.
• To implement an interface, a class must provide the complete set of
methods required by the interface.
• interface keyword, Java allows you to fully utilize the “one interface,
multiple methods” aspect of polymorphism.
• Defining an Interface An interface is defined much like a class. This is a
simplified general form of an interface: access interface name
{ return-type method-name1(parameter-list); return-type method-
name2(parameter-list); type final-varname1 = value; type final-
varname2 = value; //... return-type method-nameN(parameter-list);
type final-varnameN = value; }
Defining an Interface
• An interface is defined much like a class.
• This is a simplified general form of an interface:
• access interface name {
• return-type method-name1(parameter-list);
• return-type method-name2(parameter-list);
• type final-varname1 = value;
• type final-varname2 = value;
• //...
• return-type method-nameN(parameter-list);
• type final-varnameN = value;
• }
Implementing Interfaces
• The general form of a class that includes the implements clause looks
like this:
• class classname [extends superclass] [implements interface
[,interface...]]
• { // class-body }
• // Interface
• interface Animal {
• public void animalSound(); // interface method (does not have a body)
• public void sleep(); // interface method (does not have a body)
• }
• // cat "implements" the Animal interface
• class cat implements Animal {
• public void animalSound() {
• // The body of animalSound() is provided here
• System.out.println("The cat says: meew meew");
• }
• public void sleep() {
• // The body of sleep() is provided here
• System.out.println("Zzz");}}
• class Main {
• public static void main(String[] args) {
• cat c = new cat(); // Create a cat object
• c.animalSound();
• c.sleep();
• }
•}
• Output:
• The cat says: meew meew
Zzz
Develop a JAVA program to create an interface Resizable with methods resizeWidth(int width) and resizeHeight(int height) that allow an object to be resized.
7.
Create a class Rectangle that implements the Resizable interface and implements the resize methods.
• interface Resizable
•{
• void resizeWidth(int width);
• void resizeHeight(int height);
•}
• class Rectangle implements Resizable
• {
• private int width;
• private int height;
• public Rectangle(int width, int height)
• {
• this.width = width;
• this.height = height;
• }
• public void resizeWidth(int newWidth)
• {
• this.width = newWidth;
• }
• public void resizeHeight(int newHeight)
• {
• this.height = newHeight;
• }
• public void display()
• {
• System.out.println("Rectangle width: " + width + ", height: " + height);
• }
• public static void main(String[] args)
• {
• Rectangle rectangle = new Rectangle(5, 10);
• rectangle.display();
• rectangle.resizeWidth(8);
• rectangle.resizeHeight(15);
• rectangle.display();
• }
• }
• Output:
• Rectangle width: 5, height: 10
• Rectangle width: 8, height: 15