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

Similar documents
Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance and object compatibility

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

Chapter 9 Inheritance

Polymorphism and Interfaces. CGS 3416 Spring 2018

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

CST242 Object-Oriented Programming Page 1

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

Software Paradigms (Lesson 3) Object-Oriented Paradigm (2)

Java Object Oriented Design. CSC207 Fall 2014

Inheritance, Polymorphism, and Interfaces

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Data Structures and Algorithms Design Goals Implementation Goals Design Principles Design Techniques. Version 03.s 2-1

Object-oriented design. More UML

Introduction to Programming Using Java (98-388)

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

Multiple Inheritance, Abstract Classes, Interfaces

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CS-202 Introduction to Object Oriented Programming

Compaq Interview Questions And Answers

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

Making New instances of Classes

Programming Languages: OO Paradigm, Polymorhism and Class Members

Inheritance and Overloading. Week 11


Chapter 10 Classes Continued. Fundamentals of Java

ECE 122. Engineering Problem Solving with Java

Polymorphism. Object Orientated Programming in Java. Benjamin Kenwright

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

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

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

[ L5P1] Object-Oriented Programming: Advanced Concepts

CO Java SE 8: Fundamentals

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CS 251 Intermediate Programming Inheritance

C18a: Abstract Class and Method

Objects and Iterators

Java How to Program, 8/e

First IS-A Relationship: Inheritance

25. Generic Programming

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

CS111: PROGRAMMING LANGUAGE II

1 Shyam sir JAVA Notes

Chapter 14 Abstract Classes and Interfaces

Check out Polymorphism from SVN. Object & Polymorphism

ITI Introduction to Computing II

Programming in C# Inheritance and Polymorphism

Object-Oriented Concepts and Principles (Adapted from Dr. Osman Balci)

1.1. Annotations History Lesson - C/C++

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Review: Object Diagrams for Inheritance. Type Conformance. Inheritance Structures. Car. Vehicle. Truck. Vehicle. conforms to Object

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Association - Direction. Association Implementation. Association Example. Unidirectional Association - Example

Object-Oriented Programming

OBJECT ORİENTATİON ENCAPSULATİON

ITI Introduction to Computing II

CSCI 131, Midterm Exam 1 Review Questions This sheet is intended to help you prepare for the first exam in this course. The following topics have

Announcements/Follow-ups

Java Application Development

Chapter 11: Inheritance

C12a: The Object Superclass and Selected Methods

Computer Science II (20073) Week 1: Review and Inheritance

Inheritance Motivation

CH. 2 OBJECT-ORIENTED PROGRAMMING

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

Inheritance and Polymorphism

Polymorphism: Inheritance Interfaces

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Chapter 11 Classes Continued

Instantiation of Template class

Techniques of Java Programming: Streams in Java

8. Polymorphism and Inheritance

COMP200 INHERITANCE. OOP using Java, from slides by Shayan Javed

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

index.pdf January 21,

Java and OOP. Part 3 Extending classes. OOP in Java : W. Milner 2005 : Slide 1

This page intentionally left blank

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

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

Abstract Classes and Polymorphism CSC 123 Fall 2018 Howard Rosenthal

ITI Introduction to Computing II

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

ITI Introduction to Computing II

Polymorphism Part 1 1

Big software. code reuse: The practice of writing program code once and using it in many contexts.

Information System Design (IT60105)

Full file at Chapter 2 - Inheritance and Exception Handling

Use the scantron sheet to enter the answer to questions (pages 1-6)

Object Orientated Analysis and Design. Benjamin Kenwright

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

First Name: AITI 2004: Exam 2 July 19, 2004

Inheritance (continued) Inheritance

CS 1316 Exam 1 Summer 2009

CSCE3193: Programming Paradigms

Java Overview An introduction to the Java Programming Language

Transcription:

Inheritance Object OutputStream write(int) write(byte[]) write(byte[], int, int) FilterOutputStream write(int) write(byte[]) write(byte[], int, int) PrintStream write(int) write(byte[])

Early binding Consider the following code snippet. OutputStream stream = System.out; stream.write(0); To which method of which class is stream.write(0) bound early?

Early binding Consider the following code snippet. OutputStream stream = System.out; stream.write(0); To which method of which class is stream.write(0) bound early? Answer write(int) of OutputStream.

Late binding Consider the following code snippet. OutputStream stream = System.out; stream.write(0); To which method of which class is stream.write(0) bound late?

Late binding Consider the following code snippet. OutputStream stream = System.out; stream.write(0); To which method of which class is stream.write(0) bound late? Answer write(int) of PrintStream.

instanceof The Boolean expression r instanceof C evaluates to true if r is not null and its type is C or any of its descendants.

Casting: at compile time Assume that the declared type of the reference r is C. Then (C )r gives rise to a compile time error if C is neither a descendant nor an ancestor of C. If (C )r does not give rise to a compile time error, then its declared type is C.

Casting: at compile time Assume that the declared type of reference card is CreditCard. Which of the following gives rise to a compile time error? 1 (RewardCard)card 2 (CreditCard)card 3 (Object)card 4 (Integer)card

Casting: at compile time Assume that the declared type of reference card is CreditCard. Which of the following gives rise to a compile time error? 1 (RewardCard)card 2 (CreditCard)card 3 (Object)card 4 (Integer)card Answer 4.

Casting: at run time (C )r gives rise to a run time error if the actual type of r is not a descendant of C.

Casting: at run time Assume that the actual type of reference card is CreditCard. Which of the following gives rise to a run time error? 1 (RewardCard)card 2 (CreditCard)card 3 (Object)card

Casting: at run time Assume that the actual type of reference card is CreditCard. Which of the following gives rise to a run time error? 1 (RewardCard)card 2 (CreditCard)card 3 (Object)card Answer 1.

instanceof and casting if (card instanceof RewardCard) {... (RewardCard) card... } (RewardCard) card is needed at compile time card instanceof RewardCard is needed at run time

Shapes Cube Ellipse Rectangle Sphere Circle Square

Collection of shapes * ShapeCollection?

Shape Shape Cube Ellipse Rectangle Sphere Circle Square

Collection of shapes ShapeCollection * Shape

Shape Can you draw a rectangle, ellipse, etc?

Shape Can you draw a rectangle, ellipse, etc? Answer Yes!

Shape Can you draw a rectangle, ellipse, etc? Answer Yes! Can you draw a shape?

Shape Can you draw a rectangle, ellipse, etc? Answer Yes! Can you draw a shape? Answer No. Shape is an abstract notion.

Shape Can you create a Rectangle object, Ellipse object, etc?

Shape Can you create a Rectangle object, Ellipse object, etc? Answer Yes!

Shape Can you create a Rectangle object, Ellipse object, etc? Answer Yes! Should one be able to create a Shape object?

Shape Can you create a Rectangle object, Ellipse object, etc? Answer Yes! Should one be able to create a Shape object? Answer No.

Abstract class An abstract class cannot be instantiated, that is, we cannot create instances of the class. An abstract class may contain methods. If one cannot create instances of a class, are its methods of any use?

Abstract class An abstract class cannot be instantiated, that is, we cannot create instances of the class. An abstract class may contain methods. If one cannot create instances of a class, are its methods of any use? Answer Yes! They can be inherited by subclasses.

Abstract class API: public abstract class Shape UML: class name in italics

Shape collection Problem Create a random collection of shapes and print the total area of all shapes combined.

Shape collection Problem Create a random collection of shapes and print the total volume of all shapes combined.

Shape Shape Cube Ellipse Rectangle Sphere Circle Square

Shape collection Only Cube and Sphere have a volume. Can we introduce an abstract class HasVolume with method getvolume() as a superclass for Cube and Sphere?

Shape collection Only Cube and Sphere have a volume. Can we introduce an abstract class HasVolume with method getvolume() as a superclass for Cube and Sphere? Answer No, because Cube and Sphere already have a superclass and Java does not support multiple inheritance.

Shape Shape Cube Ellipse Rectangle Sphere Circle Square interface HasVolume

Interface An interface only contains method headers (specifications).

Interface API: public interface HasVolume UML: interface name preceded by interface

Interface An interface specifies methods, it does not provide an implementation for them. A class C implements an interface I if C contains an implementation of each method specified in I. A class can implement multiple interfaces.

Class implements interface API: public class Cube implements HasVolume UML: dashed arrow

Another interface: Iterator Interface Iterator<E> E is a type parameter. To use the Iterator interface, you need to provide a type as argument. Iterator<Shape> iterator = collection.iterator();

To do Study the remainder of Chapter 9 of the textbook.