07-objects2

07-objects2 - COMP 202 COMP 202 Introduction to Computing 1...

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

View Full Document Right Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 1 COMP 202 Building Your Own Classes CONTENTS : • Anatomy of a class • Constructors and Methods (parameter passing) • Instance Data
Background image of page 1

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 2 COMP 202 • We've been using predefined classes. Now we will learn to write our own classes to define new objects. • This week we focus on: – Objects: attributes, state and behaviour – Anatomy of a Class: attributes and methods – Classes as Types – Creating new objects – Parameter passing
Background image of page 2
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 3 Part 1 About Objects
Background image of page 3

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 4 Objects • An object has: state - descriptive characteristics methods - what it can do (or what can be done to it) » services, actions, behavior, …. • For example, consider a bank client with a checking and a savings account. • The state of the client is the balance of the checking and saving accounts. • Methods are withdrawal, deposit and transfer, querying the balance etc. • Some methods might change the state
Background image of page 4
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 5 Classes A class is a blueprint of an object It is the model or pattern from which objects are created For example, the String class is used to define String objects: String x = “Bob”; Each String object contains specific characters (its state) Each String object has methods such as toUpperCase: x = x.toUpperCase(); In the case of String , the methods don’t change the object itself; but this is very specific to strings Class Object Variable State Object
Background image of page 5

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 6 Classes • The String class was provided for us by the Java standard class library • But we can also write our own classes that define specific objects that we need • For example, suppose we wanted to write a bank program that manages the clients and their saving and checking accounts. • We could write a Client class to represent client objects with the two associated accounts.
Background image of page 6
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 7 Part 2 The Anatomy of a Class
Background image of page 7

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 8 The Anatomy • A class can be considered to be a cardboard box containing items (called members in Java): – Constants – Variables – Methods • constructor methods (that help creating an object of the class) • other useful methods (withdraw, transfer) • possibly a main method • Each item (data and method) in the box can be accessed and modified by using the DOT operator
Background image of page 8
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 9 Classes A class contains data declarations and method declarations (collectively called members of the class) int x, y; char ch; DaTa±²e³laRaT´µNs ¶eThµ²±²e³laRaT´µNs Our cardboard box ~ the class
Background image of page 9

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

View Full DocumentRight Arrow Icon
COMP 202 – Introduction to Computing 1 COMP 202 - Objects 2 10 The Idea Behind A Class • A class builds objects
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 63

07-objects2 - COMP 202 COMP 202 Introduction to Computing 1...

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

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