Chapter 6 Overloading Methods and Constructors o o

  • Slides: 11
Download presentation
Chapter 6

Chapter 6

Overloading Methods and Constructors o o o Two or more methods in a class

Overloading Methods and Constructors o o o Two or more methods in a class may have the same name as long as their parameter lists are different. When this occurs, it is called method overloading. This also applies to constructors. Method overloading is important because sometimes you need several different ways to perform the same operation.

Overloaded Method add public int add(int num 1, int num 2) { int sum

Overloaded Method add public int add(int num 1, int num 2) { int sum = num 1 + num 2; return sum; } public String add (String str 1, String str 2) { String combined = str 1 + str 2; return combined; }

Method Signature and Binding o A method signature consists of the method’s name and

Method Signature and Binding o A method signature consists of the method’s name and the data types of the method’s parameters, in the order that they appear. The return type is not part of the Signatures of the signature. add(int, int) add(String, String) o methods of previous slide add The process of matching a method call with the correct method is known as binding. The compilier uses the method signature to determine which version of the overloaded method to bind the call to.

Rectangle Class Constructor Overload If we were to add the no-args constructor we wrote

Rectangle Class Constructor Overload If we were to add the no-args constructor we wrote previously to our Rectangle class in addition to the original constructor we wrote, what would happen when we execute the following calls? Rectangle box 1 = new Rectangle(); Rectangle box 2 = new Rectangle(5. 0, 10. 0);

Rectangle Class Constructor Overload If we were to add the no-args constructor we wrote

Rectangle Class Constructor Overload If we were to add the no-args constructor we wrote previously to our Rectangle class in addition to the original constructor we wrote, what would happen when we execute the following calls? Rectangle box 1 = new Rectangle(); Rectangle box 2 = new Rectangle(5. 0, 10. 0); The first call would use the no-args constructor and box 1 would have a length of 1. 0 and width of 1. 0. The second call would use the original constructor and box 2 would have a length of 5. 0 and a width of 10. 0.

The Bank. Account Example Bank. Account -balance: double +Bank. Account(): Overloaded Constructors +Bank. Account(start.

The Bank. Account Example Bank. Account -balance: double +Bank. Account(): Overloaded Constructors +Bank. Account(start. Balance: double): +Bank. Account(str. String): Overloaded deposit methods Overloaded withdraw methods Overloaded set. Balance methods +deposit(amount: double): void +deposit(str: String): void +withdraw(amount: double): void +withdraw(str: String): void +set. Balance(b: double): void +set. Balance(str: String): void +get. Balance(): double

Bank. Account. java public class Bank. Account { private double balance; // Account balance

Bank. Account. java public class Bank. Account { private double balance; // Account balance public Bank. Account() { balance = 0. 0; } public Bank. Account(double start. Balance) { balance = start. Balance; } public Bank. Account(String str) { balance = Double. parse. Double(str); } public void deposit(double amount) { balance += amount; } public void deposit(String str) { balance += Double. parse. Double(str); }

Bank. Account. java o o o o public void withdraw(double amount) { balance -=

Bank. Account. java o o o o public void withdraw(double amount) { balance -= amount; } public void withdraw(String str) { balance -= Double. parse. Double(str); } public void set. Balance(double b) { balance = b; } public void set. Balance(String str) { balance = Double. parse. Double(str); } public double get. Balance() { return balance; } }

Account. Test. java import javax. swing. JOption. Pane; // For the JOption. Pane class

Account. Test. java import javax. swing. JOption. Pane; // For the JOption. Pane class import java. text. Decimal. Format; // For the Decimal. Format class public class Account. Test { public static void main(String[] args) { String input; // To hold user input Decimal. Format dollar = new Decimal. Format("#, ###. 00"); input = JOption. Pane. show. Input. Dialog("What is your " + "account's starting balance? "); Bank. Account account = new Bank. Account(input); input = JOption. Pane. show. Input. Dialog("How much were " + "you paid this month? "); account. deposit(input);

Account. Test. java JOption. Pane. show. Message. Dialog(null, "Your pay has been deposited. n"

Account. Test. java JOption. Pane. show. Message. Dialog(null, "Your pay has been deposited. n" + "Your current balance is $ " + dollar. format(account. get. Balance())); input = JOption. Pane. show. Input. Dialog("How much would " + "you like to withdraw? "); account. withdraw(input); JOption. Pane. show. Message. Dialog(null, "Now your balance is $" + dollar. format(account. get. Balance())); } } System. exit(0);