Chapter 11: More on the Implications of Inheritance

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
 Specifies a set of methods (i.e., method headings) that any class that implements that interface must have.  An interface is a type (but is not a class).
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
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.
Java Review Interface, Casting, Generics, Iterator.
CS-I Final Review Hao Jiang Computer Science Department Boston College.
CSE 2501 Review Declaring a variable allocates space for the type of datum it is to store int x; // allocates space for an int int *px; // allocates space.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Java Syntax Primitive data types Operators Control statements.
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[]
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
ITI Introduction to Computing II Lab-6 Dewan Tanvir Ahmed University of Ottawa.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
Advanced Java Programming CS 537 – Data Structures and Algorithms.
Implications of Inheritance COMP206, Geoff Holmes and Bernhard Pfahringer.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
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.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Session 22 Chapter 11: Implications of Inheritance.
Programming Languages and Paradigms Activation Records in Java.
Session 6 Comments on Lab 3 & Implications of Inheritance.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Duke CPS From C++ to Java l Java history: Oak, toaster-ovens, internet language, panacea l What it is ä O-O language, not a hybrid (cf. C++)
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
1 Interfaces and Abstract Classes The ability to define the behavior of an object without specifying that behavior is to be implemented Interface class.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Lecture 12 Implementation Issues with Polymorphism.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
Session 7 More Implications of Inheritance & Chapter 5: Ball World Example.
1 clone() Defined in Object Creates an identical copy –Copies pointers to fields (does not copy fields of fields) –Makes a shallow copy if the object’s.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
C11, Implications of Inheritance
Object-Oriented Concepts
Java Memory Management
Functions.
Java Memory Management
Software Development Java Classes and Methods
Interface.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Chapter 8 Classes and Objects
Interfaces.
Chapter 3 Assignment Statement
C Basics.
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
CSC 253 Lecture 8.
Starting Out with Java: From Control Structures through Objects
CSC 253 Lecture 8.
Corresponds with Chapter 7
null, true, and false are also reserved.
CMSC 202 ArrayList Aug 9, 2007.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
CS2011 Introduction to Programming I Arrays (II)
class PrintOnetoTen { public static void main(String args[]) {
Lecture 18: Polymorphism (Part II)
Variables and Java vs C++
Classes and Objects Object Creation
Corresponds with Chapter 5
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Chapter 11: More on the Implications of Inheritance Session 23 Chapter 11: More on the Implications of Inheritance

Stack-based Memory Objects are stored on the heap public class ObjB { int z = 30; public int doMore(int i) { z = z + i; return z; } public class ObjA { int x = 100; public void do (int y, ObjB myB) { int loc = 6; int t = myB.doMore(loc); ... } Main: ObjA a = new ObjA(); ObjB b = new ObjB(); a.do(5, b) 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

Consider Factorial Example class FacTest { static public void main (String [] args) { int f = factorial(3); // * System.out.println(“Factorial of 3 is “ + f); } static public int factorial (int n) { int c = n – 1; int r; if (c > 0) { r = n * factorial(c); // ** } else { r = 1; return r;

Assignment of Objects semantics for assignment simply copies the pointer into the heap to the object 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 } public class Box { private int value; public Box() { value = 0; } public void setValue (int v) { value = v; } public int getValue() { return value;

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 Clonable { 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 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.

Equality Test 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; }