Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 4 Recap CSE 115 Spring 2007. Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the.

Similar presentations


Presentation on theme: "Week 4 Recap CSE 115 Spring 2007. Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the."— Presentation transcript:

1 Week 4 Recap CSE 115 Spring 2007

2 Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the parameter followed by a name (type1 name1, type2 name2, type3 name3…)

3 Calling methods To get the actions of a method to be performed, we need to call (or invoke) the methods Methods are called on objects  How do we get an object?

4 Method calls object.methodToBeCalled() Recall the dot (.) operator [or member access operator] Notice the () here – this is an actual parameter list. If the method needs a parameter, we need to give an actual value to the method when we call it.

5 Important Note Two methods in Java can not have the exact same method header.

6 Implementing an Interface A class realizes an interface using the keyword implements. The formal name for this relationship is realization and the informal name is “implements” or “implements an interface” Inside the code, the class must create methods with the same signatures as those in the interface and give each method a method body.

7 Java Code: Implementing an Interface public class name implements InterfaceName { public void method1() { } public Object method2() { return null; } public void method3(Object obj) { } //Note that each method must have a method body, //but that method body could be empty. }

8 “Naming” our instances Sometimes we need a way to refer to an instance after we have created it. We can use a variable (reference) to communicate with an object.

9 Creating an instance variable Instance variables are the way we represent properties in our classes. Instance variables are only accessible inside the class body because we declare them to be private. Syntax for declaring an instance variable: private type identifier

10 Syntax of an instance variable (continued) Type: We must tell what type of thing the variable will refer to. Remember that our types are names of classes or interfaces. Identifier: Instance variable names follow the style conventions of a method, but are preceded by an underscore.

11 Assignment to a variable The = is the assignment operator It is right associative, that means the value on the right is assigned to the variable on the left. _variableName = new ObjectCreated();

12 Composition Relationship We have actually created the composition relationship. More on this relationship to come.


Download ppt "Week 4 Recap CSE 115 Spring 2007. Formal Parameter Lists Comma-separated list of formal parameters Formal parameters are listed giving the type of the."

Similar presentations


Ads by Google