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.InterfacesInterfaces 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 ReconsideredCopy 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