Methods & Classes II Flashcards
What are advantages of using classes?
Links Data with code manipulation
Controls access
Three Access Modifiers & Default Access
Public
Protected - only applicable to inheritance
Private - accessible only by members of its class
Default access is public
What standard methods control access to members?
Get()
Put()
Can you pass objects to methods?
Yes, it’s common
Call by value
Copies the value of an argument into a parameter
Primitive are passed by value
Call by Reference
The reference to an argument is passed to the parameter.
Objects are passed by reference
Will changes to an object inside a method affect the original object?
Yes
Method Overloading
When two or more methods within the same class share the same name with different parameters.
E.g. Constructors
How is the correct method chosen if it is overloaded?
By matching arguments with parameters.
Recursion
A method calling itself
E.g. A factorial
What happens during recursion?
New local variables & parameters are allocated storage on the stack & the method is executed with these new variables.
Does a recursive call make a new copy of the method?
No, just the arguments are new.
What happens when there are too many recursive calls to a method?
A stack overrun
Why do recursion rather than iteration?
Some types of algorithms can be more clear & simple
How do you end the loop of recursive calls within a method?
You must include a conditional statement to force the method to return without recursion.