More Object Oriented Programming

Slides:



Advertisements
Similar presentations
EC-241 Object-Oriented Programming
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Unit 4II 1 More about classes H Defining classes revisited H Constructors H Defining methods and passing parameters H Visibility modifiers and encapsulation.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
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
1 Memory Model of A Program, Methods Overview l Closer Look at Methods l Memory Model of JVM »Method Area »Heap »Stack l Preview: Parameter Passing.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
1 Lecture 4 Objects and Classes Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 5: Enhancing Classes
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© 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
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
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.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
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.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
C# Programming Methods.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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.
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Object Oriented Programming
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Lecture 11 C Parameters Richard Gesick.
CSI 1102 Introduction to Software Design
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.
Variables and Their scope
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 5: Enhancing Classes
CSCI 3328 Object Oriented Programming in C# Chapter 6: Methods
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
Object Oriented Programming Review
Java Programming Language
Lecture 11 Parameters CSE /26/2018.
Object-Oriented Design Part 2
Parameters, Overloading Methods, and Random Garbage
Corresponds with Chapter 5
Object Oriented Programming (OOP) Lecture No. 12
Previous Lecture: Today’s Lecture: Reading (JV):
Presentation transcript:

More Object Oriented Programming Lecture 15 More Object Oriented Programming Richard Gesick

Topics Static members “this” Parameters by value, ref, out

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

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

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);

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

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();

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; }

Assignment Revisited 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

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

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

Parameters There are three main types of parameters: Value - passes a copy (value) of the variable to the method.  This is the default. Reference - passes a reference to the actual variable.  Marked with "ref", use this when you want to pass a value in and have any change to that value be persistent when the method is complete Out - passes a reference to the actual variable.  Marked with "out", use this when you want the method to generate a value and place it for later use in the actual variable (persists when the method is complete)

Example 1 and the output is ? static void Main() { int a = 42; Console.WriteLine (a); B (a); } static void B (int x) x += 9; Console.WriteLine (x);

Example 2 and the output is ? static void Main() { int a = 42; Console.WriteLine (a); B (ref a); } static void B (ref int x) x += 9; Console.WriteLine (x);

Example 3 and the output is ? static void Main() { int a; B (out a); Console.WriteLine (a); } static void B (out int x) x = 9; Console.WriteLine (x);

Example 4 and the output is ? class Z { public int y; } static void Main() { Z myZ = new Z(); myZ.y = 42; Console.WriteLine (myZ.y); B (myZ); Console.WriteLine (myZ.y); } static void B (Z x) x.y += 9; Console.WriteLine (x.y);

Example 5 and the output is ? class Z { public int y; } static void Main() { Z myZ = new Z(); myZ.y = 42; Console.WriteLine (myZ.y); B (ref myZ); Console.WriteLine (myZ.y); } static void B (ref Z x) x = new Z(); x.y = 1; Console.WriteLine (x.y);

Be careful to note the difference between a pass-by-reference parameter and a parameter of a reference type. Use the activation stack to track local variables and how parameters of the above types affect the variables from one stack frame to the next.