ch7DesignClasses

# ch7DesignClasses - Chapter 7 Designing Classes A class...

This preview shows pages 1–14. Sign up to view the full content.

Chapter 7 Designing Classes

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

View Full Document
Choosing Classes A class represents a single concept Concepts from mathematics: Point Rectangle Ellipse Concepts from real life BankAccount Purse Actors (end in -er, -or) Random (better called RandomNumberGenerator) Utility (helper)classes--no objects, only static methods Math All nouns Does some kind of work for you
Cohesion Cohesive = public interface closely related to the single concept that the class represents This class lacks cohesion: public class Purse { public Purse(){. ..} public void addNickels(int count){. ..} public void addDimes(int count){. ..} public void addQuarters(int count){. ..} public double getTotal(){. ..} public static final double NICKEL_VALUE =0.05; public static final double DIME_VALUE =0.1; public static final double QUARTER_VALUE =0.25; . .. }

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

View Full Document
Cohesion Two concepts: purse and coin Solution: Make two classes: public class Coin { public Coin(double aValue,String aName){. ..} public double getValue(){. ..} } public class Purse { public Purse(){. ..} public void add(Coin aCoin){. ..} public double getTotal(){. ..} } Purse Coin
Coupling A class depends on another if it calls one of its methods Purse depends on Coin because it calls getValue on coins Coin does not depend on Purse High Coupling = many class dependencies Minimize coupling to minimize the impact of interface changes

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

View Full Document
Dependency Relationship between Purse and Coin Classes
High and Low Coupling between Classes

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

View Full Document
Review of a class-setup
Encapsulation Hiding the implementation details of a class (making all fields and helper methods private ) is called encapsulation . Encapsulation helps in program maintenance and team development. A class encapsulates a small set of well-defined tasks that objects of a class can perform.

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

View Full Document
public vs. private Public constructors and methods of a class constitute its interface with classes that use it — its clients . All fields are usually declared private — they are hidden from clients. Static constants occasionally may be public . “Helper” methods that are needed only inside the class are declared private .
public vs. private (cont’d) public class MyClass { // Constructors: public MyClass (. ..) { . .. } ... // Public methods: public < sometype > myMethod (. ..) { . .. } ... // Private methods: private < sometype > myMethod (. ..) { . .. } ... // Private fields: private < sometype > myField; ... }

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

View Full Document
Constructors A constructor is like a method for creating objects of the class. A constructor often initializes an object’s fields. Constructors do not have a return type (not even void ) and they do not return a value. All constructors in a class have the same name — the name of the class. Constructors may take arguments.
Constructors (cont’d) If a class has more than one constructor, they are “overloaded” and must have different numbers and/or types of arguments.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 60

ch7DesignClasses - Chapter 7 Designing Classes A class...

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

View Full Document
Ask a homework question - tutors are online