AAA. Today’s lecture Review of Chapter 6 Go over examples.

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 3 Writing Java Applications, Java Development Tools.
Advertisements

Chapter 6 Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and.
This is Java Jeopardy Writing Methods…chapter 4…
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Interfaces A Java interface is a collection
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
Chapter 4: Writing Classes
CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Road Map Introduction to object oriented programming. Classes
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
OOP Languages: Java vs C++
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Writing Classes (Chapter 4)
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
CSC Programming I Lecture 8 September 9, 2002.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Chapter 10 Classes and Objects: A Deeper Look Visual C# 2010 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Session 7 Methods Strings Constructors this Inheritance.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Working With Objects Tonga Institute of Higher Education.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Finalizers, this reference and static Sangeetha Parthasarathy 06/13/2001.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Classes - Intermediate
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
Internet Computing Module II. Syllabus Creating & Using classes in Java – Methods and Classes – Inheritance – Super Class – Method Overriding – Packages.
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Comp1004: Building Better Objects II Encapsulation and Constructors.
© 2004 Pearson Addison-Wesley. All rights reserved3-1 Objects Declaration: String title;  title (object variable) of type String( Class )  title is just.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
COP Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes.
Chapter 5: Enhancing Classes
Java Memory Management
Java Memory Management
Classes and Objects Encapsulation
Encapsulation and Constructors
Object Oriented Programming in java
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Packages & Random and Math Classes
Corresponds with Chapter 5
Presentation transcript:

AAA

Today’s lecture Review of Chapter 6 Go over examples

Chapter 6 review What is a static method? (A static variable?) How does Java handle assignment (during method calls)? What is scope? Local? Parameter? Instance? Class? What is visibility? Public? Private?

Copying parameters Example What value gets printed? public class Test { public static void main(String[] args) { int x=3; changeVal(x,5); System.out.println(x); } public static void changeVal(int p, int v) { p = v; } ("3" is printed.)

But what does this mean for objects? What gets printed? public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); changeName(p,"Thomas"); System.out.println(p.getName()); } public static void changeName(Person p, String n) { p.setName(n); } Thomas ( Java copies the reference)

How about now? What gets printed? public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); changeName(p,"Thomas"); System.out.println(p.getName()); } public static void changeName(Person p, String n) { p = new Person(n,30); } Mason(new object created/modified/discarded) Does it matter that we have a local variable called p in changeName ? What if it were called q ?

How about now? (v2) What gets printed? public class Test { public static void main(String[] args) { Person p = new Person("Mason",21); p = changeName(p,"Thomas"); System.out.println(p.getName()); } public static Person changeName(Person q, String n) { q = new Person(n,30); return q; } "Thomas" (new object created/modified/returned, THEN p is updated to point to it.) Does it matter that we have a local variable called p in changeName ? What if it were called q ?

Reference Assignment continued Why are the references copied, instead of the entire object? Objects can be big Wastes space Takes a long time to copy What can be a downside to copying just the reference? Now you have two things pointing to the same location in memory What happens if you forget this mistakenly?

Creating lots of objects We know how to create and allocate memory to programs with new in Java Could our program ever run out of memory? ◦ Not if it is super small ◦ What if you run something for weeks that is constantly creating objects it only uses once? Want a way to retrieve (recover) memory that is no longer being used ◦ Turns out this is tricky! How do we know an object is no longer needed?

Coming up: String Methods Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection

Method Overloading (Quick View) We can have multiple methods with the same name in one class! They are distinct methods with no actual relations other than the name – this is just a mnemonic convenience! To coexist, their method signatures must be uniquely identifiable by the parameter types and method name alone. parameter names are ignored – arguments are nameless. return type is ignored – not explicit via method call, so can't help select correct method. Constructors are methods too – we can write multiple constructors for one class, as long as they have different signatures. This is a valuable opportunity (something Python disallows).

Method Overloading - Examples Example: these methods may all be in one class: 1.public int foo (int a, int b){…} 2.public int foo (char a, int b){…} 3.public int foo (int b, char a){…} 4.public int bar (int a, int b){…} 5.public String foo (int a, int b, int c){…} The following could not be added to the class: public String foo (int a, int b) {…}return type irrelevant public int foo (int other, int names){…}param names irrelevant private int foo (int a, int b){…}modifier irrelevant

© 2004 Pearson Addison- Wesley. All rights reserved 4-13 Visibility Modifiers publicprivate Variables Methods Provide services to clients Support other methods in the class Enforce encapsulation

© 2004 Pearson Addison- Wesley. All rights reserved 4-14 Let’s go over the examples

© 2004 Pearson Addison- Wesley. All rights reserved 4-15 Questions?