Session 22 Chapter 11 Implications of Inheritance Using











- Slides: 11

Session 22 Chapter 11: Implications of Inheritance

Using Images in Java • Image class in java. awt. Image • Toolkit (see section 17. 6 of text) contains some useful utilities: – get. Image() takes a string or URL and returns a value of type Image – get. Screen. Size() returns a Dimension object containing the number of pixels of both the height and width – get. Screen. Resolution() returns the number of dots per inch • Dividing one by the other will yield the physical size of the screen – get. Font. List() returns an array of strings that contain the names of the fonts available on the current system – etc. .

Simple Image Code import java. awt. *; public class Image. Test. App extends Frame { private Image demo; public static void main(String[] args) { Image. Test. App app = new Image. Test. App(); } public Image. Test. App() { set. Title( "Image Test" ); set. Size( 450, 450 ); demo = Toolkit. get. Default. Toolkit(). get. Image("logo. gif" ); show(); } public void paint( Graphics g ) { g. draw. Image( demo, 200, 50, this ); } } // end class Image. Test. App

Exercise with Image Code • Modify the Image. Test. App so the logo moves to where the mouse is pressed in the frame.

A Solution import java. awt. *; import java. awt. event. *; public class Image. Test. App extends Frame { private Image demo; private int demo. X; private int demo. Y; private int demo. Size; public static void main(String[] args) { Image. Test. App app = new Image. Test. App(); } public Image. Test. App() { set. Title( "Image Test" ); set. Size( 450, 450 ); demo = Toolkit. get. Default. Toolkit(). get. Image( "logo. gif" ); demo. X = 200; demo. Y = 200; demo. Size = 50; add. Mouse. Listener( new Mouse. Keeper() ); show(); }. . .

A Solution import java. awt. *; import java. awt. event. *; public class Image. Test. App extends Frame {. . . public void paint( Graphics g ) { g. draw. Image( demo, demo. X-demo. Size/2, demo. Y-demo. Size/2, demo. Size, this ); } private class Mouse. Keeper extends Mouse. Adapter { public void mouse. Pressed( Mouse. Event e ) { demo. X = e. get. X(); demo. Y = e. get. Y(); repaint(); } // end mouse. Pressed } // end private class Mouse. Keeper } // end class Image. Test. App

Polymorphism • polymorphism comes from the Greek root for “many shapes” • polymorphism is about how we can use different objects in the same place in our program, i. e. , polymorphism depends on objects that are substitutable for one another • Decorators are great examples of polymorphism: – Because the decorator class is a subclass, instance of the decorator can substitute for instances of the superclass – Because the decorator class holds an instance of the superclass, it can hold instances of anything substitutable for the superclass.

Polymorphic Variable • A polymorphic variable can hold many different types of values • Object-oriented languages often restrict the types of values to being subclasses of the declared type of the variable. • Example: “Pin. Ball. Target target” can be assigned a “Hole”, “Score. Pad”, etc.

Implications of Inheritance/Polymorphism • At compile-time, the amount of memory for polymorphic variables cannot be determined, so all objects reside in the heap • Because values reside in the heap, reference semantics is used for assignment and parameter passing • Most natural interpretation of equality is identity. Since programmers often require a different meaning two operators are needed • Garbage collection needed since it is hard for a programmer to know if when an object is no longer referenced

Typical Memory Layout Heap Stack Global variables Program

Stack-based Memory Main: Obj. A a = new Obj. A(); Obj. B b = new Obj. B(); a. do(5, b) public class Obj. A { int x = 100; public void do (int y, Obj. B my. B) { int loc = 6; int t = my. B. do. More(loc); . . . } public class Obj. B { int z = 30; public int do. More(int i) { z = z + i; return z; } } } • Objects are stored on the heap • When a method is called, an activation record is allocated on the stack to hold: – return address (where to return after execution) – parameters – local variables (stuff declared in the method) • When a method returns, the activation record is popped