DOC PREVIEW
Berkeley COMPSCI 61B - Lecture Notes

This preview shows page 1-2-3-25-26-27 out of 27 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 27 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Slide 1Announcementreturns in finallyExceptions Inside catch and finallyInheritance with ExceptionsNested ClassesStatic Nested ClassStatic Nested ClassExampleInner classesExampleCloningCloningclone()Does Shallow Copy Matter?Redefining cloneImplement Deep CopyingAnother ExampleAnother ExampleCloning in the Subclasscloneable Interfacefor each LoopEnumenumenumSlide 26ReadingCS 61B Data Structures and Programming Methodology July 8, 2008David SunAnnouncement•Midterm 1 is tomorrow 11:00am – 1:00pm.•Everything up to and including today’s material can appear on the test.returns in finallytry { statementX; return 1; } catch (SomeException e) { e.printStackTrace(); return 2; } finally { f.close(); return 3; }•The finally clause is executed regardless whether an exception is thrown or not. So in the above, the code always returns 3.Exceptions Inside catch and finally•An exception thrown in a catch clause will proceed as usual, but the finally clause will still get executed before the exception goes on.•An exception thrown in a finally clause replaces the old exception, and the method ends immediately. •You can also put a try clause inside a catch or finally clause.Inheritance with Exceptionsclass parent {void f() { . . . }}//Compiler Errorclass child extends parent {void f() throws IOException { . . . }}•Why? –Substitution rule.•Rule: In the overriding method in the child class cannot expand the set of checked exception thrown by the original method in the parent class.Nested Classes•A nested class is a class that is defined inside another class.class OuterClass { ... class NestedClass { ... } }•Two types:–Static –Non-staticStatic Nested Class•A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class.•Why bother?–The nested class might be used only in the implementation of the outerclass -- avoid name clashes or “pollution of the name space” with names that will never be used anywhere else.–The nested class is conceptually “subservient” to the outerclass -- group classes and provide better encapsulation.Static Nested Classclass OuterClass { private SomeClass outerField;private void outerMethod() { . . . }static class StaticNestedClass { ... }}•Like static fields and methods, StaticNestedClass is associated with OuterClass, i.e. to create an object of StaticNestedClass: OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();•Like static class methods, StaticNestedClass cannot refer to the private instance field outerField or the method outerMethod() defined in OuterClass.Example//List.javapublic class List {private ListNode head;private int size; public List() {head = null;size = 0;} . . . }//ListNode.java, global//visibilitypublic class ListNode {int item; ListNode next;}//List.javapublic class List {private ListNode head;private int size; public List() {head = null;size = 0;} . . . //now only visible inside List private static class ListNode {int item; ListNode next; }}Inner classes•An inner class is associated with an instance of its enclosing class so it has direct access to that object's methods and fields. OuterClass outerObject = new OuterClass();OuterClass.InnerClass innerObject = outerObject.new InnerClass();compare withOuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();Exampleclass Bank { }private int count;private void connectTo( . . .) { . . .}public class Account {public Account () { count ++;} public void call (int number) {Bank.this.connectTo( . . .); . . . }}Bank e = new Bank( . . );Bank.Account a1 = e.new Account( . . . );Bank.Account a2 = e.new Account( . . .);Accountouter= Bankprivate int count;private void connectTo();Note: the outer reference is invisible in the definition of the inner class. You can’t explicitly refer to outer.Cloning•Copying a variable only copies the reference, the original variable and the copy refer to the same object, so changing one affects the other. Point original = new Point(1, 3);Point copy = original;copy.x = 3;what’s in original.x ?x = 3y = 3 original = copy =Cloning•If you want to make a copy of the object that begins its life being identical to original but whose state can diverge over time:Employee copy = original.clone();copy.x = 3; //original unchangedx = 1y = 3 original = copy =x = 3y = 3clone()•The clone method is a protected method of Object.– So clone() can only be called in side the class Point.–The user of Point can’t make the call original.clone();•Default implementation in Object is a shallow copy:–It will make a field-by-field copy. –If all the fields are basic types, then field copying is fine. –If some of the fields contain references to subobjects, then field copying gives your another reference to the subobject.Does Shallow Copy Matter?•It depends…•Sharing is “safe” if:–If the shared subobject object is immutable, e.g. a String–If the subobject remain constant throughout the lifetime of the object, with no mutators touching it.•But frequently you want a deep copy.Redefining clone •How:1. Have the class implement the Clonable interface.2. Redefine the clone method with the public access modifier.class Employee implements Clonable {. . . //raise visibility to public, change return typepublic Employee clone() throws CloneNotSupportedException {return (Employee) super.clone;}}Implement Deep Copyingclass Employee implements Clonable {. . . public Employee clone() throws CloneNotSupportedException {//call object.clone()Employee theClone = (Employee) super.clone();//clone mutable fieldstheClone.birthDay = (Date)birthDay.clone();//clone other mutable field. . . return theClone;}}Another Examplepublic class List implements Cloneable { public List clone() throws CloneNotSupportedException { //same as return super.clone();List l = new List(); l.head = head; l.size = size; return l; } }Another Examplepublic class List implements Cloneable { public List clone() throws CloneNotSupportedException { List l = (List)super.clone();l.head = head.clone();} }public class ListNode implements Cloneable {public ListNode clone() throws CloneNotSupportedException { ListNode theClone = (ListNode)super.clone();theClone.next = theClone.next.clone(); //recursive} }Cloning in the Subclass•The clone method in the superclass may be sufficient if the additional state in the subclass are all primitive types.•If data


View Full Document

Berkeley COMPSCI 61B - Lecture Notes

Documents in this Course
Lab

Lab

4 pages

Matrix

Matrix

3 pages

Numbers

Numbers

14 pages

Lectures

Lectures

12 pages

Project 1

Project 1

24 pages

Exam

Exam

8 pages

Load more
Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?