Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming Review

Similar presentations


Presentation on theme: "Object Oriented Programming Review"— Presentation transcript:

1 Object Oriented Programming Review
Lecture 5 Object Oriented Programming Review CSE 1322 4/26/2018

2 Topics Identifying Classes & Objects Static Members
Class Relationships “this” Parameters Revisited Overloading Methods Operators Testing 4/26/2018

3 Identifying Classes & Objects
Identify potential classes within the specification Nouns 4/26/2018

4 What Goes in the Class How do you decide what should be in the class
Data Methods Not easy, but must be done 4/26/2018

5 A Block diagram of a class (Java style)
[access_modifier] [static] [final] type name [= initial value]; 4/26/2018

6 Object declaration and Initialization
class Rectangle { double length; double breadth; } 4/26/2018

7 Reference Object vs Reference 4/26/2018

8 Memory allocation for Objects
4/26/2018

9 Object vs Reference 4/26/2018

10 Objects Communicate with each other through Message Passing
4/26/2018

11 Static Members Static methods can be invoked via the class name
Static variables are stored at the class level (one copy for all instances) 4/26/2018

12 The static Modifier Remember that static methods (also called class methods) that can be invoked through the class name rather than through a particular object For example, the methods of the Math class are static: Math.sqrt (25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object 4/26/2018

13 Static Variables Static variables are also called class variables
Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static int count; Memory space for a static variable is created when the class in which it is declared is loaded All objects created from the class share static variables Changing the value of a static variable in one object changes it for all others Local variables cannot be static 4/26/2018

14 Static Methods class Helper public static int triple (int num) {
int result; result = num * 3; return result; } class Helper Because it is static, the method can be invoked as: value = Helper.triple (5); 4/26/2018

15 Static Methods Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, a static method can reference static variables or local variables 4/26/2018

16 Class Relationships Classes can have various relationships to one another. Most common are: Dependency (“uses”) Aggregation (“has a”) Inheritance (“is a”) 4/26/2018

17 Object Relationships Objects can have various types of relationships to each other A general association, as we've seen in UML diagrams, 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 4/26/2018

18 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? 4/26/2018

19 Aggregation One class is “made up” of other classes
“has a” relationship Gameboard has a marble Deck has a card 4/26/2018

20 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 4/26/2018

21 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 4/26/2018

22 Association:: Aggregation and Composition
4/26/2018

23 Association:: Aggregation and Composition
4/26/2018

24 Object vs Reference 4/26/2018

25 The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference is used in a method called tryMe If tryMe is invoked as follows, the this reference refers to obj1: obj1.tryMe(); But in this case, the this reference refers to obj2: obj2.tryMe(); 4/26/2018

26 The this reference The this reference can also be used to distinguish the parameters of a constructor from the corresponding instance variables with the same names public Account (String name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; } 4/26/2018

27 The this reference class Rectangle { int length; int breadth; }
4/26/2018

28 The this keyword does not refer to local variable
4/26/2018

29 Assignment Revisited Before 5 12 After 5
The act of assignment takes a copy of a value and stores it in a variable For primitive types: num2 = num1; Before num1 5 num2 12 After num1 5 num2 4/26/2018

30 Reference Assignment For object references, assignment copies the memory location: bishop2 = bishop1; Before bishop1 bishop2 After bishop1 bishop2 4/26/2018

31 Aliases Two or more references that refer to the same object are called aliases of each other One object (and its data) can be accessed using different reference variables Aliases can be useful, but should be managed carefully Changing the object’s state (its variables) through one reference changes it for all of its aliases 4/26/2018


Download ppt "Object Oriented Programming Review"

Similar presentations


Ads by Google