1 / 17

Notes from Week 5

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).

cherndon
Download Presentation

Notes from Week 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006

  2. 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.

  3. 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.

  4. 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.

  5. 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

  6. 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.

  7. 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.

  8. 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?)

  9. Strings • List of Characters • Characters are any single letter, digit, or symbol • In code: ”Strings are surrounded by quotes”

  10. Printing information to the console (screen) System.out.println(”Some text.”);

  11. Inheritance • Can be viewed as a specialization of a superclass. • Can be viewed as generalizing several subclasses into a superclass.

  12. Generalization Relationship • “is a” relationship • There is a superclass and a subclass • In code: public class ClassB extends ClassA { }

  13. 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.

  14. 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.

  15. 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!

  16. Accessors (get methods) • Access the value of an instance variable. public TypeOfProperty getProperty(){ return _instanceVariableName; }

  17. Mutators (set methods) • Change the value of an instance variable. public void setProperty (TypeOfProperty newValue) { _instanceVariableName = newValue; }

More Related