59
(C) 2010 Pearson Education, Inc. All rights reserved. • abstract class Employee represents the general concept of an employee. • Subclasses: SalariedEmployee, CommissionEmployee , HourlyEmployee and BasePlusCommissionEmployee (an indirect subclass) • Fig. 10.2 shows the inheritance hierarchy for our polymorphic employee-payroll application. • Abstract class names are italicized in the UML.

(C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

Embed Size (px)

Citation preview

Page 1: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• abstract class Employee represents the general concept of an employee.

• Subclasses: SalariedEmployee, CommissionEmployee , HourlyEmployee and BasePlusCommissionEmployee (an indirect subclass)

• Fig. 10.2 shows the inheritance hierarchy for our polymorphic employee-payroll application.

• Abstract class names are italicized in the UML.

Page 2: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 3: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Abstract superclass Employee declares the “interface” to the hierarchy—that is, the set of methods that a program can invoke on all Employee objects. We use the term “interface” here in a general sense to

refer to the various ways programs can communicate with objects of any Employee subclass.

• Each employee has a first name, a last name and a social security number defined in abstract superclass Employee.

Page 4: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Class Employee (Fig. 10.4) provides methods earnings and toString, in addition to the get and set methods that manipulate Employee’s instance variables.

• An earnings method applies to all employees, but each earnings calculation depends on the employee’s class. An abstract method—there is not enough information to

determine what amount earnings should return. Each subclass overrides earnings with an appropriate

implementation.

• Iterate through the array of Employees and call method earnings for each Employee subclass object. Method calls processed polymorphically.

Page 5: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• The diagram in Fig. 10.3 shows each of the five classes in the hierarchy down the left side and methods earnings and toString across the top.

• For each class, the diagram shows the desired results of each method.

• Declaring the earnings method abstract indicates that each concrete subclass must provide an appropriate earnings implementation and that a program will be able to use superclass Employee variables to invoke method earnings polymorphically for any type of Employee.

Page 6: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 7: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 8: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Fig. 10.9 creates an object of each of the four concrete. Manipulates these objects nonpolymorphically, via

variables of each object’s own type, then polymorphically, using an array of Employee variables.

• While processing the objects polymorphically, the program increases the base salary of each BasePlusCommissionEmployee by 10% Requires determining the object’s type at execution time.

• Finally, the program polymorphically determines and outputs the type of each object in the Employee array.

Page 9: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 10: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 11: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 12: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Every object in Java knows its own class and can access this information through the getClass method, which all classes inherit from class Object. The getClass method returns an object of type Class (from package java.lang), which contains information about the object’s type, including its class name.

The result of the getClass call is used to invoke getName to get the object’s class name.

Page 13: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• There are four ways to assign superclass and subclass references to variables of superclass and subclass types.

• Assigning a superclass reference to a superclass variable is straightforward.

• Assigning a subclass reference to a subclass variable is straightfor-ward.

• Assigning a subclass reference to a superclass variable is safe, because the subclass object is an object of its superclass. The superclass variable can be used to refer only to superclass

members. If this code refers to subclass-only mem-bers through the superclass

variable, the compiler reports errors.

Page 14: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Attempting to assign a superclass reference to a subclassvariable is a compilation error. To avoid this error, the superclass reference must

be cast to a subclass type explicitly. At execution time, if the object to which the

reference refers is not a subclass object, an exception will occur.

Use the instanceof operator to ensure that such a cast is performed only if the object is a subclass object.

Page 15: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

Polymorphism Example: Drawing

• Revisit the shape example• real drawing!?

Page 16: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• The next example stores information about the displayed shapes so that we can reproduce them each time the system calls paintComponent.

• We’ll make “smart” shape classes that can draw themselves by using a Graphics object.

• Figure 8.21 declares class MyLine, which has all these capabilities.

• Method paintComponent in class DrawPanel iterates through an array of MyLine objects. Each iteration calls the draw method of the current MyLine object

and passes it the Graphics object for drawing on the panel.

Page 17: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 18: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 19: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 20: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 21: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 22: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 23: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 24: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Shape classes have many similarities. • Using inheritance, we can “factor out” the

common features from all three classes and place them in a single shape superclass.

• Then, using variables of the superclass type, we can manipulate objects of all three shape types polymorphically.

• Removing the redundancy in the code will result in a smaller, more flexible program that is easier to maintain.

Page 25: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 26: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Class MyBoundedShape can be used to factor out the common features of classes MyOval and MyRectangle.

Page 27: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 28: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• All classes in Java inherit directly or indirectly from Object, so its 11 methods are inherited by all other classes.

• Figure 9.12 summarizes Object’s methods. • Can learn more about Object’s methods in the online API

documentation and in The Java Tutorial at :java.sun.com/javase-/6/docs/api/java/lang/Object.html

orjava.sun.com/docs/books/tutorial/java/IandI/ objectclass.html

• Every array has an overridden clone method that copies the array. If the array stores references to objects, the objects are not copied—a

shallow copy is performed. • For more information about the relationship between arrays and

class Object, see Java Language Specification, Chapter 10, at java.sun.com/docs/books/jls/third_edition/ html/arrays.html

Page 29: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 30: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 31: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 32: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Each class in the Java API belongs to a package that contains a group of related classes.

• Packages are defined once, but can be imported into many programs.

• Packages help programmers manage the complexity of application components.

• Packages facilitate software reuse by enabling programs to import classes from other packages, rather than copying the classes into each program that uses them.

• Packages provide a convention for unique class names, which helps prevent class-name conflicts.

Page 33: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• The steps for creating a reusable class:• Declare a public class; otherwise, it can be used only by

other classes in the same package.• Choose a unique package name and add a package

declaration to the source-code file for the reusable class declaration. In each Java source-code file there can be only one package

declaration, and it must precede all other declarations and statements.

• Compile the class so that it’s placed in the appropriate package directory.

• Import the reusable class into a program and use the class.

Page 34: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Placing a package declaration at the beginning of a Java source file indicates that the class declared in the file is part of the specified package.

• Only package declarations, import declarations and comments can appear outside the braces of a class declaration.

• A Java source-code file must have the following order: a package declaration (if any), import declarations (if any), then class declarations.

• Only one of the class declarations in a particular file can be public. • Other classes in the file are placed in the package and can be used only

by the other classes in the package. • Non-public classes are in a package to support the reusable classes in

the package.

Page 35: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 36: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 37: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Every package name should start with your Internet domain name in reverse order. For example, our domain name is deitel.com, so our

package names begin with com.deitel. For the domain name yourcollege.edu, the package name

should begin with edu.yourcollege. • After the domain name is reversed, you can choose any

other names you want for your package. We chose to use jhtp as the next name in our package

name to indicate that this class is from Java How to Program.

The last name in our package name specifies that this package is for Chapter 8 (ch08).

Page 38: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Compile the class so that it’s stored in the appropriate package.

• When a Java file containing a package declaration is compiled, the resulting class file is placed in the directory specified by the declaration.

• The package declaration package com.deitel.jhtp.ch08;

• indicates that class Time1 should be placed in the directorycom deitel jhtp ch08

• The directory names in the package declaration specify the exact location of the classes in the package.

Page 39: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• javac command-line option -d causes the javac compiler to create appropriate directories based on the class’s package declaration. The option also specifies where the directories should be

stored.

• Example:javac -d . Time1.java

• specifies that the first directory in our package name should be placed in the current directory (.).

• The compiled classes are placed into the directory that is named last in the package statement.

Page 40: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• The package name is part of the fully qualified class name. Class Time1’s name is actually com.deitel.jhtp.ch08.Time1

• Can use the fully qualified name in programs, or import the class and use its simple name (the class name by itself).

• If another package contains a class by the same name, the fully qualified class names can be used to distinguish between the classes in the program and prevent a name conflict (also called a name collision).

Page 41: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 42: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 43: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Specifying the Classpath During Compilation• When compiling a class that uses classes from

other packages, javac must locate the .class files for all other classes being used.

• The compiler uses a special object called a class loader to locate the classes it needs. The class loader begins by searching the standard Java

classes that are bundled with the JDK. Then it searches for optional packages. If the class is not found in the standard Java classes or

in the extension classes, the class loader searches the classpath, which contains a list of locations in which classes are stored.

Page 44: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• The classpath consists of a list of directories or archive files, each separated by a directory separator Semicolon (;) on Windows or a colon (:) on

UNIX/Linux/Mac OS X. • Archive files are individual files that contain

directories of other files, typically in a compressed format. Archive files normally end with the .jar or .zip

file-name extensions. • The directories and archive files specified in the

classpath contain the classes you wish to make available to the Java compiler and the JVM.

Page 45: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• By default, the classpath consists only of the current directory.

• The classpath can be modified by providing the -classpath option to the javac

compiler setting the CLASSPATH environment variable (not

recommended).• Classpath

• java.sun.com/javase/6/docs/technotes/tools/index.html#genera

The section entitled “General Information” contains information on setting the classpath for UNIX/Linux and Windows.

Page 46: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 47: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 48: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 49: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Specifying the Classpath When Executing an Application• When you execute an application, the JVM must be able to

locate the .class files of the classes used in that application.

• Like the compiler, the java command uses a class loader that searches the standard classes and extension classes first, then searches the classpath (the current directory by default).

• The classpath can be specified explicitly by using either of the techniques discussed for the compiler.

• As with the compiler, it’s better to specify an individual program’s classpath via command-line JVM options. If classes must be loaded from the current directory, be sure to

include a dot (.) in the classpath to specify the current directory.

Page 50: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• If no access modifier is specified for a method or variable when it’s declared in a class, the method or variable is considered to have package access.

• In a program uses multiple classes from the same package, these classes can access each other’s package-access members directly through references to objects of the appropriate classes, or in the case of static members through the class name.

• Package access is rarely used.

Page 51: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 52: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 53: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 54: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• Labels are a convenient way of identifying GUI components on the screen and keeping the user informed about the current state of the program.

• A JLabel (from package javax.swing) can display text, an image or both.

• The example in Fig. 9.13 demonstrates several JLabel features, including a plain text label, an image label and a label with both text and an image.

Page 55: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 56: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 57: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

Page 58: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• An ImageIcon represents an image that can be displayed on a JLabel.

• The constructor for ImageIcon receives a String that specifies the path to the image.

• ImageIcon can load images in GIF, JPEG and PNG image formats.

• JLabel method setText changes the text the label displays.

Page 59: (C) 2010 Pearson Education, Inc. All rights reserved. abstract class Employee represents the general concept of an employee. Subclasses: SalariedEmployee,

(C) 2010 Pearson Education, Inc. All rights reserved.

• An overloaded version of method add that takes two parameters allows you to specify the GUI component to add to a JFrame and the location in which to add it. The first parameter is the component to attach. The second is the region in which it should be placed.

• Each JFrame has a layout to position GUI components. Default layout for a JFrame is BorderLayout. Five regions—NORTH (top), SOUTH (bottom), EAST (right side), WEST

(left side) and CENTER (constants in class BorderLayout) Each region is declared as a constant in class BorderLayout.

• When calling method add with one argument, the JFrame places the component in the BorderLayout’s CENTER automatically.