Notes from Week 5Writing Methods is a two-step processWriting the method (defining the method)Method Writing - Method SignatureSlide 5Slide 6Method Writing - Method BodyMethod CallingStringsPrinting information to the console (screen)InheritanceGeneralization RelationshipInheritance BasicsAccessors & MutatorsMethod OverloadingAccessors (get methods)Mutators (set methods)Notes from Week 5CSE 115 Spring 2006February 13, 15 & 17, 2006Writing Methods is a two-step processGive 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:VisibilityReturn typeName for methodParameter listSpecify Method Body:Code that method must execute to perform required task.Method Writing - Method SignatureVisibility public is best if you want anyone to be able to call your methods.Return typevoid if nothing is returned.If something is returned, need to use keyword return in method body to indicate value being returned.Method Writing - Method SignatureGive the method a name (mind the rules)Method is an identifierStyle: first letter lower case, each subsequent word capitalized.Eg) myFirstMethodMethod Writing - Method SignatureParameters (formal parameters)Specify for eachTypeNameWhy do parameters get a name in the method definition?So we can refer to them inside the method body.Method Writing - Method BodyCode 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 CallingMethod 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?)StringsList of CharactersCharacters are any single letter, digit, or symbolIn code:”Strings are surrounded by quotes”Printing information to the console (screen)System.out.println(”Some text.”);InheritanceCan be viewed as a specialization of a superclass.Can be viewed as generalizing several subclasses into a superclass.Generalization Relationship“is a” relationshipThere is a superclass and a subclassIn code:public class ClassB extends ClassA {}Inheritance BasicsWhen 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 & MutatorsMethods 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 OverloadingTwo 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 =
View Full Document