Archive for May, 2007

Virtual web hosting - Understanding Inheritance 145 Implementing Inheritance Now that we

Tuesday, May 8th, 2007

Understanding Inheritance 145 Implementing Inheritance Now that we have seen why inheritance is useful in OOP, let s look at how it is implemented in Java. A class uses the extends keyword to inherit from another class. The extends keyword appears after the class name when declaring the class and is followed by the name of the class being extended. For example, the following statement is used to declare that the Salary class is a child of the Employee class: public class Salary extends Employee Similarly, the Hourly class can extend Employee with the statement: public class Hourly extends Employee The following Employee class will be used as the parent of Salary and Hourly. Note that you do not add any special code to denote that Employee is a parent class. public class Employee { public String name; public String address; public int SSN; public int number; public void mailCheck() { System.out.println( Mailing a check to + name + + address); } } The following Salary class uses the extends keyword to denote Salary is a child class of Employee. public class Salary extends Employee { public float salary; //Annual salary public float computePay() { System.out.println( Computing salary pay for + name); return salary/52; } }
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Free web hosting services - 144 Chapter 6 A better design for the

Monday, May 7th, 2007

144 Chapter 6 A better design for the Salary and Hourly classes is to take their common elements and put them in a parent class, leaving the unique elements in the child classes. For example, the mailCheck() method could appear in the Employee parent class, and the computePay() method could appear in each of the child classes. The assumption is that the process of mailing a check is the same for all employees, but computing their pay is directly affected by how they are paid. Employees do not share a common computePay() method. By placing computePay() in each child class, the method will be written twice. This is not repeating code, though, because computePay() in Salary is quite different than computePay() in Hourly. The is a Relationship The is a relationship is a simple but powerful rule for testing if your inheritance is a good design. Whenever you use inheritance, you should be able to say that a child is a parent. If this statement is true, your inheritance is likely a good design. For example, it is true that a salaried employee is an employee. Similarly, an hourly employee is an employee; therefore, it is reasonable that the Salary and Hourly classes extend the Employee class. Let s look at an example where inheritance is not a good idea. Suppose that you have a Date class that represents a calendar date, and you want to use that class to keep track of the date when an employee was hired. Because inheritance has so many benefits, you decide to have the Employee class extend the Date class. When you instantiate an Employee object, you will also get a Date object for storing the employee s hire date; however, is it true that an employee is a date? The is a relationship clearly fails here. Although the result might work for us, an Employee class inheriting from a Date class is not a good design and should not be used. The solution to the improper use of inheritance with the Employee and Date classes is to realize that an employee has a hire date, not that an employee is a hire date. If an object has an attribute, the attribute should be a field in the class. The Employee class should add a field of type Date to represent the hire date of an employee, as opposed to extending the Date class.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

Understanding Inheritance 143 System.out.println( Mailing a check to (Jetty web server)

Monday, May 7th, 2007

Understanding Inheritance 143 System.out.println( Mailing a check to + name + + address); } public float computePay() { return (float) salary/52.0; } } public class Hourly { public String name; public String address; public int SSN; public int number; public float hourlyRate; public float hoursWorked public void mailCheck() { System.out.println( Mailing a check to + name + + address); } public float computePay() { return (float) hoursWorked * hourlyRate; } } The Salary and Hourly classes demonstrate the need for inheritance. Although Salary and Hourly employees are different types, they are not entirely different. In fact, the two types of employees have a lot in common, as seen by the repetition of fields and methods in these two classes. Using inheritance will improve this design considerably, so keep in mind that writing two separate classes for the two different types of employees is not yet a satisfactory solution. As you can see, the Salary and Hourly classes are repeating code. Salary and hourly employees are still employees, and there is a lot of information in common between the two. Inheritance can be used in this situation not only to avoid repeating code, but also to create a program design that allows for better maintenance and code changes later. When two or more classes are different but share similar features, take the common elements of the classes and put them in a parent class. The classes can extend this parent class, thereby inheriting all the features of the parent, yet the different features can remain in each child class.
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Sex offenders web site - 142 Chapter 6 . When to Use Inheritance

Sunday, May 6th, 2007

142 Chapter 6 . When to Use Inheritance (continued) //Perform arithmetic for salaried employee break; case 1: //Perform arithmetic for hourly employee break; case 2: //Perform arithmetic for contractor employee break; //and so on if necessary } } And the computePay() method will keep getting longer and longer as different types of employees arise. Any time you find yourself writing classes that do not know what type they are, you probably should rethink your design. With inheritance, all of these issues are avoided. The common features of employees appear in a parent class. Each different type of employee is represented by a child class. When a new type of employee such as a contractor comes along, a new child class is written that extends the parent class. None of the existing code needs to be touched. This benefit is difficult to achieve with procedural programming and also with poorly designed OOP applications. To avoid these situations, constantly test your OOP design against these simple rules: An object has an attribute, and an object does a behavior. In our employee example, if it is not true that an employee has a salary, an Employee class should not have a salary field, and we should redesign our program. The Salary class should have a field to represent the employee s annual salary because a Salaried employee has a salary. The Hourly class should have fields to represent the employee s hourly pay and number of hours worked because these are attributes of an Hourly employee. The following classes demonstrate what the Salary and Hourly classes might look like. Look closely at the attributes and behaviors of these two classes. As with the Employee class, there is something flawed with this design also. public class Salary { public String name; public String address; public int SSN; public int number; public float salary; public void mailCheck() {
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

Understanding Inheritance 141 . (Free web hosts) When to Use Inheritance

Sunday, May 6th, 2007

Understanding Inheritance 141 . When to Use Inheritance The Employee class has a field named salary, which implies that an employee has a salary; however, hourly employees have an hourly rate, which is quite different from a salary. A tempting fix for this situation is to add a Boolean field to the Employee class named isSalary that is true for salary employees and false for hourly employees. The salary field could be used to represent an annual salary when isSalary is true or an hourly rate when isSalary is false. The isSalary field could also be used within the computePay() method to determine which arithmetic to use because computing pay for salaried employees is different than for hourly employees. For example, public float computePay() { if(isSalary) { //Perform arithmetic for salaried employee } else { //Perform arithmetic for hourly employee } } Adding a field such as isSalary and trying to use one class to represent two different types of objects is not a good OOP design. You could make it work, but you are not taking advantage of the benefits of inheritance. If you use a field to determine the type of an object, your end result is a class that looks object-oriented but is really procedural. For example, an Employee object will need to check this added Boolean field just to know what type of object it is, causing the design of the program to not be focused on objects. A bigger problem arises when something needs to be changed. What happens to the isSalary field when a new type of employee needs to be added? Suppose that the company starts hiring contractors who are paid by the day. The Boolean no longer works because it can t be used to distinguish between three types. You could change the field to an int, name it employeeType, and use an enumeration like 0 for salary, 1 for hourly, and 2 for contractor. Again, you could make this work, but you have to make serious modifications to the Employee class. The computePay() method will have to entirely rewritten: public float computePay() { switch(employeeType) { case 0: continued
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

140 Chapter 6 Inheritance is (Web site traffic) arguably the single

Saturday, May 5th, 2007

140 Chapter 6 Inheritance is arguably the single most important aspect of OOP. The ability to create a new class as an extension of an existing class has many benefits, including the important concepts of polymorphsism and abstraction, discussed in Chapter 8, Polymorphism and Abstraction. Inheritance is best explained with an example. Recall the example where a program is needed to pay employees of a company every week. One obvious object in this problem domain is the employee, and it was decided that an Employee class is to be written. Consider the following Employee class, which includes attributes for the employee s name, address, SSN, number, and salary. The methods are compute- Pay() and mailCheck(). public class Employee { public String name; public String address; public int SSN; public int number; public float salary; public void mailCheck() { System.out.println( Mailing a check to + name + + address); } public float computePay() { return (float) salary/52.0; } } The design of the Employee class might seem fine initially. An employee has a name, address, and number, and we want the employee objects to compute their pay and mail a check. Keep in mind that we are using the Employee class in the context of paying employees. Is it true that every employee has a salary? What about employees who are paid by the hour, or contractors who are paid by the day, or those in other situations where an employee is not paid by an annual salary? Perhaps the Employee class needs further analysis. The first mistake in the Employee class is adding a field of type salary. Does every employee have a salary? If the answer is no, the Employee class should not have a field of type salary. What do we do instead? We need to realize that although employees are objects in our problem domain, there are actually two different types of employee objects: salaried employees and hourly employees. Therefore, we should write two classes: Salary and Hourly.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

CHAPTER 6 CHAPTER 6 Understanding Inheritance Inheritance is (Anonymous web server)

Saturday, May 5th, 2007

CHAPTER 6 CHAPTER 6 Understanding Inheritance Inheritance is one of the most important benefits of object-oriented programming. It allows a new class to be written that extends an existing class. This chapter discusses the details of understanding and implementing inheritance, including the is a relationship, the extends keyword, the Object class, method overriding, the super keyword, and how inheritance affects constructors. An Overview of Inheritance In object-oriented programming (OOP), a new class can be built upon an existing class with the new class extending the existing class and inheriting its attributes and behaviors. Extending a class is called inheritance. The existing class is referred to as the parent class, and the new class is referred to as the child class. Other common OOP terms for the parent class are base class and super class. The child class is often referred to as the derived class or subclass. These different terms for the parent and child class are used interchangeably but have the same meaning.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision ftp web hosting services

138 Chapter 5 Answers to Review Questions 1. (Web hosting domain names)

Saturday, May 5th, 2007

138 Chapter 5 Answers to Review Questions 1. return. 2. void. 3. True, although private methods can be invoked only by other methods in the class. 4. False. In fact, most methods you write will not be static. 5. False. A method signature must contain parentheses immediately following the method name. If there are no parameters, the parentheses will be empty. 6. findInRange. 7. double. 8. Three: two ints and a boolean. 9. protected. 10. Hopefully false, even though the method could be written so that it did throw an exception every time. However, the throws keyword in a method signature means that the method might throw an exception, not that it will throw an exception. 11. The this keyword. 12. True. The generated constructor is referred to as the default constructor. 13. False. It is your decision whether you want your class to have a no-argument constructor. 14. Call-by-value. 15. False. With call-by-value, methods cannot change arguments, no matter what.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Methods 137 Review Questions 1. A method uses

Friday, May 4th, 2007

Methods 137 Review Questions 1. A method uses what keyword to return a value within the body of the method? 2. A method uses what keyword to denote that it does not return a value? 3. True or False: A method can have private access. 4. True or False: All methods must be declared static. 5. True or False: The parentheses are optional in a method signature. Use the following method signature to answer the next five questions: protected double findInRange(int x, int y, boolean b) throws NotFoundException 6. What is the name of the method? 7. What is the data type of the return value? 8. How many parameters does it have? 9. What is the access specifier? 10. True or False: The method will throw a NotFoundException every time the method is invoked. 11. What keyword is used within a constructor to invoke another constructor in the same class? 12. True or False: If you write a class and do not put a constructor in it, the compiler generates a constructor for you. 13. True or False: Every class must have a no-argument constructor. 14. When arguments are copied into parameters, this is referred to as _______________________. 15. True or False: If a Java method changes a parameter, it changes the corresponding argument as well.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

My web server - 136 Chapter 5 Summary Methods must appear

Thursday, May 3rd, 2007

136 Chapter 5 Summary Methods must appear within a class in Java. A method s signature denotes the name of the method, its access specifier, its return value, any arguments that need to pass into the method, and any checked exceptions that the method may throw. Arguments are passed to methods in Java using call-by-value, meaning that a copy of the argument is passed to the method. A method can be overloaded, allowing a class to have more than one method with the same name as long as the parameter lists are different. Every class has at least one constructor, a unique type of method that is invoked when the class is instantiated. The name of a constructor must match the name of the class, and no return value is declared. If a class does not declare a constructor, the compiler adds the default constructor to the class. The default constructor has no parameters and does not do anything. A constructor can invoke another constructor in the same class using the this() syntax, which must be the first statement in the constructor.
Note: In case you are looking for affordable and reliable webhost to host and run your business application check Vision php5 hosting services