Chapter 12 Reusable Classes and Packages 2000 McGraw-Hl‖ Introduction to Object-Oriented Programming with Java-Wu Chapter 12-1
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 1 Chapter 12 Reusable Classes and Packages
Chapter 12 objectives After you have read and studied this chapter, you should be able to e Describe four different object categories and use them effectively in designing classes e Define a package and place reusable classes in it e Write a method that calls the superclass's method explicitly by using the reserved word super e Define overloaded methods C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 12-2
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 2 Chapter 12 Objectives After you have read and studied this chapter, you should be able to Describe four different object categories and use them effectively in designing classes. Define a package and place reusable classes in it. Write a method that calls the superclass’s method explicitly by using the reserved word super. Define overloaded methods
Reusability r We say a class is reusable if it can be used in different programs Classes in the javabook package is highly reusable r Not all classes have to be reusable. We may design a class specifically for a given program. Such class is not reusable r We must design the classes accordingly. If we intend the class to be reusable, then we need to design it so that the class is indeed reusable r One way to increase the reusability is to design a class that implements a single well-defined task C 2000 McGraw-Hill troduction to Object-Oriented Programming with Java--Wu Chapter 12-3
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 3 Reusability We say a class is reusable if it can be used in different programs. Classes in the javabook package is highly reusable. Not all classes have to be reusable. We may design a class specifically for a given program. Such class is not reusable. We must design the classes accordingly. If we intend the class to be reusable, then we need to design it so that the class is indeed reusable. One way to increase the reusability is to design a class that implements a single well-defined task
Object Categories r To aid in designing a single-task class, we divide object tasks into four categories o A user interface object handles the interaction between the end user and the program o A controller object supervises other objects in the program An appllication logic object captures the logic of the real world for which the program is written o a storage object takes care of file input and output. C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-4
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 4 Object Categories To aid in designing a single-task class, we divide object tasks into four categories: A user interface object handles the interaction between the end user and the program. A controller object supervises other objects in the program. An application logic object captures the logic of the real world for which the program is written. A storage object takes care of file input and output
Method Overriding r Two techniques to make a class more reusable are method overriding and method overloading r We say a method of a subclass overrides the inherited method when we redefine the method in the subclass Example: setvisible of drawing board overrides setvisible of MainWindow public class DrawingBoard extends MainWindow public void setvisible( boolean state The reserved word super setVisible( state )i super refers to the graphic= getGr aphics()i superclass } C 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12-5
© 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Chapter 12 - 5 Method Overriding Two techniques to make a class more reusable are method overriding and method overloading. We say a method of a subclass overrides the inherited method when we redefine the method in the subclass. Example: setVisible of DrawingBoard overrides setVisible of MainWindow public class DrawingBoard extends MainWindow { . . . public void setVisible( boolean state ) { super.setVisible( state ); graphic = getGraphics( ); } . . . } The reserved word super refers to the superclass