DOC PREVIEW
UMD CMSC 131 - Lecture 16: Method Overloading

This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 16 Method Overloading Last time 1 APIs comments and documentation Today 1 Project 3 due Sunday 2 Exam 1 coming 10 11 3 Method overloading 10 6 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 3 Due Monday It is due Monday 10 9 at 11 pm Try to turn it in earlier today if possible The project is open Start now Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly as much of grading is automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Exam 1 Is 10 11 in Discussion Section Take the test in your own section Test will cover material since beginning of semester Test will be closed book closed neighbor Start studying now CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 2 Why Is It Called The Stack Stack is part of main memory used to hold variables and their values When a variable is referenced its value is found by looking in stack When methods are invoked temporary additions stack frames are made to stack before body of method is executed So why is it called a stack CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Stacks in Computer Science A stack is a data structure device for holding values Three operations on a stack push add a new value into the stack pop remove the most recently added value still in stack top return the most recently added value in stack Think stack of plates in a restaurant push put new plate on top pop remove top plate top look at top plate CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Example S push 3 S push 4 S top 4 S pop S push 5 S top 5 5 4 3 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland S 5 What Do Stacks Have To Do with The Stack Main memory stack is short for call stack call stack information for processing method calls Contents of call stack stack frames At beginning of method call new stack frame pushed onto call stack After body of method has finished executing stack is popped CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Example public class C int f int i return i 1 Stack stack frame stack frame Heap i 1 i 2 int g int i return 1 f i 1 What is printed by System out println C g 1 4 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 Local Variables and the Stack Local variables are treated like method arguments When declared they are added to current stack frame When stack frame is popped those local variables disappear Whenever a new block is introduced so is a new stack frame int x 3 This introduces a new stack frame associating x with 3 When the block is finished the stack frame goes away CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Initialization of Instance Variables Instance variables are assigned values even if none explicitly provided Primitives are set to 0 false References are set to null i e point to nothing Consider public class C public int x C cObj new C System out println cObj x 0 is printed Don t rely on this in your programs Compliers famously get this wrong Better practice Set default values when variable is declared in class ublic tnt x 0 In constructors supply alternative values as desired CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Initialization of Local Variables Local variables are not initialized Example public void f int x int z x 3 won t compile What about static variables Java checks if variables are uninitialized If use of uninitialized variable is possible the Java compiler and Eclipse also complain They are treated like instance variables class C public static int X Expression C X evaluates to 0 Good practice always initialize variables explicitly Avoids compiler errors local variables You don t have to remember different treatments of local instance variables You don t run the risk of a misbehaving compiler CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Method Overloading Overloading declaration of two methods in the same class with same name Java allows overloading The different methods must have different argument lists e g public class C int f int f int f OK Why use overloading To allow similar operations to have the same name We have seen overloading before with constructors CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Example Recall Date class print method The method for converting dates to strings public String toString return month separator day separator year Method for printing dates to System out public void print System out print toString May want another print method that allows specification of surrounding text public void print String pre String post System out print pre print System out print post Java allows this What is output of Date d new Date d print The date is CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 When Is Overloading Allowed Terminology Prototype I O types behavior public static etc for method public static void f int x float y Signature Input types for method f int x float y You can only overload a methods if the signatures are different void f int int f float fine Just having different return types is not enough void f int int f int not fine Why Because compiler can t always tell which f to call just based on return type f 3 Which f should be run CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 13


View Full Document

UMD CMSC 131 - Lecture 16: Method Overloading

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 16: Method Overloading
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 16: Method Overloading and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 16: Method Overloading and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?