Object Oriented Programming Lecture 12 Object Oriented Programming Richard Gesick
Topics Identifying Classes & Objects Class Relationships “this” Parameters Overloading Methods Operators
Identifying Classes & Objects Identify potential classes within the specification Nouns
What Goes in the Class How do you decide what should be in the class Data Methods
Class Relationships Classes can have various relationships to one another. Most common are: Dependency (“uses”) Aggregation (“has a”) Inheritance (“is a”)
Object Relationships Objects can have various types of relationships to each other A general association is sometimes referred to as a use relationship A general association indicates that one object (or class) uses or refers to another object (or class) in some way We could even annotate an association line in a UML diagram to indicate the nature of the relationship Author Book writes
Dependency One class dependent (uses) another class Game uses ball, paddle Ship uses bullet Sometimes, a class depends on another instance of itself Is one date equal to another date? Is one picture equal to another picture?
Aggregation One class is “made up” of other classes “has a” relationship Gameboard has a marble Deck has a card
Aggregation An aggregate object is an object that contains references to other objects For example, an Account object contains a reference to a String object (the owner's name) An aggregate object represents a has-a relationship A bank account has a name Likewise, a student may have one or more addresses
Aggregation in UML An aggregation association is shown in a UML class diagram using an open diamond at the aggregate end StudentBody + Main (args : String[]) : void + ToString() : String 1 2 Student - firstName : String - lastName : String - homeAddress : Address - schoolAddress : Address - streetAddress : String - city : String - state : String - zipCode : long Address
UML Diagrams classes are represented with boxes that contain three compartments: The top compartment contains the name of the class. The middle compartment contains the attributes of the class. The bottom compartment contains the methods and properties
UML Diagrams Player - name:string -money:float -health: int +Player(): +Buy(float):void +GetName():string +Name:string {property} +Money:float {property} +ToString():string