Unit3
packages and interfaces
Chapter 1
contents
• Packages
• Packages and member access
• Understanding protected members
• Importing packages
• Java class library is continued in packages
• Interfaces
• Implementing interfaces
• Using interface references
• Variables in interfaces
• Extended interfaces
introduction
• Packages are group of related classes
• Packages help in organizing your code and provide
another layer of encapsulation.
• An interface defines a set of methods that will be
implemented by a class.
• An interface itself does not implement any method.
• Packages and interfaces give greater control over the
organization of your program.
packages
• Package serves two purposes:
– It provides a mechanism by which related pieces of a
program can be organized as a unit.
• Classes defined within a package must be accessed through a
package name
– A package participates in java access control mechanism
• Classes defined within a package can be made private to that
package and do not accessible by code outside the package.
• In general, when you name a class, you are allocating
a name from the namespace.
• A namespace defines a declarative region,
• In java, no two class can use the same name from the
same namespace, hence each name of a class in a
namespace must be unique.
• when there are many programs, default namespace
becomes crowded,
• In large programs, finding unique names for each
class can be difficult
• Further you must avoid name collisions with the
code created by other programmers working on the
same project and with Java’s library
• The solution for such a situation is to create classes
in a package
• When classes are declared within package , the name
of the package is attached to each of its class, thus
avoiding name collisions with other classes that have
same name, but are in other packages
Defining a package
• When no package statement is specified in the
program, the default or global package is used.
• The default package has no name, it is transparent
• Its for short sample programs, inadequate for real
applications.
• Most of the time , in a application there will be one
or more packages.
• To create a package, put a command package at the
top of Java source file.
• The classes declared within that file will then belong
to specified package, since a package defines a
namespace, the names of the classes that you put
into the file becomes a part of that package’s
namespace
• General format is
package mypack;
• Here mypack is the name of the package
• Like rest of Java, package names are case senstive,
lowercase is often used for package names.
• More than one file can include the same package
statement.
• The package statement simply specifies to which the
class defined in the file belong.
• You can create hierarchy of packages. Simply
separate each package from the name the one above
it by use of a period, general form of a multilevel
package statement is shown here:
package pack1.pack2.pack3…packN;
• You must create directories that support the package
hierarchy that you create.
• package alpha.beta.gamma;
• Must be stored in …/ alpha/beta/gamma
Finding packages and CLASSPATH
• Packages are mirrored by directories.
• How does the java run time system know where to
look for packages that you create?
• The answer is
• Java run time system uses the current working
directory as its starting point.
• You can specify a directory path or paths by seeing
the CLASSPATH environmental variable
• You can use –classpath option with java and javac to
specify the path to your class.
Packages and member access
• If a member of class has no explicit access modifier , then
it is visible within its package but not out of the package
• Hence when declared as default, it becomes private in
nature w.r.to outside of the package.
• Members declared explicitly as public are visible
everywhere, including different classes and different
packages.
• A private member is unaffected by its membership in a
package.
• A member specified as protected is accessible within its
package and all subclasses including subclass in other
packages.
Understanding protected members
• The protected modifier creates a member that is
accessible within its package and to subclasses in
other packages
• It protects arbitrary access from outside the package.
• With a small addition of code in protectDemo class,
it will throw an error because it is not an inherited
class of book.
• It cannot access the variables because it is specified
with protected access modifiers
Importing packages
• When you use class from other packages, you can fully
qualify the name of the class with the name of its
package.
• Using import statement, one can bring one or more
members of a package into view.
• This allows you to use those members directly, without
explicit package qualification.
• General format of the import statement:
import pkg.classname;
Here pkg is the name of the package, which can include its full
path, and classname is the name of the class being imported.
• If you want to import the entire contents of a
package , use asterisk(*) for the class name.
• import mypack.Myclass;
– Myclass is imported from mypack package
• import mypack.*;
– All of the classes in mypack are imported.
• In Java source file , import statements occur
immediately following the package statement (if it
exists) and before any class defination.
Java’s standard packages
• Java has a huge number of standard classes that are
available to all programs.
• The class library is referred to as JavaAPI (Application
Programming Interface)
• JavaAPI is stored in packages
• At the top of the package hierarchy is java
• Descending from java are several subpackages
Sub-package description
java.lang Contains a large number of general
purposes class
java.io Contains the i/o classes
java.net Contains those classes that support
networking
java.applet Contains classes for creating applets.
java.awt Contains classes that support Abstract
Window ToolKit
interfaces
• An abstract method defines the signature for a
method but provides no implementation.
• A subclass must provide its own implementation of
each abstract method defined by its superclass
• Thus an abstract method provides an interface to the
method and not an implementation.
• Interface can be created using a keyword “interface”
• In an interface, no method can include a body.
• It specifies what must be done and not how.
• Once an interface is defined any number of classes
can implement it.
• One class can implement any number of interfaces.
• To implement an interface, a class must provide
bodies for the methods described by the interface.
• Two classes might implement the same interface in
different ways, but each class still supports the same
of the methods.
• By providing the interface keyword, Java allows you
to fully utilize the “one interface, multiple methods”
• General form of the interface is as follows:
access interface name{
ret_type method_name1(param_list);
ret_type method_name2(param_list);
…
ret_type method_nameN(param_list);
type var1=value;
type var2=value;
…
type varN=value;
}
• Here the access is public or not used.
• When no access modifier is included, then the
default access results, and the interface is available
only to other members of its package.
• When it is declared as public, the interface can be
used by any other code.
• name is the name of the interface and can be any
valid identifier.
• Method are declared using only their return type and
signature.
• Variables declared in an interface are not instance
variable
• They are implicitly final, public and static and hence
must be declared
public interface series{
int getNext(); //return a number next in the series
void reset(); //restart
void SetStart(int X); //set staring value
}
Implementing interfaces
• Once an interface has been declared, one or more classes
can implement that interface.
• To implement an interface , include the implement clause
in a class definition and then create the methods defined
by the interface.
• The general form of a class that includes the implements
clause looks like:
class classname extends superclass implements interface{
//class_body
}
To implement more than one interface, the interface are
separated with a comma.
• The methods that implement an interface must be
declared public.
• The type signature of the implementing method
must exactly the type signature specified in the
interface definition.
• Example
Using interface references
• A variable that can refer to any object that
implements its interface.
• When you call a method on an object through an
interface reference, it is the version of the method
implemented by the object that is executed.
• This process is similar to using a superclass reference
to access a subclass object.
Variables in interfaces
• Variables can be implemented in an interface but will
be implicitly public, static and final in nature.
• Large programs make use of several constant values
that describe such as array size, various limits, special
values .
• Large programs will be collection of several separate
source files , there needs to be a convenient way to
make these constants available to each file.
• Interface offers that solution.
• To define a set of shared constants, create an
interface that contains only these constants and with
out any methods.
• Each file needs access to the constants simply
“implements” the interface
• This brings constants into view.
Variables in interfaces-example
interface Iconst{
int MIN=0;
int MAX=100;
String ERROR_MSG= “Boundary error”;
}
class IConstD implements Iconst{
public static void main(String args[]){
int nums[]=new int [MAX];
for(int i=MIN;i<11;i++){
if(i>=MAX) System.out.println(ERROR_MSG);
else
System.out.println(nums[i]+ “ “);
}
}
}
Extending interfaces
• One interface can inherit another by use of the
keyword extends.
• The syntax is same as for inheriting classes.
• When a class implements an interface that inherits
another interface, it must provide implementations
for all methods defined within the interface
inheritance chain.
Chap1 packages
Chap1 packages

Chap1 packages

  • 1.
  • 2.
    contents • Packages • Packagesand member access • Understanding protected members • Importing packages • Java class library is continued in packages • Interfaces • Implementing interfaces • Using interface references • Variables in interfaces • Extended interfaces
  • 3.
    introduction • Packages aregroup of related classes • Packages help in organizing your code and provide another layer of encapsulation. • An interface defines a set of methods that will be implemented by a class. • An interface itself does not implement any method. • Packages and interfaces give greater control over the organization of your program.
  • 4.
    packages • Package servestwo purposes: – It provides a mechanism by which related pieces of a program can be organized as a unit. • Classes defined within a package must be accessed through a package name – A package participates in java access control mechanism • Classes defined within a package can be made private to that package and do not accessible by code outside the package.
  • 5.
    • In general,when you name a class, you are allocating a name from the namespace. • A namespace defines a declarative region, • In java, no two class can use the same name from the same namespace, hence each name of a class in a namespace must be unique. • when there are many programs, default namespace becomes crowded, • In large programs, finding unique names for each class can be difficult
  • 6.
    • Further youmust avoid name collisions with the code created by other programmers working on the same project and with Java’s library • The solution for such a situation is to create classes in a package • When classes are declared within package , the name of the package is attached to each of its class, thus avoiding name collisions with other classes that have same name, but are in other packages
  • 7.
    Defining a package •When no package statement is specified in the program, the default or global package is used. • The default package has no name, it is transparent • Its for short sample programs, inadequate for real applications. • Most of the time , in a application there will be one or more packages. • To create a package, put a command package at the top of Java source file.
  • 8.
    • The classesdeclared within that file will then belong to specified package, since a package defines a namespace, the names of the classes that you put into the file becomes a part of that package’s namespace • General format is package mypack; • Here mypack is the name of the package • Like rest of Java, package names are case senstive, lowercase is often used for package names.
  • 9.
    • More thanone file can include the same package statement. • The package statement simply specifies to which the class defined in the file belong. • You can create hierarchy of packages. Simply separate each package from the name the one above it by use of a period, general form of a multilevel package statement is shown here: package pack1.pack2.pack3…packN;
  • 10.
    • You mustcreate directories that support the package hierarchy that you create. • package alpha.beta.gamma; • Must be stored in …/ alpha/beta/gamma
  • 11.
    Finding packages andCLASSPATH • Packages are mirrored by directories. • How does the java run time system know where to look for packages that you create? • The answer is • Java run time system uses the current working directory as its starting point. • You can specify a directory path or paths by seeing the CLASSPATH environmental variable • You can use –classpath option with java and javac to specify the path to your class.
  • 14.
    Packages and memberaccess • If a member of class has no explicit access modifier , then it is visible within its package but not out of the package • Hence when declared as default, it becomes private in nature w.r.to outside of the package. • Members declared explicitly as public are visible everywhere, including different classes and different packages. • A private member is unaffected by its membership in a package. • A member specified as protected is accessible within its package and all subclasses including subclass in other packages.
  • 16.
    Understanding protected members •The protected modifier creates a member that is accessible within its package and to subclasses in other packages • It protects arbitrary access from outside the package.
  • 21.
    • With asmall addition of code in protectDemo class, it will throw an error because it is not an inherited class of book. • It cannot access the variables because it is specified with protected access modifiers
  • 24.
    Importing packages • Whenyou use class from other packages, you can fully qualify the name of the class with the name of its package. • Using import statement, one can bring one or more members of a package into view. • This allows you to use those members directly, without explicit package qualification. • General format of the import statement: import pkg.classname; Here pkg is the name of the package, which can include its full path, and classname is the name of the class being imported.
  • 25.
    • If youwant to import the entire contents of a package , use asterisk(*) for the class name. • import mypack.Myclass; – Myclass is imported from mypack package • import mypack.*; – All of the classes in mypack are imported. • In Java source file , import statements occur immediately following the package statement (if it exists) and before any class defination.
  • 26.
    Java’s standard packages •Java has a huge number of standard classes that are available to all programs. • The class library is referred to as JavaAPI (Application Programming Interface) • JavaAPI is stored in packages • At the top of the package hierarchy is java • Descending from java are several subpackages
  • 27.
    Sub-package description java.lang Containsa large number of general purposes class java.io Contains the i/o classes java.net Contains those classes that support networking java.applet Contains classes for creating applets. java.awt Contains classes that support Abstract Window ToolKit
  • 28.
    interfaces • An abstractmethod defines the signature for a method but provides no implementation. • A subclass must provide its own implementation of each abstract method defined by its superclass • Thus an abstract method provides an interface to the method and not an implementation. • Interface can be created using a keyword “interface” • In an interface, no method can include a body. • It specifies what must be done and not how.
  • 29.
    • Once aninterface is defined any number of classes can implement it. • One class can implement any number of interfaces. • To implement an interface, a class must provide bodies for the methods described by the interface. • Two classes might implement the same interface in different ways, but each class still supports the same of the methods. • By providing the interface keyword, Java allows you to fully utilize the “one interface, multiple methods”
  • 30.
    • General formof the interface is as follows: access interface name{ ret_type method_name1(param_list); ret_type method_name2(param_list); … ret_type method_nameN(param_list); type var1=value; type var2=value; … type varN=value; }
  • 31.
    • Here theaccess is public or not used. • When no access modifier is included, then the default access results, and the interface is available only to other members of its package. • When it is declared as public, the interface can be used by any other code. • name is the name of the interface and can be any valid identifier. • Method are declared using only their return type and signature.
  • 32.
    • Variables declaredin an interface are not instance variable • They are implicitly final, public and static and hence must be declared public interface series{ int getNext(); //return a number next in the series void reset(); //restart void SetStart(int X); //set staring value }
  • 33.
    Implementing interfaces • Oncean interface has been declared, one or more classes can implement that interface. • To implement an interface , include the implement clause in a class definition and then create the methods defined by the interface. • The general form of a class that includes the implements clause looks like: class classname extends superclass implements interface{ //class_body } To implement more than one interface, the interface are separated with a comma.
  • 34.
    • The methodsthat implement an interface must be declared public. • The type signature of the implementing method must exactly the type signature specified in the interface definition. • Example
  • 37.
    Using interface references •A variable that can refer to any object that implements its interface. • When you call a method on an object through an interface reference, it is the version of the method implemented by the object that is executed. • This process is similar to using a superclass reference to access a subclass object.
  • 39.
    Variables in interfaces •Variables can be implemented in an interface but will be implicitly public, static and final in nature. • Large programs make use of several constant values that describe such as array size, various limits, special values . • Large programs will be collection of several separate source files , there needs to be a convenient way to make these constants available to each file. • Interface offers that solution.
  • 40.
    • To definea set of shared constants, create an interface that contains only these constants and with out any methods. • Each file needs access to the constants simply “implements” the interface • This brings constants into view.
  • 41.
    Variables in interfaces-example interfaceIconst{ int MIN=0; int MAX=100; String ERROR_MSG= “Boundary error”; } class IConstD implements Iconst{ public static void main(String args[]){ int nums[]=new int [MAX]; for(int i=MIN;i<11;i++){ if(i>=MAX) System.out.println(ERROR_MSG); else System.out.println(nums[i]+ “ “); } } }
  • 42.
    Extending interfaces • Oneinterface can inherit another by use of the keyword extends. • The syntax is same as for inheriting classes. • When a class implements an interface that inherits another interface, it must provide implementations for all methods defined within the interface inheritance chain.