Chapter 9 Abstract Classes and Interfaces@D Prerequisites for Part II hapter 5 Arrays Chapter 6 Obiects and Classes hapter sTrings You can cover GUI after Chapter 8 hapter 8 Inheritance and polymorphism hapter 11 Getting Started with GUI Programming hapter 9 Abstract Classes and Interfaces Chapter 12 Event-Driven Programm hapter 10 Object-Oriented Modeling hapter 15 Exceptions and Assertions You can cover Exceptions and 1O after Chapter 8 Chapter 16 Simple Input and Output Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 9 Abstract Classes and Interfaces Prerequisites for Part II Chapter 6 Objects and Classes Chapter 7 Strings Chapter 8 Inheritance and Polymorphism Chapter 5 Arrays Chapter 9 Abstract Classes and Interfaces Chapter 10 Object-Oriented Modeling Chapter 11 Getting Started with GUI Programming Chapter 12 Event-Driven Programming Chapter 15 Exceptions and Assertions Chapter 16 Simple Input and Output You can cover Exceptions and I/O after Chapter 8 You can cover GUI after Chapter 8
Objectives To design and use abstract classes($9.2) To process calendar using the Calendar and gregorianCalendar classes(§9.3 To declare interfaces to model weak inheritance relationships (s9. 4) To define a natural order using the comparable interface($9. 4) To know the similarities and differences between an abstract class and interface(§9.4) To enable objects cloneable using the Cloneable interface(89.4.4 Optional) To use wrapper classes( Byte, Short, Integer, Long, Float, double Character, and Boolean)to wrap primitive data values into objects (§9.5) To create a generic sort method(89.5) To simplify programming using JDK 1.5 automatic conversion between primitive types and wrapper class types($9.6) Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives To design and use abstract classes (§9.2). To process calendar using the Calendar and GregorianCalendar classes (§9.3). To declare interfaces to model weak inheritance relationships (§9.4). To define a natural order using the Comparable interface (§9.4). To know the similarities and differences between an abstract class and interface (§9.4). To enable objects cloneable using the Cloneable interface (§9.4.4 Optional). To use wrapper classes (Byte, Short, Integer, Long, Float, Double, Character, and Boolean) to wrap primitive data values into objects (§9.5). To create a generic sort method (§9.5). To simplify programming using JDK 1.5 automatic conversion between primitive types and wrapper class types (§9.6)
The abstract Modifier ● The abstract class O Cannot be instantiated O Should be extended and implemented in subclasses ● The abstract method OMethod signature without implementation Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 The abstract Modifier ⚫ The abstract class Cannot be instantiated Should be extended and implemented in subclasses ⚫ The abstract method Method signature without implementation
Abstract classes UML Notation he abstract class name and the abstract method names are italicized Circle Cylinder Object Geometricobject radius double length: double color: String getRadiuso: double getLength(: double filled boolean atRadius(radius: double): void setLength(length: double): void find Volume(: double get Color): String et Color( String color): void isFllledo: boolean FsetFilled(boolean filled):void findarea0: double Rectangle findPerimeter0: double width double length double get Width(: double t Width(width: double): vold getLength(: double setLength(length: double ): void GeometricObject Circle Cylinder Rectangle Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Abstract Classes GeometricObject Circle Cylinder Rectangle Circle -radius: double +getRadius(): double +setRadius(radius: double): void Cylinder -length: double +getLength(): double +setLength(length: double): void +findVolume(): double GeometricObject -color: String -filled: boolean +getColor(): String +setColor(String color): void +isFilled(): boolean +setFilled(boolean filled): void +findArea(): double +findPerimeter(): double Object Rectangle -width: double -length: double +getWidth(): double +setWidth(width: double): void +getLength(): double +setLength(length: double): void UML Notation: The abstract class name and the abstract method names are italicized
Abstract classes OAn abstract method cannot be contained in a nonabstract class OIf a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract OIn other words in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Abstract Classes ⚫An abstract method cannot be contained in a nonabstract class. ⚫If a subclass of an abstract superclass does not implement all the abstract methods, the subclass must be declared abstract. ⚫In other words, in a nonabstract subclass extended from an abstract class, all the abstract methods must be implemented, even if they are not used in the subclass