Packages&Interfaces
Packages&Interfaces
Save, compile :
1) Save the file as MyPackageClass.java
2) Compile file javac MyPackageClass.java
3) Compile package as javac -d . MyPackageClass.java
This forces the compiler to create the "mypack" package.
The -d keyword specifies the destination for where to save the class file
L 1.9
Importing Packages
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
Using packagename.classname
•There are mainly three reasons to use interface. They are given
below.
•It is used to achieve abstraction.
•By interface, we can support the functionality of multiple
inheritance.
Example
interface printable public class IntefacePgm1
{ {
void print(); public static void main(String
} args[])
class Pgm1 implements printable {
{ Pgm1 obj = new Pgm1 ();
public void print() obj.print();
{ }
System.out.println("Hello"); }
}
}
Two classes implementing same interface
interface Drawable public class TestInterface1
{ {
void draw(); public static void main(String args[])
}
{
Drawable d=new Circle();
//Implementation: by second user
class Rectangle implements Drawable d.draw();
{ }
public void draw() }
{
System.out.println("drawing rectangle");
}
}