packages&interfaces
TRANSCRIPT
-
7/27/2019 Packages&Interfaces
1/24
PACKAGES
-
7/27/2019 Packages&Interfaces
2/24
Packages
Packagesare containers for classes that areused to keep the class name spacecompartmentalized.
The package is both a naming and a visibilitycontrol mechanism.
You can define classes inside a package that arenot accessible by code outside that package.
You can also define class members that are onlyexposed to other members of the same package.
2
-
7/27/2019 Packages&Interfaces
3/24
Defining a Package
To create a package simply include a packagecommand asthe first statement in a Java source file.
Any classes declared within that file will belong to thespecified package.
If you omit the package statement, the class names are put
into the default package, which has no name.
This is the general form of the package statement:
package pkg;
Eg:package MyPackage;
Note: The .class files for any classes you declare to be part of MyPackagemust be stored in a directory called MyPackage.
The general form of a multileveled package statement isshown here:packagepkg1.pkg2.pkg3;
3
-
7/27/2019 Packages&Interfaces
4/24
// A short package demonstration.package BookPack;
class Book {private String title;private String author;private int pubDate;Book(String t, String a, int d) {
title = t;author = a;pubDate = d;
}void show() {
System.out.println(title);System.out.println(author);System.out.println(pubDate);System.out.println();
}}
4
-
7/27/2019 Packages&Interfaces
5/24
class BookDemo {
public static void main(String args[]) {
Book books[] = new Book[5];
books[0] = new Book("Java: A Beginner'sGuide","Schildt", 2005);
books[1] = new Book("Java: The CompleteReference","Schildt", 2005);
books[2] = new Book("The Art of Java",
"Schildt and Holmes", 2003);
books[3] = new Book("Red Storm Rising",
"Clancy", 1986);
books[4] = new Book("On the Road",
"Kerouac", 1955);for(int i=0; i < books.length; i++)
books[i].show();
}
}
5
-
7/27/2019 Packages&Interfaces
6/24
Class Member Access
Private No
ModifierProtected Public
Same class Yes Yes Yes Yes
Same packagesubclass No Yes Yes Yes
Same package
non-subclass
No Yes Yes Yes
Different package
subclass
No No Yes Yes
Different package
non-subclass
No No No Yes
Note: A class has only two possible access levels default and public.
6
-
7/27/2019 Packages&Interfaces
7/24
Example// This is file Protection.java in package p1
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("base constructor");
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);
}
}
7
-
7/27/2019 Packages&Interfaces
8/24
//This is file Derived.java in package p1
package p1;
class Derived extends Protection {
Derived() {System.out.println("derived constructor");
System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
8
-
7/27/2019 Packages&Interfaces
9/24
// This is file SamePackage.java in package p1
package p1;
class SamePackage {
SamePackage() {
Protection p = new Protection();System.out.println("same package constructor");
System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
9
-
7/27/2019 Packages&Interfaces
10/24
//This is file Protection2.java in package p2
package p2;
class Protection2 extends p1.Protection {
Protection2() {
System.out.println("derived other package
constructor");// class or package only
// System.out.println("n = " + n);
// class only
// System.out.println("n_pri = " + n_pri);System.out.println("n_pro = " + n_pro);
System.out.println("n_pub = " + n_pub);
}
}
10
-
7/27/2019 Packages&Interfaces
11/24
// This is file OtherPackage.java in package p2
package p2;class OtherPackage {
OtherPackage() {
p1.Protection p = new p1.Protection();
System.out.println("other package constructor");
// class or package only
// System.out.println("n = " + p.n);
// class only
// System.out.println("n_pri = " + p.n_pri);
// class, subclass or package only// System.out.println("n_pro = " + p.n_pro);
System.out.println("n_pub = " + p.n_pub);
}
}
11
-
7/27/2019 Packages&Interfaces
12/24
// The test file for p1package p1;
// Instantiate the various classes in p1.
public class Demo {public static void main(String args[]) {
Protection ob1 = new Protection();
Derived ob2 = new Derived();
SamePackage ob3 = new SamePackage();
}
}
12
-
7/27/2019 Packages&Interfaces
13/24
// The test file for p2
package p2;
// Instantiate the various classes in p2.public class Demo {
public static void main(String args[]) {
Protection2 ob1 = new Protection2();
OtherPackage ob2 = new OtherPackage();}
}
13
-
7/27/2019 Packages&Interfaces
14/24
ImportingPackages
All of the standard classes are stored in some namedpackage.
Java includes the import statement to bring certainclasses, or entire packages, into visibility.
In a Java source file, import statements occur immediatelyfollowing the package statement (if it exists) and beforeany class definitions.
importpkg1.pkg2.classname|*;
14
-
7/27/2019 Packages&Interfaces
15/24
// Make the instance variables in Book protected.
package bookpack;
public class Book {
private String title;
private String author;
private int pubDate;
public Book(String t, String a, int d) {title = t;
author = a;
pubDate = d;
}
public void show() {
System.out.println(title);
System.out.println(author);
System.out.println(pubDate);
}}
15
-
7/27/2019 Packages&Interfaces
16/24
// Demonstrate import.
package bookpackB;
import bookpack.*;
class UseBook {public static void main(String args[]) {
// Use the Book Class from bookpack package
Book books[] = new Book[5];
books[0] = new Book("Java: ABeginner's Guide","Schildt", 2005);
books[1] = new Book("Java: TheComplete Reference", "Schildt", 2005);
16
17
-
7/27/2019 Packages&Interfaces
17/24
books[2] = new Book("The Art ofJava","Schildt and Holmes", 2003);
books[3] = new Book("Red Storm Rising","Clancy", 1986);
books[4] = new Book("On the Road","Kerouac", 1955);
for(int i=0; i < books.length; i++){
books[i].show();
}
}
}
17
-
7/27/2019 Packages&Interfaces
18/24
INTERFACES
19
-
7/27/2019 Packages&Interfaces
19/24
Interfaces
Defines what a class must do but not
how it will do it.
Using the keyword interface, you canfully abstract a class interface from itsimplementation.
Once it is defined, any number ofclasses can implement an interface.
Also, one class can implement anynumber of interfaces.
19
20
-
7/27/2019 Packages&Interfaces
20/24
Defining an Interfaceaccess interface name {return-type method-name1(parameter-list);return-type method-name2(parameter-list);// ...return-type method-nameN(parameter-list);type final-varname1 = value;
type final-varname2 = value;// ...type final-varnameN = value;
}
Note: Here access is eitherpublic ordefault (not used)public interface Series {
int getNext(); // return next number in series
void reset(); // restart
void setStart(int x); // set starting value}
20
21
-
7/27/2019 Packages&Interfaces
21/24
Implementing Interfaces
To implement an interface, include the implements clausein a class definition, and then create the methods definedby the interface.
access class classname [extends superclass]
[implementsinterface [,interface...]] {// class-body
}
Example:// Implement Series.
class ByTwos implements Series {
int start;int val;
ByTwos() {
start = 0;
val = 0;}
21
22
-
7/27/2019 Packages&Interfaces
22/24
public int getNext() {
val += 2;
return val;
}
public void reset() {
start = 0;
val = 0;
}public void setStart(int x) {
start = x;
val = x;
}}
class SeriesDemo {
public static void main(String args[]) {
ByTwos ob = new ByTwos();
22
23
-
7/27/2019 Packages&Interfaces
23/24
for(int i=0; i < 5; i++)
System.out.println("Next value is" +ob.getNext());
System.out.println("\nResetting");
ob.reset();
for(int i=0; i < 5; i++)
System.out.println("Next value is" +ob.getNext());
System.out.println("\nStarting at 100");
ob.setStart(100);
for(int i=0; i < 5; i++)
System.out.println("Next value is " +
ob.getNext());
}
}
23
24
-
7/27/2019 Packages&Interfaces
24/24
Notes
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.
Variables in Interfaces are implicitly public,static, andfinal.
i.e Constant values that describe such things as array
size, various limits, special values, and the like.
Interfaces can be extended.
i.e One interface can inherit another by use of thekeyword extends.
24