5 Packages and Interfaces








Package
Reusability of code is one of the most important requirements in the software industry.  As it saves time, effort and also ensures consistency.  For example a class once defined and then used repeatedly will ensure that all applications using that particular class will have identical behavior.  In Java, to reuse the already existing code, we make use of “Packages”.

Thus all reusable code is put into packages.  A package is a collection of classes, interfaces and other packages.  Thus a package can be defined as a means of organizing classes together as groups.  Packages are useful for following purposes:-

Ø  It allows to organize classes into smaller units and make it easy to locate and use the appropriate class file.
Ø  It helps to avoid naming conflicts.  A package basically hides the classes and avoids conflicts in names.
Ø  Packages allow to protect classes, data and methods in a larger way than on a class-to-class basis.
Ø  Packages names can be used to identify classes.

Thus packages need not only a collection of classes and interfaces.  They can also contain other packages, each level representing a smaller, more specific grouping of classes.

Creating Packages
Package can be created with package keyword followed with the package name.  Before creating a package one thing should be noted is that the package should be stored in the directory with same name of the package.  Thus in the following example we are creating a package with the name MyPackage, this should be stored in the Directory MyPackage.

Example 1(Creating a Package)
package MyPackage;
public class Calculate
{
public double volume(double height,double width,double depth)
{
return(height * width * depth);
}
public int add(int x,int y)
{
return( x + y);
}
public int divide(int x,int y){
return( x / y);
}
}

Example 2(Using a Package)
import java.io.*;
import MyPackage.Calculate;
class PkDemo
{
public static void main(String args[])
{
Calculate calc = new Calculate( );
int sum=calc.add(10,20);
double vol =calc.volume(10.3f,12.2f,32.32f);
int div=calc.divide(20,4);
System.out.println("Addition is "+ sum);
System.out.println("Volume is "+vol);
System.out.println("Division is "+ div);
}
}

Interfaces
Inheritance plays a very important role in saving the time and energy of programmers by reusing the existing code.  Most of the real world programs make use of something called multiple heritance.  In multiple inheritances, we can inherit methods and properties from several different classes.  Java does not support multiple inheritance.  However seeing the importance of multiple inheritance, java introduces the concept of interface.

An interface is a template of behavior (in form of methods) that other classes need to implement.  This means that the body of the method is empty.  Thus an interface is a kind of class containing methods and variables.  The difference lies in the way of defining the abstract methods by the concept of interface.  This means that interfaces do not specify any code to implement these methods and data fields contain only constants.  Classes have the ability to define objects, while interfaces define a set of methods and constants to be implemented by another object.  Interface enables us to define set of functionality without having any idea as to how this property of the function will be defined later.  For example:

Class Demo extends Mycalc

Where Mycalc is an interface.  A class can implement as many interfaces as it needs, in addition to extending a class separating them with a comma.  For example:

Class Point extends Applet implements Runnable, ActionListener
Interfaces can not extend classes but can extend other classes.  If we implement an interface that extends an interface, we have to override methods in the new interface as well as in the old interface.  All methods in the interfaces have to be of type public.  It is illegal to use any other standard modifiers, such as protected, private etc., when declaring the methods in an interface.

Modifiers
Modifiers are the keywords that give additional meaning to the code and classes.  There are two types of categories of modifiers.
à    Access modifiers.
à    Non access modifiers

The access modifiers are:

  • public
This makes the class features publicly available to any class,

  • protected
This allows access to the class itself, subclasses and all the classes present in the different packages.  Thus protected access modifiers are used when we want to restrict the access to certain features of a class.

  • Private
This modifiers makes the class’s features available only in that class.


Practical Examples

Example 1(Package Creation)
package Asia;
public class India
 {
int states;
boolean democratic;
String capital;
public India( )
{
states =26;
democratic=true;
capital= new String("New Delhi");
}
  public String getCapital( )
   {
            return capital;
               }                  
}

Example 2(Pacakage Utilisation)
import Asia.India;
class America
 {
India Ind;
public America( )
{
Ind= new India( );
System.out.println("Capital of India is "+ Ind.getCapital( ));
}
public static void main(String args[])
{
new America( );
               }                  
}

Example 3(Interface)
interface Area
{
final static float pi = 3.14f;
float compute(float x,float y);
}
class Rectangle implements Area
{
public float compute(float x,float y)
{
return(x*x);
}
}
class Circle implements Area
{
public float compute(float x,float y)

{
return (pi*x*x);
}
}
class FirstInterface
{
public static void main(String args[])
{
Rectangle rect= new Rectangle( );
Circle cir=new Circle( );
Area area;
area = rect;
System.out.println("Area of Rectangle "+area.compute(10,20));
area = cir;
System.out.println("Area of Circle "+area.compute(10,0));
}
}

Example 4(Interface)
interface Calc
{
final static int x = 40;
int Add (int  x,int y);
}

class Addition implements Calc
{
public int Add(int x,int y)
{
y=100;
return(x+y);
}
}
class Product implements Calc
{
public int Add(int x,int y)
{
y=90;
return (x*y);
}
}

class SecInterface
{
public static void main(String args[])
{
Addition a1= new Addition( );
Product b1=new Product( );
Calc Cal;
Cal = a1;
 System.out.println("Addition  "+Cal.Add(100,20));
Cal =b1;
System.out.println("Product "+Cal.Add(140,50));
}
}

Example 5(Abstract Modifier)
abstract class Employee
{
int basic=2000;
abstract void salary( );
}
class Manager extends Employee
{
void salary( )
{
System.out.println("Salary  " + basic*5);
}
}
class Worker extends Employee{
void salary( ){
System.out.println("Salary  " + basic*2);
}
}
class Abstract {
public static void main(String args[]) {
Manager m= new Manager( );
System.out.println(" " );
System.out.println("Manager" );
System.out.println("***********" );
m.salary( );
System.out.println(" " );
Worker w = new Worker( );
System.out.println("Worker" );
System.out.println("***********" );
w.salary( );
}
}

No comments:

Post a Comment