Method Overloading - IT1908 PDF
Document Details
Uploaded by BrainyDahlia5055
STI
Tags
Summary
This document covers method overloading in Java, including explanations and examples of how to use it. It also details topics like argument passing, and autoboxing concepts. The concepts are relevant to computer science and programming.
Full Transcript
IT1908 METHOD OVERLOADING Overloaded methods are differentiated by the number and the...
IT1908 METHOD OVERLOADING Overloaded methods are differentiated by the number and the type of arguments passed into the method. Passing Information to a Method You cannot declare more than one (1) method with the same Parameters are the variables listed in a method declaration. name and the same number and type of arguments. Ex. public double computeSalePrice(double You cannot declare two (2) methods with the same signature origPrice, double discountRate) { } even if they have different return types. Arguments are the actual values that are passed in when the Ex. method is invoked. public void draw(int i) { } Ex. computeSalePrice(37.50, 0.15) public int draw(int i) { } //does not compile Java is a pass-by-value language. This means that a copy of the variable is made, and the method receives that copy. Autoboxing Ex. main() method calls newNumber() method Autoboxing is the automatic conversion that the Java public static void main(String[] args) { compiler makes between the primitive types and their int num = 1; corresponding object wrapper classes. newNumber(2); Ex. int to Integer, double to Double System.out.println(num); When the primitive type version is not present, Java performs } autoboxing. public static void newNumber(int num) { public void draw(Integer i) { num = 3; System.out.print(“Integer”); } } Output: 1 Calling statement: draw(10); Explanation: The variable num in main() does not change Output: Integer even after calling it because no assignments are made to it. When the primitive type version is present, Java does not Overloading Methods need to perform autoboxing. Ex. A method signature consists of the method’s name and the public void draw(Integer i) { parameter types. System.out.print(“Integer”); Ex. The method signature of computeSalePrice(double } origPrice, double discountRate) is public void draw(int i) { computeSalePrice(double, double) System.out.print(“int”); Method overloading occurs when there are different method } signatures with the same name but different type parameters. Calling statement: draw(10); Ex. Output: int public void draw(String s) { } public void draw(int i) { } References: public void draw(int i, double f) { } Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning Java programming: The Thus, you can create statements like the following: object-oriented approach. Indiana: John Wiley & Sons, Inc. draw(“Circle”); Oracle Docs (n.d.). Citing sources. Retrieved from https://docs.oracle.com/javase/tutorial/java/javaOO/index.html draw(10); draw(10, 5.0); 03 Handout 1 *Property of STI [email protected] Page 1 of 1