Archive for September, 2007

472 Object-Oriented Programming Chapter 9 27 28 //

Monday, September 10th, 2007

472 Object-Oriented Programming Chapter 9 27 28 // set coordinates, radius and height 29 cylinder.setHeight( 10 ); 30 cylinder.setRadius( 4.25 ); 31 cylinder.setPoint( 2, 2 ); 32 33 // get String representation of Cylinder and calculate 34 // area and volume 35 output += “nnThe new location, radius ” + 36 “and height of cylinder aren” + cylinder + 37 “nArea is ” + precision2.format( cylinder.area() ) + 38 “nVolume is ” + precision2.format( cylinder.volume() ); 39 40 JOptionPane.showMessageDialog( null, output, 41 “Demonstrating Class Cylinder”, 42 JOptionPane.INFORMATION_MESSAGE ); 43 44 System.exit( 0 ); 45 } 46 47 } // end class Test Fig. 9.15 Testing class Cylinder(part 2 of 2). The series of examples in this section nicely demonstrates inheritance and defining and referencing protectedinstance variables. The reader should now be confident with the basics of inheritance. In the next several sections, we show how to program with inheritance hierarchies in a general manner, using polymorphism. Data abstraction, inheritance and polymorphism are the crux of object-oriented programming. 9.10 Introduction to Polymorphism With polymorphism, it is possible to design and implement systems that are more easily extensible. Programs can be written to process generically as superclass objects objects of all existing classes in a hierarchy. Classes that do not exist during program development can be added with little or no modifications to the generic part of the program as long as those classes are part of the hierarchy that is being processed generically. The only parts of Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming 471 45 // calculate

Monday, September 10th, 2007

Chapter 9 Object-Oriented Programming 471 45 // calculate volume of Cylinder 46 public double volume() 47 { 48 return super.area() * height; 49 } 50 51 // convert the Cylinder to a String 52 public String toString() 53 { 54 return super.toString() + “; Height = ” + height; 55 } 56 57 } // end class Cylinder Fig. 9.14 Class Cylinderdefinition (part 2 of 2). Method mainof the Testapplication (Fig. 9.15) instantiates an object of class Cylinder(line 19), then uses get methods (lines 23 26) to obtain information about the Cylinderobject. Again, the Testapplications s mainmethod cannot reference directly the protected data of class Cylinder. Method main uses set methods setHeight, setRadius and setPoint (lines 29 31) to reset the height, radius and coordinates of the Cylinder. Then main uses toString, area and volume to print the attributes and some facts about the Cylinder. Figure 9.15 is a Test application to test class Cylinder s capabilities. 1 // Fig. 9.15: Test.java 2 // Application to test class Cylinder 3 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 // Deitel packages 11 import com.deitel.jhtp4.ch09.Cylinder; 12 13 public class Test { 14 15 // test class Cylinder 16 public static void main( String args[] ) 17 { 18 // create Cylinder 19 Cylinder cylinder = new Cylinder( 5.7, 2.5, 12, 23 ); 20 DecimalFormat precision2 = new DecimalFormat( “0.00″ ); 21 22 // get coordinates, radius and height 23 String output = “X coordinate is ” + cylinder.getX() + 24 “nY coordinate is ” + cylinder.getY() + 25 “nRadius is ” + cylinder.getRadius() + 26 “nHeight is ” + cylinder.getHeight(); Fig. 9.15 Testing class Cylinder(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

470 Object-Oriented Programming Chapter 9 (Hp web site) Our last example

Sunday, September 9th, 2007

470 Object-Oriented Programming Chapter 9 Our last example is shown in Fig. 9.14 and Fig. 9.15. Figure 9.14 shows the Cylinder class definition with the Cylinder method definitions. Note that class Cylinder extends class Circle. This means that the public interface to Cylinder includes the Circlemethods and Pointmethods as well as the Cylinderconstructor and Cylinder methods setHeight, getHeight, area (which overrides the Circleareamethod), volumeand toString. 1 // Fig. 9.14: Cylinder.java 2 // Definition of class Cylinder 3 package com.deitel.jhtp4.ch09; 4 5 public class Cylinder extends Circle { 6 protected double height; // height of Cylinder 7 8 // no-argument constructor 9 public Cylinder() 10 { 11 // implicit call to superclass constructor here 12 setHeight( 0 ); 13 } 14 15 // constructor 16 public Cylinder( double cylinderHeight, double cylinderRadius, 17 int xCoordinate, int yCoordinate ) 18 { 19 // call superclass constructor to set coordinates/radius 20 super( cylinderRadius, xCoordinate, yCoordinate ); 21 22 // set cylinder height 23 setHeight( cylinderHeight ); 24 } 25 26 // set height of Cylinder 27 public void setHeight( double cylinderHeight ) 28 { 29 height = ( cylinderHeight >= 0 ? cylinderHeight : 0 ); 30 } 31 32 // get height of Cylinder 33 public double getHeight() 34 { 35 return height; 36 } 37 38 // calculate area of Cylinder (i.e., surface area) 39 public double area() 40 { 41 return 2 * super.area() + 42 2 * Math.PI * radius * height; 43 } 44 Fig. 9.14 Class Cylinderdefinition (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming 469 4 // Java (Web hosting india)

Saturday, September 8th, 2007

Chapter 9 Object-Oriented Programming 469 4 // Java core packages 5 import java.text.DecimalFormat; 6 7 // Java extension packages 8 import javax.swing.JOptionPane; 9 10 // Deitel packages 11 import com.deitel.jhtp4.ch09.Circle; 12 13 public class Test { 14 15 // test class Circle 16 public static void main( String args[] ) 17 { 18 // create a Circle 19 Circle circle = new Circle( 2.5, 37, 43 ); 20 DecimalFormat precision2 = new DecimalFormat( “0.00″ ); 21 22 // get coordinates and radius 23 String output = “X coordinate is ” + circle.getX() + 24 “nY coordinate is ” + circle.getY() + 25 “nRadius is ” + circle.getRadius(); 26 27 // set coordinates and radius 28 circle.setRadius( 4.25 ); 29 circle.setPoint( 2, 2 ); 30 31 // get String representation of Circle and calculate area 32 output += 33 “nnThe new location and radius of c aren” + circle + 34 “nArea is ” + precision2.format( circle.area() ); 35 36 JOptionPane.showMessageDialog( null, output, 37 “Demonstrating Class Circle”, 38 JOptionPane.INFORMATION_MESSAGE ); 39 40 System.exit( 0 ); 41 } 42 43 } // end class Test Fig. 9.13 Testing class Circle(part 2 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

468 Object-Oriented Programming Chapter 9 (Yahoo web space) 15 // constructor

Saturday, September 8th, 2007

468 Object-Oriented Programming Chapter 9 15 // constructor 16 public Circle( double circleRadius, int xCoordinate, 17 int yCoordinate ) 18 { 19 // call superclass constructor to set coordinates 20 super( xCoordinate, yCoordinate ); 21 22 // set radius 23 setRadius( circleRadius ); 24 } 25 26 // set radius of Circle 27 public void setRadius( double circleRadius ) 28 { 29 radius = ( circleRadius >= 0.0 ? circleRadius : 0.0 ); 30 } 31 32 // get radius of Circle 33 public double getRadius() 34 { 35 return radius; 36 } 37 38 // calculate area of Circle 39 public double area() 40 { 41 return Math.PI * radius * radius; 42 } 43 44 // convert the Circle to a String 45 public String toString() 46 { 47 return “Center = ” + “[” + x + “, ” + y + “]” + 48 “; Radius = ” + radius; 49 } 50 51 } // end class Circle Fig. 9.12 Circleclass definition (part 2 of 2). Application Test (Fig. 9.13) instantiates an object of class Circle (line 19), then uses get methods to obtain the information about the Circleobject. Method mainindirectly references the protected data of class Circle through method calls. Method main then uses set methods setRadiusand setPoint to reset the radius and coordinates of the center of the circle. Finally, main displays the Circle object circle and calculates and displays its area. 1 // Fig. 9.13: Test.java 2 // Applet to test class Circle 3 Fig. 9.13 Testing class Circle(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Java web server - Chapter 9 Object-Oriented Programming 467 17 // get

Friday, September 7th, 2007

Chapter 9 Object-Oriented Programming 467 17 // get coordinates 18 String output = “X coordinate is ” + point.getX() + 19 “nY coordinate is ” + point.getY(); 20 21 // set coordinates 22 point.setPoint( 10, 10 ); 23 24 // use implicit call to point.toString() 25 output += “nnThe new location of point is ” + point; 26 27 JOptionPane.showMessageDialog( null, output, 28 “Demonstrating Class Point”, 29 JOptionPane.INFORMATION_MESSAGE ); 30 31 System.exit( 0 ); 32 } 33 34 } // end class Test Fig. 9.11 Testing class Point(part 2 of 2). Our next example imports the Point class definition from Fig. 9.10, so we do not show the class definition again here. Figure 9.12 shows the Circle class definition with the Circle method definitions. Note that class Circle extends class Point. This means that the publicinterface to Circle includes the Point methods as well as the Circle methods setRadius, getRadius, area, toStringand the Circleconstructors. 1 // Fig. 9.12: Circle.java 2 // Definition of class Circle 3 package com.deitel.jhtp4.ch09; 4 5 public class Circle extends Point { // inherits from Point 6 protected double radius; 7 8 // no-argument constructor 9 public Circle() 10 { 11 // implicit call to superclass constructor occurs here 12 setRadius( 0 ); 13 } 14 Fig. 9.12 Circleclass definition (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Web hosting service - 466 Object-Oriented Programming Chapter 9 22 // set

Thursday, September 6th, 2007

466 Object-Oriented Programming Chapter 9 22 // set x and y coordinates of Point 23 public void setPoint( int xCoordinate, int yCoordinate ) 24 { 25 x = xCoordinate; 26 y = yCoordinate; 27 } 28 29 // get x coordinate 30 public int getX() 31 { 32 return x; 33 } 34 35 // get y coordinate 36 public int getY() 37 { 38 return y; 39 } 40 41 // convert into a String representation 42 public String toString() 43 { 44 return “[” + x + “, ” + y + “]”; 45 } 46 47 } // end class Point Fig. 9.10 Pointclass definition (part 2 of 2). Figure 9.11 shows a Test application for testing class Point. The main method must use getX and getY to read the values of protected instance variables x and y. Remember that protected instance variables are accessible only to methods of their class, their subclasses and other classes in the same package. Also, note the implicit call to toStringwhen pointis added to a Stringat line 25. 1 // Fig. 9.11: Test.java 2 // Applet to test class Point 3 4 // Java extension packages 5 import javax.swing.JOptionPane; 6 7 // Deitel packages 8 import com.deitel.jhtp4.ch09.Point; 9 10 public class Test { 11 12 // test class Point 13 public static void main( String args[] ) 14 { 15 Point point = new Point( 72, 115 ); 16 Fig. 9.11 Testing class Point(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming 465 Software Engineering Observation

Thursday, September 6th, 2007

Chapter 9 Object-Oriented Programming 465 Software Engineering Observation 9.15 Modifications to a superclass do not require subclasses to change as long as the public interface to the superclass remains unchanged. 9.8 Composition vs. Inheritance We have discussed is a relationships that are implemented by inheritance. We have also discussed has a relationships (and seen examples in preceding chapters) in which a class has objects of other classes as members such relationships create new classes by composition of existing classes. For example, given the classes Employee, BirthDate and TelephoneNumber, it is improper to say that an Employee is a BirthDate or that an Employeeis a TelephoneNumber. But it is certainly appropriate to say that an Employeehas a BirthDate and that an Employeehas a TelephoneNumber. 9.9 Case Study: Point, Circle, Cylinder Now let us consider a substantial inheritance example. We consider a point, circle, cylinder hierarchy. First we develop and use class Point(Fig. 9.10 and Fig. 9.11). Then we present an example in which we derive class Circlefrom class Point(Fig. 9.12 and Fig. 9.13). Finally, we present an example in which we derive class Cylinderfrom class Circle (Fig. 9.14 and Fig. 9.15). Figure 9.10 is the class Point definition. Class Pointis defined as part of package com.deitel.jhtp4.ch09 (line 3). Note that Point s instance variables are protected. Thus, when class Circle is derived from class Point, the methods of class Circle will be able to reference coordinates x and y directly rather than using access methods. This could result in better performance. 1 // Fig. 9.10: Point.java 2 // Definition of class Point 3 package com.deitel.jhtp4.ch09; 4 5 public class Point { 6 protected int x, y; // coordinates of Point 7 8 // No-argument constructor 9 public Point() 10 { 11 // implicit call to superclass constructor occurs here 12 setPoint( 0, 0 ); 13 } 14 15 // constructor 16 public Point( int xCoordinate, int yCoordinate ) 17 { 18 // implicit call to superclass constructor occurs here 19 setPoint( xCoordinate, yCoordinate ); 20 } 21 Fig. 9.10 Pointclass definition (part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Graphic web design - 464 Object-Oriented Programming Chapter 9 9.7 Software Engineering

Wednesday, September 5th, 2007

464 Object-Oriented Programming Chapter 9 9.7 Software Engineering with Inheritance We can use inheritance to customize existing software. When we use inheritance to create a new class from an existing class, the new class inherits the attributes and behaviors of an existing class; then we can add attributes and behaviors or override superclass behaviors to customize the class to meet our needs. It can be difficult for students to appreciate the problems faced by designers and implementers on large-scale software projects in industry. People experienced on such projects will invariably state that a key to improving the software development process is encouraging software reuse. Object-oriented programming in general, and Java in particular, certainly does this. It is the availability of substantial and useful class libraries that delivers the maximum benefits of software reuse through inheritance. As interest in Java grows, interest in Java class libraries will increase. Just as shrink-wrapped software produced by independent software vendors became an explosive growth industry with the arrival of the personal computer, so, too, will the creation and sale of Java class libraries. Application designers will build their applications with these libraries, and library designers will be rewarded by having their libraries wrapped with the applications. What we see coming is a massive worldwide commitment to the development of Java class libraries for a huge variety of applications arenas. Software Engineering Observation 9.12 Creating a subclass does not affect its superclass’s source code or the superclass s Java byte- codes; the integrity of a superclass is preserved by inheritance. A superclass specifies commonality. All classes derived from a superclass inherit the capabilities of that superclass. In the object-oriented design process, the designer looks for commonality among a set of classes and factors it out to form desirable superclasses. Subclasses are then customized beyond the capabilities inherited from the superclass. Software Engineering Observation 9.13 Just as the designer of non-object-oriented systems should avoid unnecessary proliferation of functions, the designer of object-oriented systems should avoid unnecessary proliferation of classes. Proliferating classes creates management problems and can hinder software reusability, simply because it is more difficult for a potential user of a class to locate that class in a huge collection. The trade-off is to create fewer classes, each providing substantial additional functionality, but such classes might be too rich for certain users. Performance Tip 9.1 When creating a new class, inherit from the class closest to what you need i.e., the one that provides the minimum set of capabilities required for a new class to perform its tasks. Subclasses could inherit data and functionality that they will not use, in which case memory and processing resources may be wasted. Note that reading a set of subclass declarations can be confusing because inherited members are not shown, but inherited members are nevertheless present in the subclasses. A similar problem can exist in the documentation of subclasses. Software Engineering Observation 9.14 In an object-oriented system, classes are often closely related. Factor out common attributes and behaviors and place these in a superclass. Then use inheritance to form subclasses without having to repeat common attributes and behaviors. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Chapter 9 Object-Oriented Programming 463 Lines 14 15 set (Web design templates)

Wednesday, September 5th, 2007

Chapter 9 Object-Oriented Programming 463 Lines 14 15 set circle1 to null, then set circle2 to null. Each of these objects is no longer needed in the program, so Java marks the memory occupied by circle1 and circle2 for garbage collection. Java guarantees that, before the garbage collector runs to reclaim the space for each of these objects, the finalize methods for each object will be called. The garbage collector is a low-priority thread that runs automatically whenever processor time is available. We choose here to ask the garbage collector to run with a call to class System s static method gc in line 17. Java does not guarantee the order in which objects will be garbage collected; therefore, it cannot guarantee which object s finalizer will execute first. Notice, in the command-line output window, that finalize methods are called for both the Circle and Point when each Circle object is garbage collected. 9.6 Implicit Subclass-Object-to-Superclass-ObjectConversion Despite the fact that a subclass object also is a superclass object, the subclass type and the superclass type are different. Subclass objects can be treated as superclass objects. This makes sense because the subclass has members corresponding to each of the superclass members remember that the subclass normally has more members than the superclass has. Assignment in the other direction is not allowed because assigning a superclass object to a subclass reference would leave the additional subclass members undefined. A reference to a subclass object could be implicitly converted into a reference to a superclass object because a subclass object is a superclass object through inheritance. There are four possible ways to mix and match superclass references and subclass references with superclass objects and subclass objects: 1. Referring to a superclass object with a superclass reference is straightforward. 2. Referring to a subclass object with a subclass reference is straightforward. 3. Referring to a subclass object with a superclass reference is safe, because the subclass object is an object of its superclass as well. Such code can refer only to superclass members. If this code refers to subclass-only members through the superclass reference, the compiler will report a syntax error. 4. Referring to a superclass object with a subclass reference is a syntax error. As convenient as it might be to treat subclass objects as superclass objects, and to do this by manipulating all these objects with superclass references, there appears to be a problem. In a payroll system, for example, we would like to be able to walk through an array of employees and calculate the weekly pay for each person. But intuition suggests that using superclass references would enable the program to call only the superclass payroll calculation routine (if indeed there is such a routine in the superclass). We need a way to invoke the proper payroll calculation routine for each object, whether it is a superclass object or a subclass object, and to do this simply by using the superclass reference. Actually, this is precisely how Java behaves and is discussed in this chapter when we consider polymorphism and dynamic binding. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01