Lecture 36 Cloning Last time 1 Object 2 Polymorphism and abstract methods 3 Upcasting downcasting Today 1 Project 7 assigned 2 equals reconsidered 3 Copying and cloning 4 Composition 11 27 2006 CMSC 131 Fall 2006 Rance Cleaveland 2006 Univeristy of Maryland Project 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 automated CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 1 Inheritance 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 2006 Rance Cleaveland 2006 University of Maryland 2 Inheritance Recap cont All objects are derived directly or indirectly from Object Late binding and inheritance allows you to create polymorphic variables 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 instanceof operator 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 instanceof CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 3 Abstract 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 shared Advantage abstract classes A given class can match multiple interfaces but can only inherit from one class Advantage interfaces Which to use Code to share use abstract classes Otherwise interfaces CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 University Person Recap extends Person class Student instance variables int admitYear double gpa methods Student various int getAdmitYear double getGpa void setAdmitYear int void setGpa double String toString boolean equals Student class Person instance variables String name String idNum methods Person various String getName String getIdNum void setName String void setIdNum String String toString boolean equals Person CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland extends Person class Faculty instance variables int hireYear methods Faculty various int hireYear void setHireYear int String toString boolean equals Student 5 equals Reconsidered Recall definition of equals in Person public boolean equals Person p return name equals p getName idNum equals p getIdNum in Student public 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 2006 Rance Cleaveland 2006 University of Maryland 6 Why 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 2006 Rance Cleaveland 2006 University of Maryland 7 A 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 false CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Copy Constructors Reconsidered Copy constructors used to make copies of objects Recall copy constructor for Person public Person Person p name p name idNum p idNum Does this always do what we want CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 9 Deep Copying of a Person Array public 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 deepCopy a returns r a 0 new Student BG 123 45 6789 2005 3 2 a 1 new Faculty FS 111 11 1111 2003 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 this CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 A 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 new copy will be in same class as original object late binding This object based approach to copying is called cloning Object class includes clone method that can be overridden CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Defining clone in Person public Person clone return new Person this this used to refer to current object Copy constructor called on this Similar clone methods definable for Student Faculty New deepCopy method public static Person deepCopy Person a Person r new Person a length for int i 0 i a length i r i a i clone return r CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 equals
View Full Document