Chapter-4

Chapter-4 - Every instance of a particular class has same...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 1 Every instance of a particular class has same methods, but the methods can behave differently based on values of the instance variables Song s1 = new Song(); s1.setTitle(“Sing”); Song s2 = new Song(); s2.setTitle(“Darkstar”); s1.play(); s2.play(); Song title setTitle() play() void play() {System.out.println(title);}
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 2 Method Declarations A method declaration specifies the code that will be executed when the method is invoked When a method is invoked, the flow of control jumps to the method and executes its code When complete, the flow returns to the place where the method was called and continues The invocation may or may not return a value, depending on how the method is defined
Background image of page 2
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 3 What happens when a method is invoked ? If the called method is in the same class , only the method name is needed m2(); m2 m1 Foo class
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 4 m1 m2 m2(); Foo class What happens when a method is invoked ? The called method is often part of another class Foo f = new Foo(); f.m1(); main FooTest class
Background image of page 4
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 5 Method Declarations A method declaration starts with a method header void setRadius( int r ) method name return type parameter list The parameter list specifies the type and name of each parameter The name of a parameter in the method declaration is called a  formal parameter
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 6 Method Declarations void setRadius( double r ) { radius = r; } The return expression must be consistent with the return type The method header is followed by the method body double getRadius() { return radius; }
Background image of page 6
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University 7 The return statement The return type of a method indicates the type of value that the method sends back to the calling location A method that does not return a value has a void return type A return statement specifies the value that will be returned return < expression> ; Its expression must conform to the return type
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COE318 Fall 2008, Olivia Das, Elec. and Comp. Engg., Ryerson University
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/12/2010 for the course COE 318 taught by Professor Ken during the Spring '08 term at Ryerson.

Page1 / 29

Chapter-4 - Every instance of a particular class has same...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online