Chap1 packages

44
Unit3 packages and interfaces Chapter 1

Transcript of Chap1 packages

Page 1: Chap1 packages

Unit3packages and interfaces

Chapter 1

Page 2: Chap1 packages

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

Page 3: Chap1 packages

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.

Page 4: Chap1 packages

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.

Page 5: Chap1 packages

• 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

Page 6: Chap1 packages

• 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

Page 7: Chap1 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.

Page 8: Chap1 packages

• 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 ispackage 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.

Page 9: Chap1 packages

• 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;

Page 10: Chap1 packages

• You must create directories that support the package hierarchy that you create.

• package alpha.beta.gamma;• Must be stored in …/ alpha/beta/gamma

Page 11: Chap1 packages

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.

Page 12: Chap1 packages
Page 13: Chap1 packages
Page 14: Chap1 packages

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.

Page 15: Chap1 packages
Page 16: Chap1 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.

Page 17: Chap1 packages
Page 18: Chap1 packages
Page 19: Chap1 packages
Page 20: Chap1 packages
Page 21: Chap1 packages

• 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

Page 22: Chap1 packages
Page 23: Chap1 packages
Page 24: Chap1 packages

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.

Page 25: Chap1 packages

• 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.

Page 26: Chap1 packages

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

Page 27: Chap1 packages

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

Page 28: Chap1 packages

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.

Page 29: Chap1 packages

• 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”

Page 30: Chap1 packages

• 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;}

Page 31: Chap1 packages

• 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.

Page 32: Chap1 packages

• 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}

Page 33: Chap1 packages

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.

Page 34: Chap1 packages

• 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

Page 35: Chap1 packages
Page 36: Chap1 packages
Page 37: Chap1 packages

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.

Page 38: Chap1 packages
Page 39: Chap1 packages

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.

Page 40: Chap1 packages

• 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.

Page 41: Chap1 packages

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);elseSystem.out.println(nums[i]+ “ “);

} }}

Page 42: Chap1 packages

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.

Page 43: Chap1 packages
Page 44: Chap1 packages