Tham khảo tài liệu 'java for webobjects developers-p4', công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Java for WebObjects Developers-P4 Overloading methods same name different types Overloading multiple versions of a method with different arguments Each is distinct due to unique number and or type of arguments double balance double balance double discount double balance BigDecimal discount double balance double discount NSArray coupons You cannot change the return value type double balance int balance will not compile Overloading methods same name different types Java supports method overloading. Overloading means defining multiple versions of a given method each with a different and distinct argument list. Notice that the return type must stay the same. Conceptually each method version should produce analogous behavior. Overloading is useful when the same action can be performed based on different sets of parameters. Consider the shopping cart example. You can calculate the balance in different ways. You can calculate the simple balance of all items in the cart. You can specify a discount rate as an argument. Sometimes the discount rate is a primitive type other times it is a number object. Occasionally the customer has an additional set of coupons. As a class designer you can implement multiple versions of the balance method each taking a different set of arguments. This is called overloading the balance method. It creates a much more flexible class design that is potentially reusable in multiple scenarios. Remember overloaded methods have the same name but differ by the number and type of arguments. You cannot vary the return type. 72 Chapter 3 Java for WebObjects Developers Overriding inherited methods Overriding replacing the implementation of an inherited method Re-implement the method using the same name type and arguments You have two choices Replace the superclass behavior Extend the superclass behavior To extend the behavior include a call to the superclass method Invoke the superclass method using the keyword super Overriding inherited methods When a .