motivations chapter 13 abstract classes and
play

Motivations Chapter 13 Abstract Classes and You have learned how to - PDF document

Motivations Chapter 13 Abstract Classes and You have learned how to write simple programs Interfaces to create and display GUI components. Can you write the code to respond to user actions, such as clicking a button to perform an action?


  1. Motivations Chapter 13 Abstract Classes and ✦ You have learned how to write simple programs Interfaces to create and display GUI components. Can you write the code to respond to user actions, such as clicking a button to perform an action? ✦ In order to write such code, you have to know CS1: Java Programming about interfaces. An interface is for defining Colorado State University common behavior for classes (including unrelated classes). Before discussing interfaces, we Original slides by Daniel Liang introduce a closely related subject: abstract Modified slides by Chris Wilcox classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Objectives Abstract Classes and Abstract Methods ◆ To design and use abstract classes (§13.2). GeometricObject ◆ To generalize numeric wrapper classes, BigInteger , and BigDecimal using the abstract Number class (§13.3). Circle ◆ To process a calendar using the Calendar and GregorianCalendar Rectangle classes (§13.4). TestGeometricObject ◆ To specify common behavior for objects using interfaces (§13.5). ◆ To define interfaces and define classes that implement interfaces Run (§13.5). ◆ To define a natural order using the Comparable interface (§13.6). ◆ To make objects cloneable using the Cloneable interface (§13.7). ◆ To explore the similarities and differences among concrete classes, abstract classes, and interfaces (§13.8). ◆ To design the Rational class for processing rational numbers (§13.9). ◆ To design classes that follow the class-design guidelines (§13.10). Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 4 rights reserved. rights reserved.

  2. object cannot be created from abstract method in abstract class abstract class An abstract method cannot be contained in a An abstract class cannot be instantiated using nonabstract class. If a subclass of an abstract superclass does not implement all the abstract the new operator, but you can still define its methods, the subclass must be defined abstract. In constructors, which are invoked in the other words, in a nonabstract subclass extended from constructors of its subclasses. For instance, an abstract class, all the abstract methods must be the constructors of GeometricObject are implemented, even if they are not used in the invoked in the Circle class and the Rectangle subclass. class. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. abstract class without abstract superclass of abstract class may be method concrete A class that contains abstract methods must A subclass can be abstract even if its be abstract. However, it is possible to define superclass is concrete. For example, the an abstract class that contains no abstract Object class is concrete, but its subclasses, methods. In this case, you cannot create such as GeometricObject, may be abstract. instances of the class using the new operator. This class is used as a base class for defining a new subclass. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 8 rights reserved. rights reserved.

  3. concrete method overridden to be abstract class as type abstract You cannot create an instance from an A subclass can override a method from its abstract class using the new operator, but an superclass to define it abstract. This is rare, abstract class can be used as a data type. but useful when the implementation of the Therefore, the following statement, which method in the superclass becomes invalid in creates an array whose elements are of the subclass. In this case, the subclass must be GeometricObject type, is correct. defined abstract. GeometricObject[] geo = new GeometricObject[10]; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 10 10 rights reserved. rights reserved. The Abstract Calendar Class and Its Case Study: the Abstract Number Class GregorianCalendar subclass Run LargestNumbers Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 11 12 12 rights reserved. rights reserved.

  4. The Abstract Calendar Class and Its The GregorianCalendar Class GregorianCalendar subclass An instance of java.util.Date represents a specific You can use new GregorianCalendar() to construct instant in time with millisecond precision. a default GregorianCalendar with the current time java.util.Calendar is an abstract base class for and use new GregorianCalendar(year, month, date) extracting detailed information such as year, month, to construct a GregorianCalendar with the specified date, hour, minute and second from a Date object. year, month, and date. The month parameter is 0- Subclasses of Calendar can implement specific based, i.e., 0 is for January. 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. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 13 13 14 14 rights reserved. rights reserved. The get Method in Calendar Class Getting Date/Time Information from The get(int field) method defined in the Calendar class is useful to Calendar extract the date and time information from a Calendar object. The fields are defined as constants, as shown in the following. Run TestCalendar Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 15 15 16 16 rights reserved. rights reserved.

  5. What is an interface? Interfaces Why is an interface useful? What is an interface? Why is an interface useful? An interface is a classlike construct that contains How do you define an interface? only constants and abstract methods. In many How do you use an interface? ways, an interface is similar to an abstract class, but the intent of an interface is to specify common behavior for objects. For example, you can specify that the objects are comparable, edible, cloneable using appropriate interfaces. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 17 17 18 18 rights reserved. rights reserved. Define an Interface Interface is a Special Class To distinguish an interface from a class, Java uses the An interface is treated like a special class in Java. following syntax to define an interface: Each interface is compiled into a separate bytecode public interface InterfaceName { file, just like a regular class. Like an abstract class, constant declarations; you cannot create an instance from an interface abstract method signatures; } using the new operator, but in most cases you can use an interface more or less the same way you use Example : an abstract class. For example, you can use an public interface Edible { interface as a data type for a variable, as the result /** Describe how to eat */ of casting, and so on. public abstract String howToEat(); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 19 19 20 20 rights reserved. rights reserved.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend