PackagesAndInterfaces
PackagesAndInterfaces
▪ Any classes declared within that file will belong to the specified package.
▪ If package statement is not included, then class names are put into the default
package, which has no name.
General form :
package pkg;
General form
package pkg1[.pkg2[.pkg3]];
• First, by default, the Java run-time system uses the current working directory as its
starting point.
• We can use –classpath option with java and javac to specify the path to our class.
Finding packages and CLASSPATH
Example
package MyPack;
In order for a program to find MyPack, one of the 3 things must be true.
• The –classpath option must specify the path to MyPack when the program is run via
java.
When second two options are used, the classpath must not include MyPack itself.
It must specify the path to MyPack.
Name the above file as prg.java and put it in a directory called MyPack.
Next compile the file, make sure that resulting class file is also in MyPack.
▪ The first class defines four int variables in each of the legal protection
modes.
▪ The third class, SamePackage, is not a subclass of Protection, but is in the same
package and also has access to all but n_pri.
package p1
package p2
class protection
class derived2 extends p1.protection
{
{
}
class derived extends protection
}
{
class otherpackage
{
}
class samepackage
{
}
}
Protection.java
package p1;
public class Protection
{
int n = 1;
private int n_pri = 2;
protected int n_pro = 3;
public int n_pub = 4;
public Protection()
{
System.out.println("n = " + n);
System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
Derived.java
package p1;
class Derived extends Protection
{
Derived()
{
System.out.println("n = " + n);
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
SamePackage.java
package p1;
class SamePackage
{
SamePackage()
{
Protection p = new Protection();
System.out.println("n = " + p.n);
// System.out.println("n_pri = " + p.n_pri);
System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
Protection2.java
package p2;
class derived2 extends p1.Protection
{
Protection2()
{
// System.out.println("n = " + n);
// System.out.println("n_pri = " + n_pri);
System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
OtherPackage.java
package p2;
class OtherPackage
{
OtherPackage()
{
p1.Protection p = new p1.Protection();
Once imported, a class can be referred to directly, using only its name.
import pkg1[.pkg2].(classname|*);
import java.util.Date;
import java.io.*;
import java.awt.*;
import statements occur immediately following the package statement (if it exists) and before
any class definitions.
All of the standard Java classes included with Java are stored in a package called java.
The basic language functions are stored in a package inside of the java package called
java.lang.
We have to import every package or class that we want to use, but since Java is useless
without much of the functionality in java.lang, it is implicitly imported by the compiler for all
programs.
This is equivalent to the following line being at the top of all of our programs:
import java.lang.*;
import statement is optional.
import java.util.*;
class MyDate extends Date
{
The same example without the import statement looks like this:
}
import org.cloudbus.cloudsim.Datacenter;
import org.cloudbus.cloudsim.DatacenterBroker;
import org.cloudbus.cloudsim.Host;
import org.cloudbus.cloudsim.Storage;
import org.cloudbus.cloudsim.Vm;
class prg
{
public static void main(String args[])
{
Host h = new Host();
Vm v = new Vm();
}
}
Interface
• An interface is basically a kind of class.
Example:
interface item
{
static final int code=1000;
void display();
}
Example
interface Callback
{
void callback(int param);
}
Implementing Interfaces
c can be used to access the callback() method, it can not access any other
members of the Client class.
// Another implementation of Callback.
If a class includes an interface but does not fully implement the methods defined by
that interface, then that class must be declared as abstract.
Ex: class A
{
public interface prg
{
voud disp();
}
}
Nested interface
▪ Interfaces (or classes) can have only public and default access specifiers when
declared outside any other class.
▪ This interface declared in a class can either be default, public, protected not
private.
▪ Here c_name is the name of the class in which it is nested and i_name is the
name of the interface itself
Nested interface
class Test
{
interface Yes
{ void show();
}
}
class A
{
public static void main(String[] args)
{
Test.Yes obj;
Testing t = new Testing();
obj=t;
obj.show();
}
}
Applying Interfaces
interface SharedConstants
{ int NO = 0;
int YES = 1;
int MAYBE = 2;
int LATER = 3;
}
Class A implements SharedConstants
{ int x;
A() { x=1; }
void fun()
{ if (x > 1 ) return NO; else return YES; }
}
Interfaces Can Be Extended
interface A
{
void meth1();
}
interface B extends A
{
void meth2();
}
// This class must implement all of A and B
class MyClass implements B
{
public void meth1()
{ System.out.println("Implement meth1().");
}