lec16 - Lecture 16 Constructors What they are What they do...

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

View Full Document Right Arrow Icon
CS125 Course Notes Lecture 16, Slide 1 Lecture 16 Constructors What they are What they do How to use them (Savitch 5.5)
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 16, Slide 2 A Constructor • A special kind of method that initializes an object. • Is called when an object is created using new .
Background image of page 2
CS125 Course Notes Lecture 16, Slide 3 Constructors continued • If we had an object of type Sample, we could create it as follows: Sample name = new Sample(); •This is the call to the constructor. •Matches class name
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 16, Slide 4 Constructors continued • Alternatively, a constructor could have parameters: Board myBoard = new Board(10,12); •This is the call to the constructor. •Matches class name
Background image of page 4
CS125 Course Notes Lecture 16, Slide 5 Defining a constructor • Syntax: public ClassName ( parameters ) { // The body goes here } • By convention, we place it before all other methods in the class.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Course Notes Lecture 16, Slide 6 Body of a constructor • Includes assignments to instance variables. • Parameters can be used in the assignment statements. • Should include any other actions to be
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/27/2009 for the course MATH 135 taught by Professor Andrewchilds during the Fall '08 term at Waterloo.

Page1 / 19

lec16 - Lecture 16 Constructors What they are What they do...

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

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