Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.

Slides:



Advertisements
Similar presentations
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Object Oriented Programming in Java. Object Oriented Programming Concepts in Java Object oriented Programming is a paradigm or organizing principle for.
Session 9 MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
Methods Liang, Chapter 4. What is a method? A method is a way of running an ‘encapsulated’ series of commands. System.out.println(“ Whazzup ”); JOptionPane.showMessageDialog(null,
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Implications of Inheritance COMP204, Bernhard Pfahringer.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Programming With Java ICS Chapter 8 Polymorphism.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Java exercise review Lesson 25: Exercise 1, 2 and 3.
1 Block1 – unit 2 (The Case study in Budd 5-6).  create a small application that uses the Abstract Windowing Toolkit (AWT)  Swing packages to simulate.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Implications of Inheritance COMP206, Geoff Holmes and Bernhard Pfahringer.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
Engr 691 Special Topics in Engineering Science Software Architecture Spring Semester 2004 Lecture Notes.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
Chapter 5: Ball Worlds Features 2 classes, constant data fields, constructors, extension through inheritance, graphics.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Session 19 Chapter 10 – Mechanisms for Software Reuse.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
Session 18 Chapter 8: Understanding Inheritance. Recall Exercise 2 From Tuesday It’s very annoying to move a target from the pallet and drop it in the.
Session 22 Chapter 11: Implications of Inheritance.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Objects and Classes Chapter Nine. Definition “an object is a combination of some data (variables) and some actions (methods)”. Hopefully the data and.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Session 6 Comments on Lab 3 & Implications of Inheritance.
Interfaces and Inner Classes
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Session 18 Lab 9 Re-cap, Chapter 12: Polymorphism & Using Sound in Java Applications.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Java exercise review Lesson 26: Exercise 4. Exercise #4 – a sample solution 1.Write the psudocode and the deployment diagram for what you are planning.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
1 Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Lecture 12 Implementation Issues with Polymorphism.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Session 8 Lab 4 comments, MultiballWorld, Refactoring Ball using Inheritence, and Intro. to CannonWorld.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
A structured walkthrough
C11, Implications of Inheritance
Chapter 5: Enhancing Classes
Java Memory Management
Java Memory Management
Java Review: Reference Types
Corresponds with Chapter 7
Chapter 11: More on the Implications of Inheritance
CS2011 Introduction to Programming I Arrays (II)
Chapter 8 Class Inheritance and Interfaces
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Session 7 More Implications of Inheritance & Chapter 5: Ball World Example

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

Assignment of Objects public class Box { private int value; public Box() { value = 0; } public void setValue (int v) { value = v; } public int getValue() { return value; } pubic class BoxTest { public static void main (String [] args) { Box x = new Box(); x.setValue ( 7 ); Box y = x; y.setValue( 11 ); System.out.println( “x’s value = “ + x.getValue()); System.out.println(“y’s value = “ + y.getValue()); } // end main } What’s the output? Why?

Assignment of Objects semantics for assignment - simply copies the address of to the object Address Heap: single Box object: 4000 Run-time Stack: Main’s Activation Record: x: 4000 y: 4000

Try to fix the problem by creating a copy() method.

Cloneable Interface Java has no general mechanism to copy arbitrary objects But, the base class Object provides a clone() method that creates a bitwise copy The Cloneable interface represents objects that can be cloned Several methods in Java’s library use clone()

Cloneable Box Class public class Box implements Cloneable { private int value; public Box() { value = 0; } public void setValue (int v) { value = v; } public int getValue() { return value; } public Object clone() { Box b = new Box(); b.setValue ( getValue() ); return b; } // end clone }

Using the Cloneable Box Class pubic class BoxTest { public static void main (String [] args) { Box x = new Box(); x.setValue ( 7 ); Box y = (Box) x.clone(); // assigns y a copy of y y.setValue( 11 ); System.out.println( “x’s value = “ + x.getValue()); System.out.println(“y’s value = “ + y.getValue()); } // end main }

Equality Test == tests for pointer equality, so == really tests for object identity (i.e., is it the same object) and not equality. equals() is a method inherited from class Object. The Java run-time system uses equals() in a number of places and expects to be able to test any Object for equality with any other Object.

3. What’s wrong with each of the following? public class Ball { private Rectangle location; private Color color; public Color color() { return color; } public Rectangle region() { return location; }... a) public boolean equals( Ball b ) { return this == b; } b) public boolean equals( Ball b ) { return this.equals( b ); } c) public boolean equals( Ball b ) { return ( location == b.region() ) && ( color == b.color () ); }

equals() Example class Circle extends Shape { int radius;... int getRadius() { return radius; }... public boolean equals (Object arg) { if (arg instanceof Circle) { Circle argc = (Circle) arg; if (radius == argc.getRadius()) { return true; } // end if return false; }

equals() of Ball... public boolean equals (Object arg) { if (arg instanceof Ball) { Ball argBall = (Ball) arg; if (color.equals(argBall.color()) && location.equals(argBall.region()) { return true; } // end if return false; }

Chapter 5: Ball World Example

Figuring Out the BallWorld Why use a Rectangle instead of a Point to represent the Ball’s location?RectanglePoint Modify the color of the ball to be green. Modify the ball to be much larger and to move much faster. Modify the simulation so that it runs much longer. Modify the shape of the window to be 100 X 800. Modify the Ball so the it initial moves in a random direction and with random magnitude. – (Hint: Use the static double Math.random() in the Java package Math. It returns a random in the range [0.0 – 1.0).

The Role of Inheritance in Java Graphics “Don’t call us. We’ll call you.” Without inheritance, we would have to understand many details of how windows work and how they interact with the operating system. Some of those details are over our heads at this point. We don’t know enough OOP or Java yet. Even if we could understand them (and we will eventually), we don’t care about them.

The Role of Inheritance in Java Graphics With inheritance, a BallWorld can act as a “regular” Frame when we don’t care about the details and as a special kind of Frame when we do. But there is more to it than that. Not only does BallWorld inherit a lot of individual methods that we use — but many of the methods in Frame call the methods we implement in BallWorld!

Example: Frame manipulation Your BallWorld should be able to handle: Frame relocation Frame resizing Minimizing/Maximizing Etc…. But wait, _I_ didn’t write any code to handle this…

Example: The show() and paint() Methods But there is more to it than that. Not only does BallWorld inherit a lot of individual methods that we use — but many of the methods in Frame call the methods we implement in BallWorld! Consider how the BallWorld program displays its output: // BallWorld public void paint( Graphics g ) { aBall.paint( g ); aBall.move ();... counter = counter + 1; if ( counter < 2000 ) repaint(); else System.exit(0); }

Example: The show(), paint(), and repaint() Methods // In BallWorld public void paint( Graphics g ) { aBall.paint( g ); aBall.move ();... counter = counter + 1; if ( counter < 2000 ) repaint(); else System.exit(0); } // In BallWorldApplication BallWorld world=new BallWorld(Color.red); world.show(); show() inherited from Frame show() calls paint(Graphics g) of BallWorld the Graphics object g passed by show() has the ability to draw a host of items to the Frame the Graphics object g is passed to paint(g) of aBall Ball’s paint uses the Graphics object to put a fillOval on the screen // In Ball public void paint( Graphics g ) { g.setColor( color ); g.fillOval( location.x, location.y, location.width, location.height );

“Don’t call us. We’ll call you.” This is a simple example of... multiple objects collaborating to solve a problem It is also an even better example of... writing a program by filling in the details (e.g., paint()) of another program (e.g., Frame) that already does a lot of work The Java AWT is also an example of a framework, a group of classes that work together to provide generic solutions in an application domain, which programmers can extend to provide specific solutions.