Richard's J2SE Day 6
Packages, Interfaces, and Other Class Features

=================================================================
Old notes from "Sams Teach Yourself Java 2 in 21 Days" (still vaild, but different page number)
=================================================================

(P141) More about classes

Modifiers (P142)
Modifiers are keywords that you add to those definitions in order to change their meanings.

Using more than one modifer, can place them in any order. (method return type e.g. void is not modifier)

Access Control for Methods and Variables (P143)
Encapsulation is the process that prevent class variables from being read or modified by other classes.
(P147) Tables: The different levels of access control

Visibility

public protected default private
From the same class

yes yes yes yes
From any class in the same package yes yes yes no
From any class outside the package yes no no no
From a subclass in the same package yes yes yes no
From a subclass outside the same package yes yes no no

General rule: You cannot override a method and make the new method more controlled than the original.
However, you can make it more public.

Static Variables and Methods (P148)
Class variables and methods can be accessed using the class name followed by a dot and the name of the variable or method.
Using the class name is better, as instance variables and methods can never be referred by a class name.
Note: skpped example P149

Final Classes, Methods and Variables (P150)
Final => they will not be changed

Abstract Classes and Methods (P152)
The higher the class, the more abstract its definition.
A class at the top of a hierarchy can define only the behavior and attributes that are common to all the classes.
At times you find a call that does not ever need to be instantiated directly. Such a class serves as a place to hold common behavior and attributes shared by their subclasses. These classes are called abstract classes.

public abstract class Palette {
// ...
}


For example, java.awt.Component
Abstract classes can contain anything a normal class can.
Abstract classes also can contain abstract methods, which are method signatures with no implementation. These methods are implemented in subclasses of the abstract class. You cannot declare an abstract method in a non-abstract class.
If an abstract class has nothing but abstract mehtods, you're better off using a interface. (to be discussed later)

Packages (P153)
A way of organizing groups of classes. A package contains any number of class that are related in purpose, in scope, or by inheritance.
Packages are useful for several reasons:

Packages can also contain other packages.
Java class library is organized along these lines. The top level is called java; next level io, net, util and awt etc.

Using Packages (P154)
To use a class contained in a package, 3 ways

Full Package and Class Names
To refer to a class in some other package, you can use its full name.
java.awt.Font f = new java.awt.Font()
If you use that class multiple times, you should import that class.

Create own packages, place all files in a package in the same folder.
E.g. TransferBook class ... is part of ... com.prefect.library package
The following statemet should be the first statement in the source code of the class:
package com.prefect.library
store the TansferBook class in com\prefect\library subfolder where java command was entered
or com\prefect\library subfolder of any folder in the CLASSPATH setting

The import Declaration
To import individual classe from a package, import java.util.Vector
To import an entire package of classes, import.java.awt.* (note: does not import sub-packages)

In C or C++, #include results in a very large program, as it includes source codes. For Java it is not the case, import indicates only where the Java compiler can find a class.

Name Conflicts (P156)
When you have multimple classes with the same name from different packages, java compiler will not compile the program.

Creating Your Own Packages (P157)
Follow 3 basic steps

Picking a Package Name (P158)
Name convention suggested by Sun: use Internet domain name with the elements reversed: com.naviseek (unique), use lowercase to distinguish from class names

Creating the Folder Structure (P158)
package name: com.naviseek.canasta => com\naviseek\canasta\

Adding a Class to a Package (P159)
package declaration must be the first line of code in the source file
package com.naviseek.canasta

Interfaces (P160)
Interfaces, like abstract classes and methods, provide templates of behavior that other classes are expected to implement. However, provide more functionality.

The Problem of Single Inheritance (P160)
Classes can have only one immediate superclass in Java.
Other OOP include the concept of multimple inheritance.

Java has another hierarchy, which is a hierarchy of mixable behavior classes. New class has only one primary superclass, but it can pick and choose different common behaviors from the other hierarchy. This other hierarchy is the interface hierarchy. A Java interface is a collection of abstract behavior that can be mixed into any class to add to that class's behavior that is not supplied by its superclasses.

Internace contains nothing but abstract method definitions and constants, but no instance variables and no method implementations.

Interfaces and Classes (P161)
Interfaces, like classes, are declared in source files and are complied into .class files.

To use an interface include the implement keyword as part of the class definition:

public class AnimatedSign extends javax.swing.JApplet implements Runnable {
// ...
}


Note that you have to implement all the methods in that interface, cannot pick and choose.

Implementing Multiple Interfaces (P163)
You can include as many interfaces as you need in your own classes.

public class AnimateSign extends javax.swing.JApplet implements Runnable, Observable {
// ...
}

If two different interfaces both define the same method

Other Uses of Interface (P163)
You can declare a variable to be an interface type. It means that any object the variable refers to is expected to have implemented that interface
You can also cast objects to an interface.

Creating and Extending Interfaces (P164)
New Inferfaces
To create a new Interface
interface Growable {
// ....
}

Inside the interface definition, you have methods and constants.
The method definitions are public and abstract methods. (cannot be private or protected)
Methods do not have bodies. (pure design; no implementation)
Variables must be declared public, static and final.

Interface must have either public or package protection, just like classes. Interfaces without the public modifier do not automatically convert their methods to public and abstract or their constants to public.

Methods Inside Interfaces (P165)
Those methods are supposed to be abstract and apply to any kind of class.
By defining your method parameters to be interface type, you can create generic parameters that apply to any class that might use the interface. (cast it later)

Extending Interfaces (P166)
You can organize interfaces into a hierarchy. When one interface inherits from another interface, that "subinterface" acquires all the method definitions and constants.

Creating an Online Storefront (P167)
Two main tasks

Two classes: Storefront and Items
New package called com.prefect.ecommerce:

Item class implements the Comparable interface. (CompareTo(Object) method)
Note: After you have implemented the Comparable interface for an object, there are two class methods that can be called to sort an array, linked list, or other collection of those objects.
All the instance variables of this class are private, simple accessor methods are created.

After creating the Storefront class, develop a program GiftShop.java that makes use of the com.prefect.ecommerce package.
Don't save GiftShop.java in the same folder where the classes of the com.prefect.commerce package are stored. It's not part of the package.

Inner Classes
So far the classes are all members of a package. Classes that belong to a package are known as top-level classes.
Beginning with Java 1.1, you could define a class inside a class. => inner classes.
DisplayResult.java

Exercise
Modified Storefront project with noDiscount variable
Package = Item.java, Storefront.java; Program = GiftShop.java

 

=================================================================
New notes from "Sams Teach Yourself Java 6 in 21 Days"
=================================================================

Inner Classes example SquareTool.java
// run at command line with a argument e.g. "java SquareTool 13"