170 likes | 179 Views
Notes from Week 5. CSE 115 Spring 2006 February 13, 15 & 17, 2006. Writing Methods is a two-step process. Give definition of how method will perform – write the method. When we want the work of the method to actually get done – the method call. Writing the method (defining the method).
E N D
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006
Writing Methods is a two-step process • Give definition of how method will perform – write the method. • When we want the work of the method to actually get done – the method call.
Writing the method (defining the method) • Specify Method Signature: • Visibility • Return type • Name for method • Parameter list • Specify Method Body: • Code that method must execute to perform required task.
Method Writing - Method Signature • Visibility • public is best if you want anyone to be able to call your methods. • Return type • void if nothing is returned. • If something is returned, need to use keyword return in method body to indicate value being returned.
Method Writing - Method Signature • Give the method a name (mind the rules) • Method is an identifier • Style: first letter lower case, each subsequent word capitalized. • Eg) myFirstMethod
Method Writing - Method Signature • Parameters (formal parameters) • Specify for each • Type • Name • Why do parameters get a name in the method definition? • So we can refer to them inside the method body.
Method Writing - Method Body • Code that is executed when method is called. • Need additional variables inside the method? • Create a local variable. • Local variables are only accessible from inside the method body.
Method Calling • Method call: • instanceName.methodName(); • Pass in actual values for each of the required parameters. (actual parameters) • Do something with the return type if there is one. (What if you don’t?)
Strings • List of Characters • Characters are any single letter, digit, or symbol • In code: ”Strings are surrounded by quotes”
Printing information to the console (screen) System.out.println(”Some text.”);
Inheritance • Can be viewed as a specialization of a superclass. • Can be viewed as generalizing several subclasses into a superclass.
Generalization Relationship • “is a” relationship • There is a superclass and a subclass • In code: public class ClassB extends ClassA { }
Inheritance Basics • When a subclass extends a superclass, the subclass inherits all public capabilities (methods) of the superclass and they remain public in the subclass. • When a class has a superclass and we create an instance of the subclass, the no-argument constructor of the superclass is called automatically from the constructor of the subclass.
Accessors & Mutators • Methods inside of a class that work with the instance variables to provide the value of the instance variable and to change the value of the instance variable respectfully.
Method Overloading • Two methods can have the same name only if they differ in number and/or type of parameters. • Constructors can be overloaded too!
Accessors (get methods) • Access the value of an instance variable. public TypeOfProperty getProperty(){ return _instanceVariableName; }
Mutators (set methods) • Change the value of an instance variable. public void setProperty (TypeOfProperty newValue) { _instanceVariableName = newValue; }