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

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

 Pearson Education, Inc. All rights reserved static Class Members static fields – Also known as class variables – Represents class-wide.
Chapter 3 Introduction to Classes, Objects, Methods, and Strings
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look - modified by Eileen Kraemer.
Chapter 10 Classes Continued
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Lecture 9 Polymorphism Richard Gesick.
Dale Roberts Object Oriented Programming using Java - Packages Dale Roberts, Lecturer Computer Science, IUPUI Department.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Spring CISC6795: Object Oriented Programming II.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers.  Known as.
 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
 2006 Pearson Education, Inc. All rights reserved Polymorphism, Interfaces & Operator Overloading.
 2005 Pearson Education, Inc. All rights reserved Classes and Objects: A Deeper Look.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Introduction to Java Chapter 7 - Classes & Object-oriented Programming1 Chapter 7 Classes and Object-Oriented Programming.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
Object Oriented Programming
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Programming, Second Edition Chapter Twelve Advanced Inheritance Concepts.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Classes, Interfaces and Packages
 2005 Pearson Education, Inc. All rights reserved. 1 Classes and Objects: A Deeper Look.
Inheritance ndex.html ndex.htmland “Java.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Polymorphism Chapter 10.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Chapter 3 Introduction to Classes, Objects Methods and Strings
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
Chapter 6 Methods: A Deeper Look
IFS410: Advanced Analysis and Design
Lecture 22 Inheritance Richard Gesick.
Object Oriented Programming in java
Object Oriented Programming in java
Presentation transcript:

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

(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 Employee s and call method earnings for each Employee subclass object.  Method calls processed polymorphically.

(C) 2010 Pearson Education, Inc. All rights reserved. The diagram in Fig 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.

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

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

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

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

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

Polymorphism Example: Drawing Revisit the shape example real drawing!?

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

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

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

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

(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 or java.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

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

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

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

(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 ).

(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 directory com deitel jhtp ch08 The directory names in the package declaration specify the exact location of the classes in the package.

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

(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).

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

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

(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/tool s/index.html#genera  The section entitled “General Information” contains information on setting the classpath for UNIX/Linux and Windows.

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

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

(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 demonstrates several JLabel features, including a plain text label, an image label and a label with both text and an image.

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

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