CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

Similar documents
Chapter 9 Inheritance

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Java Object Oriented Design. CSC207 Fall 2014

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

What is Serialization?

Object. OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int)

Inheritance -- Introduction

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Exercise: Singleton 1

Polymorphism. Agenda

CS-202 Introduction to Object Oriented Programming

CS111: PROGRAMMING LANGUAGE II

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance and Polymorphism

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

What is Inheritance?

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Chapter 14 Abstract Classes and Interfaces

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

Inheritance and Polymorphism

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Advanced Programming Methods. Seminar 12

Inheritance and object compatibility

Chapter 5. Inheritance

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

More Relationships Between Classes

CSE 431S Type Checking. Washington University Spring 2013

Rules and syntax for inheritance. The boring stuff

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Java Fundamentals (II)

More On inheritance. What you can do in subclass regarding methods:

First IS-A Relationship: Inheritance

Example: Count of Points

[ L5P1] Object-Oriented Programming: Advanced Concepts

Inheritance. Inheritance allows the following two changes in derived class: 1. add new members; 2. override existing (in base class) methods.

12% of course grade. CSCI 201L Final - Written Fall /7

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

Introduction to Inheritance

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Inheritance (continued) Inheritance

Abstract Classes and Interfaces

Chapter 5 Object-Oriented Programming

Instance Members and Static Members

Superclasses / subclasses Inheritance in Java Overriding methods Abstract classes and methods Final classes and methods

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Lecture 18 CSE11 Fall 2013 Inheritance

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

COP 3330 Final Exam Review

Polymorphism. Arizona State University 1

CH. 2 OBJECT-ORIENTED PROGRAMMING

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Inheritance. Transitivity

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

1 Shyam sir JAVA Notes

25. Generic Programming

10/8/2018 Programming Data Structures. class diagram for assignment 08 review: polymorphism review: exception new: File I/O

ECE 122. Engineering Problem Solving with Java

Lecture 36: Cloning. Last time: Today: 1. Object 2. Polymorphism and abstract methods 3. Upcasting / downcasting

Chapter 11 Classes Continued

Starting Out with Java: From Control Structures Through Objects Sixth Edition

Programming in C# Inheritance and Polymorphism

type conversion polymorphism (intro only) Class class

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Computer Science 210: Data Structures

Object-Oriented Software Engineering. Chapter 2: Review of Object Orientation

What is Polymorphism? Quotes from Deitel & Deitel s. Why polymorphism? How? How? Polymorphism Part 1

VIRTUAL FUNCTIONS Chapter 10

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Chapter 11: Inheritance

Chapter 10 Classes Continued. Fundamentals of Java

Object-Oriented Software Engineering Practical Software Development using UML and Java. Chapter 2: Review of Object Orientation

We are on the GUI fast track path

M301: Software Systems & their Development. Unit 4: Inheritance, Composition and Polymorphism

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

ITI Introduction to Computing II

Object-Oriented Design

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

Polymorphism Part 1 1

CSE 331. Memento Pattern and Serialization

Motivations. Chapter 13 Abstract Classes and Interfaces

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

ITI Introduction to Computing II

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

Java Programming Lecture 7

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

CSC207 Week 3. Larry Zhang

Data Structures. 03 Streams & File I/O

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Outline. Inheritance. Abstract Classes Interfaces. Class Extension Overriding Methods Inheritance and Constructors Polymorphism.

C09: Interface and Abstract Class and Method

Binghamton University. CS-140 Fall Dynamic Types

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Chapter 13 Abstract Classes and Interfaces. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

Transcription:

CSE1720 Click to edit Master Week text 08, styles Lecture 13 Second level Third level Fourth level Fifth level Winter 2014! Thursday, Feb 27, 2014 1 General Info Continuation of Chapter 9 Read Chapter 10 for next week 2 1

9.1 What is Inheritance? For each pair, determine the relationship if any " Camera, Film " Vehicle, Car " Library, Book " Animal, Dog " Car, Tree 3 When can you say that the 2 nd is a subclass of the 1 st 9-3 9.1.1 Definition and Terminology The API of a class C may indicate that it extends some other class P Every feature of P is in C C inherits from P. Child-Parent, Subclass-Superclass Inheritance = is-a = Specialization Inheritance chain, hierarchy (root, descendents, ascendant) 4 9-4 2

UML P G R P A B C C K L M N (a) (b) (c) 5 9-5 No Multiple Inheritance A B M 6 9-6 3

9.1.2 The Subclass API P + a: int + b: double + k(int): void + l(): long + m(double): void C + a: double + c: char + k(double): void + l(): long + n(long): int Upper API Table Lower API Table The API of C a: double c: char b: double k(double): void l(): long n(long): int k(int): void m(double): void 7 9-7 Feature Classification 8 Inherited from parent Lower table Added as new by child Upper table Overriding by child (same signature) Upper table Shadowing by child (same name) Upper table Note: a child cannot override with a diff return! 9-8 4

Feature Count Is this correct? x = #of methods in parent s UML y = #of methods in child s UML The child s API shows x + y methods (upper plus lower) Repeat for fields. 9 9-9 Example Graphics2D is a subclass of Graphics Graphics was the first implementation, in later versions, Graphics2D was released as a more sophisticated implementation The method drawstring(string, int, int) is defined in Graphics The method drawstring(string, int, int) is defined in Graphics2D Thus, the method is overridden in Graphics2D 10 5

Example There are 6 different drawimage methods in Graphics There are 2 additional drawimage methods in Graphics2D. These two have different signatures from the drawimage methods in Graphics Thus, the method is shadowed in Graphics2D How many drawimage methods are available to a Graphics2D instance? 11 9.2.1 The Substitutability Principle When a parent is expected, a child is accepted Similar to substituting man or woman in The fare is $5 per person Similar to automatic promotion of primitives. Compiler uses it in: - LHR / RHS of an assignment - Parameter passing - catch blocks, throws statements 12 9-12 6

9.2.2 Early & Late Binding How do you bind: r.m( )? 13 9-13 Early and Late Binding! We have the statement: r.m( ); # The compiler performs early binding! this means to find the target to which an identifier resolves# all identifiers must resolve (must correspond to a definition, somewhere)# Then we have the bytecode that is generated from r.m( ); The VM performs late binding! 14 7

Early Binding, Binding with Most Specific! To bind r.m( ) here s what the compiler does:# determines the declared type of r. Suppose it is C# If it cannot be found, issue r cannot be resolved to a variable. # locates the signature m( ) in C# If more than one such m is found, it binds with the "most specific" one. # Record the signature S of the found method# If no such m can be found, then issue Cannot Resolve Symbol error.# 15 Late Binding! 16 To bind the bytecode from r.m( ) bytecode, here s what the VM does:# cue up the signature S that was recorded in the bytecode# determine the referent of r some object will be located in run-time memory# if there is no such object, issue Null Pointer Exception# determine the type of r. Suppose the class is Q # look for a method with the signature S in Q# If Q is the same as C (same as declared type), then the signature S certainly will be found# If Q is the NOT the same as C, then Q MUST be a subtype of C. The signature S will be found, since it will be either inherited or will have been overridden.# 8

9.2.3 Polymorphism An invocation of an overridden method is polymorphic The meaning changes. At compilation, the binding will be to the parent s method. At runtime, the binding will be to the child class s overriding method Polymorphism leads to elegant programs. No if statements and no redundancies. 17 9.2.3 Polymorphism What if I have something like this: P r = new C(); What if I want to invoke a method on r that is present only in the child class C, such as n()? polymorphism will not be available for compilation, must use a cast (down the chain) (C) r.n() But this can lead to a runtime exception. To avert, use instanceof before casting if (r instanceof C) (C) r.n(); 18 9-18 9

Example of polymorphism import type.lib.creditcard; import type.lib.rewardcard; public class L13Ex02 { public static void main(string[] args) { } 19 } CreditCard tomsvisa; tomsvisa = new RewardCard(123458, "Tom Bentley"); tomsvisa.charge(88); System.out.println(tomsVisa.toString()); At compile time (during early binding), the variable tomsvisa resolves to CreditCard type. The compiler locates the method charge(double) within that class (binds to most specific), and records the signature charge(double) in the bytecode At runtime (during late binding), the variable tomsvisa resolves to the RewardCard type (since that is the type of the object in memory). The VM cues up the signature charge(double) and locates that method in the RewardCard class definition. Is there a danger that such a method will not be found??? convince yourself that there is no such danger 9.2.4 Abstract Classes & Interfaces Vehicle {Abstract} Car Bus «interface» HasArea + getarea() double Rectangle Cylinder Circle 20 9-20 10

Abstract Classes & Interfaces, cont. Key points to remember: How to recognize an abstract class or an interface given its API or UML diagram. Both can be used as types for declarations. An abstract class cannot be instantiated. Instead, look for a concrete class C that extends it (or for a factory method that returns an instance of C). An interface class cannot be instantiated. Instead, look for a class C that implements it. Example: the class Calendar. 21 9-21 9.3 Obligatory Inheritance 22 9-22 11

9.3 Obligatory Inheritance Option #1 Option #2 Java uses this one. Object 23 9-23 9.3.1 The Object Class Conclusion: All classes have the features present in Object (unless they overrode them). They include: - tostring() - equals() - getclass() 24 9-24 12

9.3.2 Case Study: Object Serialization Serialize = Write the state of an object to a stream 1. Create an output stream connected to a file: FileOutputStream fos; fos = new FileOutputStream(filename); 2. Create an object output stream that feeds the file output stream: ObjectOutputStream oos; oos = new ObjectOutputStream(fos); 3. Serialize an object x: oos.writeobject(x); 4. Close the stream: oos.close(); 25 9-25 Object Serialization, cont. De-serialize = Reconstruct a serialized object FileInputStream fis; fis = new FileInputStream(filename); ObjectInputStream ois; ois = new ObjectInputStream(fis); x = (cast*) ois.readobject(); ois.close(); *The cast is needed because readobject returns an Object 26 9-26 13

9.3.3 Generics " Components that take Object parameters are very flexible because they handle any type. " But this flexibility thwarts all the benefits of strong typing (casts=potential runtime errors) " The solution is a component that can take one specific type but that type is client-defined " Such generic components provides flexibility and strong typing. 27 9-27 14