DOC PREVIEW
UMD CMSC 131 - Lecture 36: Cloning

This preview shows page 1-2-3-4-5-6 out of 17 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 17 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 17 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 17 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 17 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 17 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 17 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 17 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

11/27/2006 CMSC 131 Fall 2006Rance Cleaveland©2006 Univeristy of MarylandLecture 36:CloningLast time:1. Object2. Polymorphism and abstract methods3. Upcasting / downcastingToday:1. Project #7 assigned2. equals reconsidered3. Copying and cloning4. CompositionCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland1Project #7 Assigned! Project due Wednesday, 11/29 at 11 pm Project is closed You must complete the project by yourself Assistance can only be provided by teaching assistants (TAs) and instructors You must not look at other students' code Start now! Read entire assignment from beginning to end before starting to code Check out assignment now from CVS Follow the instructions exactly, as much of grading is automatedCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland2Inheritance Recap Inheritance occurs when one class (derived class, subclass) is defined from another class (base / parent class, superclass). To derive a class D from a base class B, use:public class D extends B { … } Derived class inherits all instance variables, methods from base class. It can also define new instance variables, methods In derived-class constructor, super( … ) can be used to invoke constructor from base class Derived class can explicitly refer to entities from base class using super, e.g. super.toString( ) Polymorphism: object in derived class can be used anywhere base class is expected (a Student “is a” Person!) Derived class can override base-class methods (and variables) final can be used to disallow overriding Java uses late binding to determine which version of method to use protected modifier exposes declarations to subclasses (and package)CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland3Inheritance Recap (cont.) All objects are derived (directly or indirectly) from Object Late binding and inheritance allows you to create polymorphicvariables When a method in a base class is not provided, the method and class are said to be abstract. Abstract methods may be implemented in (concrete) derived classes Run-time information about class / type information of objects can be obtained using getClass() method and instanceofoperator Upcasting of object to superclass type is always safe and done automatically Downcasting may not be safe, depending on actual class object belongs to at run-time. Safe downcasting can be done using instanceofCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland4Abstract Classes vs.InterfacesInterfaces seem like “pure abstract classes” Interfaces contain function prototypes These are similar to abstract methods Abstract classes permit some methods to be defined and sharedAdvantage: abstract classes A given class can match multiple interfaces, but can only inherit from one classAdvantage: interfaces Which to use? Code to share: use abstract classes Otherwise: interfacesCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland5University Person Recapclass: Personinstance variables:String nameString idNummethods:Person( … ) [various]String getName( )String getIdNum( )void setName( String )void setIdNum( String )String toString( )boolean equals( Person )class: Studentinstance variables:int admitYeardouble gpamethods:Student( … ) [various]int getAdmitYear( )double getGpa( )void setAdmitYear( int )void setGpa( double )String toString( )boolean equals( Student )extends Personclass: Facultyinstance variables:int hireYearmethods:Faculty( … ) [various]int hireYear( )void setHireYear( int )String toString( )boolean equals( Student )extends PersonCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland6equals() Reconsidered Recall definition of equals() … in Personpublic boolean equals (Person p) {return name.equals(p.getName()) &&idNum.equals(p.getIdNum());} … in Studentpublic boolean equals( Student s ) {return super.equals(s) &&admitYear == s.admitYear &&gpa == s.gpa;} What does following do?public static void main (String[] args) {Student bob = new Student ("R. Goode", "234-56-7890", 1998, 3.89);Faculty bob2 = new Faculty ("R. Goode", "234-56-7890", 2005);System.out.println (bob.equals (bob2));} true is printed!CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland7Why? bob “is-a” Student bob2 “is-a” Faculty Both “are-a” Person bob2 cannot be cast to Student bob2 can be cast to Person So the Person version of equals() in bob is called (overloading!) But how can objects in two different classes be equal?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland8A Better equals() Take Object as input Check for non-null-ness of input Check that class is correct Then do other checks For example in Person:public boolean equals (Object o) {if (o == null)return false;else if (o.getClass() != getClass())return false;else {Person p = (Person)o;return name.equals(p.getName()) &&idNum.equals(p.getIdNum());}} Similar improvements can be made to Student, Faculty Now bob.equals(bob2) returns falseCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland9Copy Constructors ReconsideredCopy constructors used to make copies of objects Recall copy constructor for Personpublic Person( Person p ) {name = p.name;idNum = p.idNum;} Does this always do what we want?CMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland10Deep Copying of a PersonArraypublic static Person[] deepCopy (Person[] a) {Person[] r = new Person[a.length];for (int i=0; i < a.length; i++)r[i] = new Person(a[i]);return r;} What happens if a contains Student, Faculty objects? They are converted into Person objects; extra info lost Consider: a[0] = new Student (“BG”,“123-45-6789”,2005,3.2); a[1] = new Faculty (“FS”,“111-11-1111”,2003); deepCopy(a) returns r: r[0] is Person object with name==“BG”, idNum == “123-45-6789” r[1] is Person object with name==“FS”, idNum == “111-11-1111” Why? Person copy constructor creates object in class Person Often, in copying, we want to preserve original class of copied object How to do thisCMSC 131 Fall 2006Rance Cleaveland©2006 University of Maryland11A Better Way to Do Copying In addition to copy constructors in classes … … include copying capability in objects To make a copy of an object, call object’s copy method This way,


View Full Document

UMD CMSC 131 - Lecture 36: Cloning

Documents in this Course
Set #3

Set #3

7 pages

Exam #1

Exam #1

6 pages

Exam #1

Exam #1

6 pages

Notes

Notes

124 pages

Notes

Notes

124 pages

Load more
Download Lecture 36: Cloning
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 36: Cloning 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 36: Cloning 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?