Object Oriented Programming Review

Slides:



Advertisements
Similar presentations
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Advertisements

Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Chapter 4: Writing Classes
Problem Solving #1 ICS Outline Review of Key Topics Review of Key Topics Example Program Example Program –Problem 7.1 Problem Solving Tips Problem.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
Enhancing classes Visibility modifiers and encapsulation revisited
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
Aalborg Media Lab 28-Jun-15 Software Design Lecture 7 “Object Oriented Design”
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Static Class Members Wrapper Classes Autoboxing Unboxing.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Introduction to UML Lian Yu Department of Computer Science and Engineering Arizona State University Tempe, AZ
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Object Oriented Design and UML
Writing Classes (Chapter 4)
The this Reference The this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Inheritance Reading for this Lecture: L&L 6.4 – 6.5.
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 7 Object-Oriented Design Concepts
6. Object-Oriented Design Based on Java Software Development, 5 th Ed. By Lewis &Loftus.
1 Object Oriented Design and UML Class Relationships –Dependency –Aggregation –Interfaces –Inheritance Interfaces Reading for this Lecture: L&L 6.4 – 6.5.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Static class members.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
SEEM Java – Basic Introduction, Classes and Objects.
Chapter 8 Inheritance 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Outline Software Development Activities Identifying Classes and Objects Static Variables and Methods Class Relationships Interfaces Enumerated Types Revisited.
1 Chapter 4: Writing Classes  Chapter 4 focuses on: class definitions encapsulation and Java modifiers method declaration, invocation, and parameter passing.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
University of Turkish Aeronautical Association Computer Engineering Department CENG 112 COMPUTER PROGRAMMING 2 Tansel Dökeroglu, Ph.D.
Chapter 5: Enhancing Classes
Classes and Objects.
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Lecture on Design Phase and UML Class Diagrams
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Passing Objects to Methods
Object Oriented Programming
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Object Oriented Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Chapter 4: Writing classes
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.
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 5: Enhancing Classes
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Packages & Random and Math Classes
Outline Creating Objects The String Class The Random and Math Classes
Object-Oriented Design Part 2
Classes and Objects Object Creation
Chapter 4: Writing Classes
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

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

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

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

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

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

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

Reference Object vs Reference 4/26/2018

Memory allocation for Objects 4/26/2018

Object vs Reference 4/26/2018

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

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

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

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

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

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

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

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

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

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

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

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

Association:: Aggregation and Composition 4/26/2018

Association:: Aggregation and Composition 4/26/2018

Object vs Reference 4/26/2018

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

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

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

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

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

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

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