Lecture 35 Polymorphism Last time 1 Method overriding 2 Shadowing 3 Access issues in inheritance Today 1 Project 7 assigned 2 Object 3 Polymorphism and abstract methods 4 Upcasting downcasting 11 20 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 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 Object Recall inheritance induces is a hierarchy on classes Undergrad is a Student Student is a Person etc Person is a Person is a n Object Object Person Student Undergrad GradStudent Faculty Instructor Professor CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Administrator 3 More on Object Special class at top of class inheritance hierarchy Defined in java lang so available in every program Every class is derived either directly or indirectly from Object If a class is not derived from anything it is automatically derived from Object e g public class Foo is equivalent to public class Foo extends Object CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 4 Structure of Object No instance variables A number of methods including toString equals Object o Note parameter to equals has type Object so any object can be an argument These methods can and usually should be overridden Class hierarchy Defined in java lang Object Your classes are all here Person Student Undergrad GradStudent String Faculty Instructor Professor CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Boolean Integer Number Float Double The classes from the Java class library are here 5 Polymorphism and Inheritance Recall Objects in derived classes can be used wherever objects in base classes are needed e g Person p new Student Java uses late binding to determine which version of overridden method to use This means reference variables like p are polymorphic Object referred to by p may be from any class derived from Person An object is not modified when assigned to p it retains its original form e g Student in above case CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 6 Polymorphism and Arrays Example Create an array of various university people and print Person list new Person 3 list 0 new Person Col Mustard 000 00 0000 list 1 new Student Ms Scarlet 111 11 1111 1998 3 2 list 2 new Faculty Prof Plum 222 22 2222 1981 for int i 0 i list length i System out println list i toString What type is list i It can be a reference to any object that is derived from Person Late binding ensures the appropriate toString will be called CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 7 An Inheritance Example Goal picture drawing program Picture is array of different shapes Program should draw each shape on screen Desiderata Shapes to handle initially are circles rectangles Want to leave door open for new shapes in future Drawing shapes depends on kind of shape drawing a rectangle different from drawing a circle CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 8 Class Design Use inheritance hierarchy for shapes Base class Shape includes Color field as in Lecture 33 drawMe method for drawing shape Derived classes will override drawMe Shape Circle Triangle Right Triangle Rectangle Equilateral Triangle CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland Base class includes private int color public void setColor int c public int getColor public void drawMe Triangle class subclasses may be added later 9 Picture Drawing Picture array shapes of type Shape To draw picture invoke drawMe for each shape Shape shapes new Shape shapes 0 new Circle shapes 1 new Rectangle for int i 0 i shapes length i shapes i drawMe Note importance of late binding for this strategy Heap shapes 0 a Circle object 1 a Rectangle object 2 CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 10 Java Code Assume class Point storing x y coordinates Code layout public class Shape private int color public void drawMe Color of shape public class Circle extends Shape private double radius Radius of circle private Point center Center of circle public void drawMe Draws a circle public class Rectangle extends Shape private Point upperLeft Upper left corner private Point lowerRight Lower right corner public void drawMe Draws a rectangle CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 11 Abstract Methods Problem how to implement drawMe method for Shape Shape class is for generic shapes insufficient info to draw Could just do something ad hoc in implementation Raise an exception Do nothing Bad idea Point of drawMe method is to override it in derived classes If we don t override ad hoc implementation run time errors Could leave out drawMe method from Shape but then we lose access to drawMe method in picture drawing Better solution Abstract methods classes What we want is for the compiler to know that Shape is an incomplete class it declares but does not define some methods e g drawMe that will be implemented in derived classes To do this we use abstract methods CMSC 131 Fall 2006 Rance Cleaveland 2006 University of Maryland 12 Abstract Methods and Classes Abstract methods Like method declarations in an interface Prototype info return type name arguments
View Full Document