Java Package - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Java Package

Description:

Java's interface classes are classes without instance variables and their ... Java's interface allows you to fully utilize the 'one-interface, multiple ... – PowerPoint PPT presentation

Number of Views:2023
Avg rating:3.0/5.0
Slides: 25
Provided by: drewh6
Category:
Tags: java | javas | package

less

Transcript and Presenter's Notes

Title: Java Package


1
JavaPackage Interface
2
Package
  • Very often, programmers could run out of
    convenient, descriptive names for individual
    classes.
  • Package are containers for classes that are used
    to keep the class name space compartmentalized.

3
Package
  • You can define classes inside a package that are
    not accessible by code outside that package or
    class members that are only exposed to other
    members of the same package.
  • This allows the classes to have intimate
    knowledge of each other, but not expose that
    knowledge to the rest of the world.

4
Package
  • In general, a Java source file can contain any of
    the following four internal parts
  • (1) A single package statement (optional)
  • (2) Any number of import statements
  • (optional)
  • (3) A single public class declaration (required)
  • (4) Any number of classes private to the
  • package (optional)

5
Define a Package
  • Simply include a package command as the first
    statement in a Java source file.
  • Any classes declared within that file will belong
    to the specified package.
  • The package statement defines a name space in
    which classes are stored.
  • If the package statement is omitted, the class
    names are in the default package, which has no
    name.

6
Define a Package
  • Java uses file system directories to store
    packages.
  • Any classes declared to be in a particular
    package have to be saved in the a directory named
    after the package.
  • For example, classes that belong to the package
    MyPackage have to be in the directory called
    MyPackage.

7
Define a Package
  • You can create a hierarchy of packages. The
    general form is
  • package.pkg1.pkg2.pkg3
  • A package hierarchy must also be reflected in the
    file system of your computer. For example,
    java.awt.image needs to be stored in
    java.awt.image directory. (Package1AccountBalance1
    .java)

8
Access Protection
  • Classes and packages are both means of
    encapsulating and containing the name space and
    scope of variables and methods.
  • Packages act as containers for classes and other
    subordinate packages.
  • Classes act as containers for data and code.

9
Access Protection
  • Anything declared public can be accessed from
    anywhere.
  • Anything declared private cannot be seen outside
    of its class.
  • By default, when no access modifier is specified,
    a class member can be accessed by subclasses and
    other classes in the same package.

10
Access Protection
  • If you want a member to be seen outside the
    current package, but only to classes that
    subclass your class directly, declare that member
    protected.

11
Access Protection
  • Private No Modifier Protected
    Public
  • same class Y Y Y Y
  • same package
  • subclass N Y Y Y
  • same package
  • non-subclass N Y Y Y
  • Different Package
  • subclass N N Y Y
  • Different Package
  • non-subclass N N N Y
  • (Package1Protection1.java)

12
Importing Packages
  • Java provides import statement to bring certain
    classes, or entire packages, into visibility.
  • Once imported, a class can be referred to
    directly.
  • Import pkg1.pkg2.classname/
  • The may increase compilation time.

13
Interfaces
  • Normally, in order for a method to be called from
    one classes to another, both classes need to be
    present at compile time so Java can check to
    ensure that the method signatures are compatible.
  • In system like this, functionality gets pushed up
    higher and higher in the class hierarchy so that
    the mechanisms will be available for more and
    more subclasses.

14
Interfaces
  • Interfaces are designed to support dynamic method
    resolution at run time.
  • Interfaces disconnect the definition of a method
    or set of methods from the inheritance hierarchy
    by making it possible for classes that not
    related in terms of the class hierarchy to
    implement the same interface. The idea is similar
    to multiple inheritance in C.

15
Interfaces
  • Javas interface classes are classes without
    instance variables and their methods are declared
    without any body.
  • Javas interface allows you to fully utilize the
    one-interface, multiple methods aspect of
    polymorphism.
  • Interface specifies what a class must do, but not
    how it does it.

16
Defining an Interface
  • An interface is defined much like a class
  • accesmodifier interface interfacename
  • returntype methodname(paremeter-list)
  • returntype methodname(paremeter-list)
  • datatype variable1 value

17
Defining an Interface
  • The access is either public or not used.
  • When the interface is declared public, it can be
    used by any other code.
  • When the access modifier is not used, the
    interface is only available to other members of
    the same package.

18
Defining an Interface
  • The methods are essentially abstract methods.
    Each class that include an interface must
    implement all of the methods.
  • Variables are implicitly final and static. They
    must also be initialized with a constant value.
  • All methods and variables are implicitly public
    is the interface is declared as public.

19
Implementing an Interface
  • Once an interface has been defined, one or more
    classes can implement that interface.
  • The general form of a class that implementing an
    interface look like this
  • accessmodifier class classname implements
    interface ,interface
  • classbody
  • (InterfaceClass1.java)

20
Implementing an Interface
  • The methods that implement an interface must be
    declared public.
  • The type signature of the implementing method
    must match exactly the type signature specified
    in the interface definition.
  • The implementing classes can also define
    additional members of their own.
  • (InterfaceClass2.java)

21
Accessing Implementations
  • You can declare variables as object references
    that use an interface rather than a class type.
  • When you call a method through one of these
    reference, the correct version will be called
    based on the actual instance of the interface
    being referred to.
  • (Interface1.java)

22
Accessing Implementations
  • An interface reference variable only has
    knowledge of the methods declared by its
    interface declaration, not the ones defined in
    the implementing class.
  • (InterfaceClass3.java Interface2.java)
  • (Interface3.java Interface4.java)

23
Variables in Interfaces
  • You can use interfaces to import shared constants
    into multiple classes by declaring an interface
    that contains variables with desired values.
  • Implementing the interfaces with constants is
    similar to importing constant into the class name
    space as final variables.
  • (Interface5.java)

24
Extending Interfaces
  • One interface can inherit another by use of the
    keyword extends.
  • The syntax is the same as for inheriting classes.
  • When a class implements an interface that
    inherits another interface, it must provide
    implementations for all methods within the
    inheritance chain.
  • (Interface6.java)
Write a Comment
User Comments (0)
About PowerShow.com