3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C.

Slides:



Advertisements
Similar presentations
Clonazione La clonazione... Ovvero: come costruire una copia (probabilmente che ritorni true su equals?)
Advertisements

Lecture 5: Interfaces.
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Java Review Interface, Casting, Generics, Iterator.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
CPSC150 Inheritance Details Chapter 9. CPSC150 Print in Entertainment ver 2 (with inheritance): public void print() { System.out.print("title: " + title.
Mutable, Immutable, and Cloneable Objects Chapter 15.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Value Semantics CS-240 & CS-341 Dick Steflik. Value Semantics determine how the value(s) of one object are copied to another object in C++ the value semantics.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4.
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Interface. Interface interface the public methods of the classWhen you talk about the interface of a class you typically mean the public methods of the.
Cloning.  Goal: Create an identical, independent copy of an object  Override the method (every class inherits clone() from class Object) public Object.
Abstract Classes and Interfaces
16-Aug-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming in Java Topic : Interfaces, Copying/Cloning,
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Does not implement clone() method! public class Bar { … public Object clone() { … } Does not implement Cloneable interface!
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Advanced Programming Rabie A. Ramadan vpro/ Lecture 4.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSE 1341 Honors Professor Mark Fontenot Southern Methodist University Note Set 17.
Constructor in Inheritance. 2 Constructors are used to initialized object. In inheritance the base class contains default constructor then, the base class.
Object Oriented Programming Java 1 Some Notes on Cloning, Packages and Visibility Notes from Bruce Eckel’s “Thinking in Java” and “Just Java” by.
Objects and Variables Local variables – Confined to single context: allocated on stack – Primitive types such as int or object references – Must be initialized.
Java Type System and Object Model Horstmann ch , 7.7.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Encapsulation, Inheritance, Composition. Class Methods Can be either void or return Can have parameters or not Must be static Should be public Know how.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
CS 151: Object-Oriented Design November 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
CS 61B Data Structures and Programming Methodology July 8, 2008 David Sun.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Carnegie Mellon University, Graduate School of Industrial Administration 1 Initialization and Cloning Slides adapted from course text “Thinking in Java”
Classes Revisited Chapter 8.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Last Revision. Question1 Novice Java programmers often write code similar to, class C { public int x;... }... C[] a = new C[10]; for(int i = 0; i < a.length;
Object Oriented Programming Lecture 2: BallWorld.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
1 Overload assignment = as a member function class point { public:..... point & operator = (const point & a);..... private: double my_x; double my_y;.....
Chapter 7: Cloning and RTTI
Functions.
Chapter 8 Classes and Objects
Interfaces.
CIS3023: Programming Fundamentals for CIS Majors II
Computer Science II Exam 1 Review.
CSE 331 Cloning objects slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
The Big Three If you need one of them, you need all of them
Chapter 11: More on the Implications of Inheritance
Polymorphism.
Chapter 8 Class Inheritance and Interfaces
Sampath Kumar S Assistant Professor, SECE
Chapter 11 Classes.
Presentation transcript:

3/11/2016ITK 2751 String String class objects are immutable public String s1,s2; s1 = "This is a String!!"; s2 = s1; s1:FFFF218C This is a String!! s2:FFFF218C public String s; s = "This is a String!!"; s = s.toUpperCase(); s:FFFF218C This is a String!! FFFF21BD THIS IS A STRING!!

3/11/2016ITK 2752 String public String s; s = "This is a String!!"; ChangeToUpper(s); s: public void ChangeToUpper (String s) { s = s.toUpperCase(); } s:FFFF218C This is a String!! FFFF21BD THIS IS A STRING!! s:FFFF21BDs:FFFF218C String class objects are immutable This function does not have any side effect

3/11/2016ITK 2753 Array is mutable int[] a; a = new int[4]; for (int i=0;i<a.length;i++) a[i] = i*i; } swap(a,1,2); public void swap(int[] a, int i, int j){ int temp; temp = a[i]; a[i]=a[j]; a[j]=temp; } a:FFFF218C 0 [0] 1 [1] 4 [2] 9 [3] FFFF218C 0 [0] 4 [1] 1 [2] 9 [3] a:FFFF218C i:1 j:2 FFFF218C [0] [1] [2] [3]

3/11/2016ITK 2754 Renew an Array... int a = new int[6];... RenewArray(a,4); // renew array a to size 4... public static void RenewArray(int[] a, int size) {... a = new int[size];... } a a Any Problem? 4 size How to fix?

3/11/2016ITK 2755 Renew an Array... int a = new int[6];... a = RenewArray(a,4); // renew array a to size 4... public static int[] RenewArray(int[] a, int size) {... a = new int[size];... return a; } a a size

3/11/2016ITK 2756 Copy the reference class Stack { private int size,top=-1; private int[] stk;..... } FFFF218C 3 [0] 4 [1] 7 [2] 0 [3] size:4 top:2 stk:FFFF218C public static void main(...) { Stack a,b; a = new Stack(4);.... b = a;.... } Stack a:Stack b:

3/11/2016ITK 2757 Shallow Copy class Stack { private int size,top=-1; private int[] stk;..... } Shallow Copy Shall copy can be done by the default clone FFFF218C 3 [0] 4 [1] 7 [2] 0 [3] size:4 top:2 stk:FFFF218C public static void main(...) { Stack a,b; a = new Stack(4);.... b = (Stack) a.clone();.... } Stack a:Stack b: size:4 top:2 stk:FFFF218C

3/11/2016ITK 2758 public class SillyStack implements Cloneable { public int size,top=-1; public int[] stk;.... public Object clone() { try { return (Object) super.clone(); } catch(CloneNotSupportedException e) { return null; }.... } Cloneable Shall copy can be done by the default clone marker interface

3/11/2016ITK 2759 Deep Copy Deep copy needs to be programed by the programmer class Stack { private int size,top=-1; private int[] stk;..... } Shallow Copy FFFF218C 3 [0] 4 [1] 7 [2] 0 [3] size:4 top:2 stk:FFFF218C public static void main(...) { Stack a,b; a = new Stack(4);.... b = (Stack) a.clone();.... } Stack a:Stack b: size:4 top:2 stk:FFFF00A5 3 [0] 4 [1] 7 [2] 0 [3] Deep Copy

3/11/2016ITK public class Stack implements Cloneable { public int size, top=-1; public int[] stk;.... // This is the copy constructor public Stack(Stack s) { this.top = s.top; this.size = s.size; this.stk = new int[this.size]; for (int i=0;i<this.size;i++) { this.stk[i] = s.stk[i]; }.... } Copy constructor for deep copy public static void main(...) { Stack a,b; a = new Stack(4);.... b = Stack(a);.... } Using the copy constructor

Clone method for deep copy 3/11/2016ITK public class Stack implements Cloneable { public int size, top=-1; public int[] stk;.... public Object clone() { Stack newStack; newStack = new Stack(this.size); newStack.top = this.top; for (int i=0;i<this.size;i++){ newStack.stk[i] = stk[i]; } return (Object) newStack; }.... } public static void main(...) { Stack a,b; a = new Stack(4);.... b = (Stack) a.clone();.... } Using the clone method Why bother? Why can we just use copy constructor?

3/11/2016ITK public class A { public int a; public A(int a) {// This is the constructor this.a = a; } public A(A obj) { // This is the copy constructor this.a = obj.a; } public String print() { return "- : "+this.a; } Copy constructor vs. clone

3/11/2016ITK public class B extends A { public int b; public B(int b) { // constructor super(b+1); this.b = b; } public B(B obj) { // copy constructor super(obj); // call the copy constructor // of the super class this.b=obj.b; } public String print() { return super.print()+" : "+this.b; } Subclass B

3/11/2016ITK public class C extends B { public int c; public C(int c) { // constructor super(c+1); this.c = c; } public C(C obj) { // copy constructor super(obj); // call the copy constructor // of the super class this.c = obj.c; } public String print() { return super.print()+" : "+this.c; } Subclass C A + a B + b C + c

3/11/2016ITK A[] a = new A[2]; B[] b = new B[2]; C[] c = new C[2]; for (int i=0; i<2; i++) { a[i] = new A(i); b[i] = new B(i); c[i] = new C(i); } printArray(a,b,c); System.out.println("**"); for (int i=0; i<2; i++) { a[i] = b[i]; b[i] = c[i]; c[i].a = 100; } printArray(a,b,c); Arrays of A, B, C a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] - : 0 - : 1 : 0 - : 2 : 1 : 0 [1] - : 1 - : 2 : 1 - : 3 : 2 : 1 ** [0] [1]

3/11/2016ITK A[] a = new A[2]; B[] b = new B[2]; C[] c = new C[2]; for (int i=0; i<2; i++) { a[i] = new A(i); b[i] = new B(i); c[i] = new C(i); } printArray(a,b,c); System.out.println("**"); for (int i=0; i<2; i++) { a[i] = b[i]; b[i] = c[i]; c[i].a = 100; } printArray(a,b,c); Simple Assignment ** [0] - : 1 : 0 - : 100 : 1 : 0 [1] - : 2 : 1 - : 100 : 2 : 1 a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] [1] 100

3/11/2016ITK A[] a = new A[2]; B[] b = new B[2]; C[] c = new C[2]; for (int i=0; i<2; i++) { a[i] = new a(i); b[i] = new b(i); c[i] = new c(i); } printArray(a,b,c); System.out.println("**"); for (int i=0; i<2; i++) { a[i] = new B(b[i]); b[i] = new C(c[i]); c[i].a = 100; } printArray(a,b,c); Using copy constructor ** [0] - : 1 : 0 - : 2 : 1 : 0 - : 100 : 1 : 0 [1] - : 2 : 1 - : 3 : 2 : 1 - : 100 : 2 : 1 a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] [1] B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 100

3/11/2016ITK A[] a = new A[2]; B[] b = new B[2]; C[] c = new B[2]; for (int i=0; i<2; i++) { a[i] = new A(i); b[i] = new B(i); c[i] = new C(i); } printArray(a,b,c); System.out.println("**"); ACopy(c,a); // copy c to a printArray(a,b,c); Problem of using copy constructor // copy x to y public static void ACopy(A[] x, A[] y) { for (int i=0; i<x.length; i++) y[i] = new A(x[i]); } a: b: c: [0] [1] [0] [1] A a=0 A a=1 B a:1 b:0 B a:2 b:1 C a:2 b:1 c:0 C a:3 b:2 c:1 [0] [1] A a:2 A a:3 ……………………………………… ** [0] - : 2 - : 1 : 0 - : 2 : 1 : 0 [1] - : 3 - : 2 : 1 - : 3 : 2 : 1

3/11/2016ITK public class X implements Cloneable { public int x; public X(int x) { this.x = x; } public X(X obj) { // This is the copy constructor this.x = obj.x; } public String print() { return ": "+x; } public Object clone() { X n; try { n = (X) super.clone(); } catch (CloneNotSupportedException e) { return null; } n.x = this.x; return (Object) n; } Superclass X implements Cloneable Marker interface To handle exception when super is not cloneable

3/11/2016ITK public class Y extends X implements Cloneable{ public int y; public Y(int y) { // constructor super(y+1); this.y = y; } public Y(Y obj) { // copy constructor super(obj); // call the super’s constructor this.y = obj.y; } public Object clone() { Y n; n = (Y) super.clone(); // super is cloneable n.y = this.y; return (Object) n; } public String print() { return super.print()+" : "+y; } Subclass Y

3/11/2016ITK public class Z extends Y implements Cloneable{ public int z; public Z(int z) { // constructor super(z+1); this.z = z; } public Z(Z obj) { // copy constructor super(obj); // call the super’s constructor this.z = obj.z; } public Object clone() { Z n; n = (Z) super.clone(); // super is cloneable n.z = this.z; return (Object) n; } public String print() { return super.print()+" : "+z; } Subclass Z

3/11/2016ITK for (int i=0; i<2; i++) { x[i] = new X(i); y[i] = new Y(i); z[i] = new Z(i); } AClone(y,x); AClone(z,y); printArray(x,y,z); Using clone method // clone a to b public static void AClone(X[] a, X[] b) { for (int i=0; i<a.length; i++) b[i] = (X) (a[i].clone()); } x: y: z: [0] [1] [0] [1] X x=0 X x=1 Y x:1 y:0 Y x:2 y:1 Z x:2 y:1 z:0 Z x:3 y:2 z:1 [0] [1] [0] - : 1 : 0 - : 2 : 1 : 0 [1] - : 2 : 1 - : 3 : 2 : 1 Y x:1 y:0 Y x:2 y:1 Z x:3 y:2 z:1 Z x:2 y:1 z:0

3/11/2016ITK The difference between copy-constructor and clone method // clone a to b public static void AClone(X[] a, X[] b) { for (int i=0; i<a.length; i++) b[i] = (X) (a[i].clone()); } copy-constructor statics determined during the compiling time  clone method dynamic determined during the running time // copy x to y public static void ACopy(A[] x, A[] y) { for (int i=0; i<x.length; i++) y[i] = new A(x[i]); }