© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline.

Slides:



Advertisements
Similar presentations
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 27 - Java Object-Oriented Programming Outline 27.1Introduction 27.2Superclasses and Subclasses.
(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.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
1 Evan Korth New York University Inheritance and Polymorphism Professor Evan Korth New York University.
Chapter 10 Classes Continued
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
C++ Polymorphism Systems Programming. Systems Programming: Polymorphism 2   Polymorphism Examples   Relationships Among Objects in an Inheritance.
Object-Oriented Programming: Polymorphism
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Chapter 12: Adding Functionality to Your Classes.
 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.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Inheritance in the Java programming language J. W. Rider.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 20 - C++ Virtual Functions and Polymorphism.
 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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Object Oriented Programming
Chapter 10: Introduction to Inheritance. Objectives Learn about the concept of inheritance Extend classes Override superclass methods Call constructors.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
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.
Inheritance ndex.html ndex.htmland “Java.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Object-Oriented Programming: Polymorphism Chapter 10.
 2002 Prentice Hall. All rights reserved. Page 1 Inheritance: Object-Oriented Programming Outline 9.1 Introduction 9.2 Superclasses and Subclasses 9.3.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance and Polymorphism
Week 4 Object-Oriented Programming (1): Inheritance
Object-Oriented Programming: Polymorphism
Lecture 23 Polymorphism Richard Gesick.
Inheritance, Polymorphism, and Interfaces
Object-Oriented Programming: Polymorphism
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Advanced Java Topics Chapter 9
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Presentation transcript:

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 27 - Java Object-Oriented Programming Outline 27.1Introduction 27.2Superclasses and Subclasses 27.3 protected Members 27.4Relationship between Superclass Objects and Subclass Objects 27.5Implicit Subclass-Object-to-Superclass-Object Conversion 27.6Software Engineering with Inheritance 27.7Composition vs. Inheritance 27.8Introduction to Polymorphism 27.9Type Fields and switch Statements 27.10Dynamic Method Binding final Methods and Classes 27.12Abstract Superclasses and Concrete Classes 27.13Polymorphism Example 27.14New Classes and Dynamic Binding 27.15Case Study: Inheriting Interface and Implementation 27.16Case Study: Creating and Using Interfaces 27.17Inner Class Definitions 27.18Notes on Inner Class Definitions 27.19Type-Wrapper Classes for Primitive Classes

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Objectives In this chapter, you will learn: –To understand inheritance and software reusability. –To understand superclasses and subclasses. –To appreciate how polymorphism makes systems extensible and maintainable. –To understand the distinction between abstract classes and concrete classes. –To learn how to create abstract classes and interfaces.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.1Introduction Object-Oriented Programming (OOP) –Inheritance - form of software reusability New classes created from existing ones –Absorb attributes and behaviors, and add in their own Subclass inherits from superclass –Direct superclass - subclass explicitly inherits –Indirect superclass - subclass inherits from two or more levels up the class hierarchy –Polymorphism Write programs in a general fashion to handle a wide variety of classes

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.1Introduction Object-Oriented Programming –Introduce protected member access Subclass methods and methods of other classes in the same package can access protected superclass members. –Abstraction - Seeing the big picture –Relationships "is a" - inheritance –Object of subclass "is an" object of the superclass "has a" - composition –Object "has an" object of another class as a member

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.1Introduction Object-Oriented Programming –A subclass cannot directly access private members of its superclass. –Class libraries Someday software may be constructed from standardized, reusable components (like hardware) Create more powerful software

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.2Superclasses and Subclasses Inheritance example –A rectangle "is a" quadrilateral Rectangle is a specific type of quadrilateral Quadrilateral is the superclass, rectangle is the subclass Incorrect to say quadrilateral "is a" rectangle –Naming can be confusing because subclass has more features than superclass Subclass more specific than superclass Every subclass "is an" object of its superclass, but not vice- versa

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.2Superclasses and Subclasses

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.2Superclasses and Subclasses –Form tree-like hierarchal structures Create a hierarchy for class Shape

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.2Superclasses and Subclasses Using inheritance –Use keyword extends class TwoDimensionalShape extends Shape{... } –private members of superclass not directly accessible to subclass –All other variables keep their member access

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved protected Members In a superclass –public members Accessible anywhere program has a reference to a superclass or subclass type –private members Accessible only in methods of the superclass –protected members Intermediate protection between private and public Only accessible by methods of superclass, of subclass, or classes in the same package Subclass methods –Can refer to public or protected members by name –Overridden methods accessible with super.methodName

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.4Relationship between Superclass Objects and Subclass Objects Object of subclass –Can be treated as object of superclass Reverse not true –Suppose many classes inherit from one superclass Can make an array of superclass references Treat all objects like superclass objects –Explicit cast Convert superclass reference to a subclass reference (downcasting) Can only be done when superclass reference actually referring to a subclass object

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Point.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Point.java (2 of 2) Circle.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Circle.java (2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InheritanceTest.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. InheritanceTest.java (2 of 2)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.4Relationship between Superclass Objects and Subclass Objects Figure 27.3 Assigning subclass references to superclass references - InheritanceTest.java

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.4Relationship between Superclass Objects and Subclass Objects Extending a class –To invoke superclass constructor explicitly (called implicitly by default) super(); //can pass arguments if needed If called explicitly, must be first statement Overriding methods –Subclass can redefine superclass method When method mentioned in subclass, subclass version called Access original superclass method with super.methodName

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.4Relationship between Superclass Objects and Subclass Objects Every Applet has used these techniques –Java implicitly uses class Object as superclass for all classes –We have overridden init and paint when we extended JApplet instanceof operator –if (p instanceof Circle) –Returns true if the object to which p points "is a" Circle

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.5Implicit Subclass-Object-to- Superclass-Object Conversion References to subclass objects –May be implicitly converted to superclass references Makes sense - subclass contains members corresponding to those of superclass –Referring to a subclass object with a superclass reference Allowed - a subclass object "is a" superclass object Can only refer to superclass members –Referring to a superclass object with a subclass reference Error Must first be cast to a superclass reference –Need way to use superclass references but call subclass methods Discussed later in the chapter

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.6Software Engineering with Inheritance Inheritance –Customize existing software Create a new class, add attributes and behaviors as needed Software reuse key to large-scale projects –Java and OOP does this –Availability of class libraries and inheritance Superclass –Specifies commonality –Look for commonality among a set of classes "Factor it out" to form the superclass –Subclasses are then customized

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.7Composition vs. Inheritance "is a" relationship –Inheritance "has a" relationship –Composition, having other objects as members Example Employee “is a” BirthDate; //Wrong! Employee “has a” BirthDate; //Composition

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.8Introduction to Polymorphism With polymorphism –Design and implement extensible programs –Generically process superclass objects –Easy to add classes to hierarchy Little or no modification required Only parts of program that need direct knowledge of new class must be changed

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 27.9Type Fields and switch Statements switch statements –Can be used to deal with many objects of different types Appropriate action based on type Problems –Programmer may forget to include a type –Might forget to test all possible cases –Every addition/deletion of a class requires all switch statements to be changed Tracking all these changes is time consuming and error prone –Polymorphic programming can eliminate the need for switch logic Avoids all these problems

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Dynamic Method Binding Dynamic Method Binding –At execution time, method calls routed to appropriate version Example –Circle, Triangle, Rectangle and Square all subclasses of Shape Each has an overridden draw method –Call draw using superclass references At execution time, program determines to which class the reference is actually pointing Calls appropriate draw method

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved final Methods and Classes Defining variables final –Indicates they cannot be modified after definition –Must be initialized when defined Defining methods final –Cannot be overridden in a subclass –static and private methods are implicitly final –Program can inline final methods Actually inserts method code at method call locations Improves program performance Defining classes final –Cannot be a superclass (cannot inherit from it) –All methods in class are implicitly final

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Abstract Superclasses and Concrete Classes Abstract classes (abstract superclasses) –Contain one or more abstract methods –Cannot be instantiated Causes syntax error Can still have instance data and non- abstract methods Can still define constructor –Sole purpose is to be a superclass Other classes inherit from it –Too generic to define real objects –Declare class with keyword abstract

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Abstract Superclasses and Concrete Classes Concrete class –Can instantiate objects –Provide specifics Class hierarchies –Most general classes are usually abstract TwoDimensionalShape - too generic to be concrete

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Polymorphism Example Class Quadrilateral –Rectangle "is a" Quadrilateral –getPerimeter method can be performed on any subclass Square, Parallelogram, Trapezoid Same method takes on "many forms" - polymorphism –Can method is called with superclass reference Java chooses correct overriden method References –Can create references to abstract classes

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Polymorphism Example Iterator classes –Walks through all the objects in a container (such as an array) –Used in polymorphic programming Walk through an array of superclass references Call draw method for each reference

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved New Classes and Dynamic Binding Dynamic binding (late binding) –Accommodates new classes –Object's type does not need to be known at compile time –At execution time, method call matched with object

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Case Study: Inheriting Interface and Implementation Polymorphism example –abstract superclass Shape Subclasses Point, Circle, Cylinder abstract method –getName non- abstract methods –area (return 0.0) –volume (return 0.0) –Class Shape used to define a set of common methods Interface is the three common methods Implementation of area and volume used for first levels of hierarchy –Create an array of Shape references Point them to various subclass objects Call methods through the Shape reference

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Shape.java Point.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Point.java (2 of 2) Circle.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Circle.java (2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Cylinder.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Cylinder.java (2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Test.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Test.java (2 of 2)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Case Study: Inheriting Interface and Implementation Figure 27.4 Shape, point, circle, cylinder hierarchy - Test.java

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Case Study: Creating and Using Interfaces Creating an interface –Keyword interface –Has set of public abstract methods –Can contain public final static data Using interfaces –Class specifies it uses interface with keyword implements –Class must define all abstract methods in interface Must use same number of arguments, same return type –Using interface like signing a contract "I will define all methods specified in the interface"

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Case Study: Creating and Using Interfaces Using interfaces (continued) –Interfaces used in place of abstract classes Used when no default implementation –Typically public data types Interface defined in its own.java file Interface name same as file name –Same "is a" relationship as inheritance Reexamine previous hierarchy –Replace abstract class Shape with interface Shape

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Shape.java Point.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Point.java (2 of 2) Circle.java (1 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Circle.java (2 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Circle.java (3 of 3) Cylinder.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Cylinder.java (2 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Test.java (1 of 2)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Test.java (2 of 2)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Case Study: Creating and Using Interfaces Figure 27.5 Point, circle, cylinder hierarchy with a Shape interface—C ircle.java

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Inner classes –Till now, all classes defined at file scope (not inside other classes) –Inner classes defined inside other classes –Anonymous inner class Has no name Frequently used with GUI event handling

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.java (1 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.java (2 of 3)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Time.java (3 of 3) TimeTest- Window.java (1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (4 of 4)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Figure 27.6 Demonstrating an inner class in a windowed application — TimeTestWindow.java. Title BarClose box Maximize Minimize

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Figure 27.6 Demonstrating an inner class in a windowed application — TimeTestWindow.java.

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Windowed applications –Execute an application in its own window (like an Applet) Inherit from class JFrame ( javax.swing ) rather than JApplet –init method replaced by constructor Instead, create GUI components in constructor Instantiate object in main (guaranteed to be called)

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Event handling –Some class must implement interface ActionListener Must define method actionPerformed Class that implements ActionListener "is an" ActionListener –Method addActionListener Takes object of type ActionListener We can pass it an instance of the class that implements ActionListener ("is a" relationship)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (1 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (2 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (3 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. TimeTest- Window.java (4 of 4)

Outline © Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Program Output Close box

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Figure 27.7 Demonstrating anonymous inner class - TimeTestWindow.java

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Event handling with anonymous Inner classes –Define the inner class inside the call to addActionListener Create an instance of the class inside the method call addActionListener takes an object of class ActionListener

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Example: myField.addActionListener( new ActionListener() { // anonymous inner class public void actionPerformed( ActionEvent e) { Actions } ); –new creates an object –ActionListener() begins definition of anonymous class and calls default constructor Similar to public class myHandler implements ActionListener –Brace ( { ) begins class definition

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Inner Class Definitions Use the following code to allow the user to close windows using the close button: window.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } );

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Notes on Inner Class Definitions Notes –Every class (including inner classes) have their own.class file –Named inner classes can be public, protected, private, or have package access Same restrictions as other members of a class –To access outer class's this reference OuterClassName.this –To create an object of another class's inner class Create an object of outer class and assign it a reference ( ref ) Type statement of form: OuterClassName. InnerClassName innerRef = ref.new InnerClassName ();

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Notes on Inner Class Definitions Notes (continued) –Inner class can be static Does not require object of outer class to be defined Does not have access to outer class's non- static members

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Type-Wrapper Classes for Primitive Types Each primitive type has a type-wrapper class –Enables manipulation of primitive types as objects of class Object –Each type wrapper is declared final, so their methods are implicitly final and may not be overridden –To manipulate a primitive value in your program, first refer to the documentation for the type-wrapper classes—the required method may already be defined.