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
Abstract classes OAn abstract class cannot be instantiated using the new operator, but you can still define its constructors which are invoked in the constructors of its subclasses O For instance the constructors of geometricObject are invoked in the circle class and the rectangle class public Circles(double radius, String color, boolean filled)i super(color, filled this. radius radius: Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 Abstract Classes ⚫An abstract class cannot be instantiated using the new operator, but you can still define its constructors, which are invoked in the constructors of its subclasses. ⚫ For instance, the constructors of GeometricObject are invoked in the Circle class and the Rectangle class. public Circle9(double radius, String color, boolean filled) { super(color, filled); this.radius = radius; }
Abstract classes OA class that contains abstract methods must be abstract O However, it is possible to declare an abstract class that contains no abstract methods OIn the above case, you cannot create instances ofthe class using the new operator. This class is used as a base class for defining a new subclass OA subclass can be abstract even if its superclass is concrete. For example, the object class is concrete, but its subclasses, such as Geometricobject, may be abstract Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 Abstract Classes ⚫A class that contains abstract methods must be abstract. ⚫ However, it is possible to declare an abstract class that contains no abstract methods. ⚫In the above case, you cannot create instances of the class using the new operator. This class is used as a base class for defining a new subclass. ⚫A subclass can be abstract even if its superclass is concrete. For example, the Object class is concrete, but its subclasses, such as GeometricObject, may be abstract
Abstract classes Cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. THerefore, the following statement, which creates an array whose elements are of geometricobject type, Is correct GeometricObject!l geo= new GeometricObject[10 Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Abstract Classes ⚫cannot create an instance from an abstract class using the new operator, but an abstract class can be used as a data type. ⚫Therefore, the following statement, which creates an array whose elements are of GeometricObject type, is correct. GeometricObject[] geo = new GeometricObject[10];
Example 9.1 Using the GeometricObject Class o Objective This example creates two geometric objects: a circle, and a rectangle, invokes the equalArea method to check if the two objects have equal area and invokes the display GeometricObject method to display the objects genercprograng Polymorphism TestGeometricObiect dynamic binding Run Liang, introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Example 9.1 Using the GeometricObject Class ⚫ Objective: This example creates two geometric objects: a circle, and a rectangle, invokes the equalArea method to check if the two objects have equal area, and invokes the displayGeometricObject method to display the objects. TestGeometricObject Run generic programming Polymorphism dynamic binding
The abstract Calendar class and Its Gregorian Calendar subclass An instance of iava. util. Date represents a specific instant in time with millisecond precision javautil. Calendar is an abstract base class for extracting detailed information such as year, month, date, hour minute and second from a date object Subclasses of calendar can implement specific calendar systems such as Gregorian calendar Lunar Calendar and Jewish calendar. Currently, java util. Gregorian Calendar for the gregorian calendar is supported in the Java API Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 The Abstract Calendar Class and Its GregorianCalendar subclass ⚫An instance of java.util.Date represents a specific instant in time with millisecond precision. ⚫java.util.Calendar is an abstract base class for extracting detailed information such as year, month, date, hour, minute and second from a Date object. ⚫Subclasses of Calendar can implement specific calendar systems such as Gregorian calendar, Lunar Calendar and Jewish calendar. Currently, java.util.GregorianCalendar for the Gregorian calendar is supported in the Java API