Chapter5 Notes

Chapter5 Notes - 1 1 Chapter 5 – Writing Classes We've...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 1 Chapter 5 – Writing Classes ¡ We've been using predefined classes. Now we will learn to write our own classes to define objects ¡ Chapter 5 focuses on ¢ class definitions and relationships ¢ instance data ¢ encapsulation and Java modifiers ¢ method declaration and parameter passing ¢ constructors ¢ method design and overloading Outline • Classes and Objects Revisited • Anatomy of a Class • Encapsulation • Anatomy of a Method • Static Class Members • Class Relationships • Method Design • Method Overloading 2 5.1 – Classes and Object Revisited ¡ The programs we’ve written in previous examples have used classes defined in the Java standard class library ¡ Now we will begin to design programs that rely on classes that we write ourselves ¡ The class that contains the main method is just the starting point of a program ¡ True object-oriented programming is based on defining classes that represent objects with well- defined characteristics and functionality 5.1 – Identifying Classes and Objects ¡ Remember that a class represents a group (classification) of objects with the same behaviors ¡ Generally, classes that represent objects should be given names that are singular nouns ¡ Examples: Coin , Student , Message ¡ A class represents the concept of one such object ¡ We are free to instantiate as many of each object as needed 3 5.1 – Identifying Classes and Objects ¡ Sometimes it is challenging to decide whether something should be represented as a class ¡ For example, should an employee's address be represented as a set of instance variables or as an Address object ¡ The more you examine the problem and its details the more clear these issues become ¡ When a class becomes too complex, it often should be decomposed into multiple smaller classes to distribute the responsibilities 5.1 – Identifying Classes and Objects ¡ We want to define classes with the proper amount of detail ¡ For example, it may be unnecessary to create separate classes for each type of appliance in a house ¡ It may be sufficient to define a more general Appliance class with appropriate instance data ¡ It all depends on the details of the problem being solved 4 5.1 – Identifying Classes and Objects ¡ Part of identifying the classes we need is the process of assigning responsibilities to each class ¡ Every activity that a program must accomplish must be represented by one or more methods in one or more classes ¡ We generally use verbs for the names of methods ¡ In early stages it is not necessary to determine every method of every class – begin with primary responsibilities and evolve the design 5.1 – Identifying Classes and Objects ¡ Recall from our overview of objects in Chapter 1 that an object has state and behavior ¡ Consider a university student object ¢ it’s state can be defined as the student’s name, address, major, and GPA ¢ it’s primary behavior may be to update the address or to recalculate the GPA ¡...
View Full Document

This note was uploaded on 03/31/2008 for the course IST 240 taught by Professor Tracymullen during the Spring '08 term at Penn State.

Page1 / 61

Chapter5 Notes - 1 1 Chapter 5 – Writing Classes We've...

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

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