Object Oriented Design. Object-Oriented Design. Inheritance & Polymorphism. Class Hierarchy. Goals Robustness Adaptability Flexible code reuse

Similar documents
Object-Oriented Design. March 2005 Object Oriented Design 1

Data Structures Lecture 2

Full file at Chapter 2 - Inheritance and Exception Handling

Object-Oriented Programming

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

CO Java SE 8: Fundamentals

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Special error return Constructors do not have a return value What if method uses the full range of the return type?

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

CS 11 java track: lecture 3

What can go wrong in a Java program while running?

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Programming II (CS300)

16-Dec-10. Consider the following method:

Polymorphism. return a.doublevalue() + b.doublevalue();

Tutorial 8 Date: 15/04/2014

JAC444 - Lecture 4. Segment 1 - Exception. Jordan Anastasiade Java Programming Language Course

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

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

CH. 2 OBJECT-ORIENTED PROGRAMMING

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

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

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Programming overview

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

15CS45 : OBJECT ORIENTED CONCEPTS

Fundamentals of Object Oriented Programming

Introduction Unit 4: Input, output and exceptions

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

COMP1008 Exceptions. Runtime Error

Outline. Object-Oriented Design Principles. Object-Oriented Design Goals. What a Subclass Inherits from Its Superclass?

CS 61B Discussion 5: Inheritance II Fall 2014

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

Exceptions - Example. Exceptions - Example

Practice for Chapter 11

Input from Files. Buffered Reader

Exception-Handling Overview


National University. Faculty of Computer Since and Technology Object Oriented Programming

Introduction to Programming Using Java (98-388)

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

More on Objects in JAVA TM

More on Exception Handling

BBM 102 Introduction to Programming II Spring Exceptions

Index COPYRIGHTED MATERIAL

CS365 Midterm -- Spring 2019

IT101. File Input and Output

CMSC 331 Second Midterm Exam

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

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

Object-Oriented Programming

Types, Values and Variables (Chapter 4, JLS)

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

WA1278 Introduction to Java Using Eclipse

Abstract Classes and Interfaces

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

APCS Unit 5 Exam. Assuming all four classes have a default constructor, which of the following statements would result in an error from the compiler?

Chapter 5 Object-Oriented Programming

Sri Vidya College of Engineering & Technology Question Bank

Class, Variable, Constructor, Object, Method Questions

Introduction to Java. Handout-3a. cs402 - Spring

Implements vs. Extends When Defining a Class

1 Shyam sir JAVA Notes

Inheritance and Polymorphism

Recitation 3. 2D Arrays, Exceptions

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Solutions Manual. Data Structures and Algorithms in Java, 5th edition International Student Version. M. T. Goodrich and R.

Exceptions. CSC207 Winter 2017

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

FILE I/O IN JAVA. Prof. Chris Jermaine

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Here is a hierarchy of classes to deal with Input and Output streams.

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

CS260 Intro to Java & Android 03.Java Language Basics

1Z0-808 oracle. Number: 1Z0-808 Passing Score: 800 Time Limit: 120 min.

Object Oriented Programming. Java-Lecture 11 Polymorphism

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Multiple Inheritance, Abstract Classes, Interfaces

CMSC 132: Object-Oriented Programming II

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

C++ Important Questions with Answers

Question 1. (2 points) What is the difference between a stream and a file?

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Exceptions. Examples of code which shows the syntax and all that

PROGRAMMING LANGUAGE 2

Programming II (CS300)

More on Exception Handling

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

Transcription:

Object-Oriented Design Object Oriented Design Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity March 2005 Object Oriented Design 1 March 2005 Object Oriented Design 2 Class Hierarchy Object Reader String InputStreamReader FileReader Number Integer Double Inheritance & Polymorphism subclass extends superclass the subclass specialises, and inherits from, the superclass field or method in subclass can override field or method in superclass the appropriate method is found by means of dynamic dispatch keywords: this & super March 2005 Object Oriented Design 3 March 2005 Object Oriented Design 4

Example: Progression class public class Progression { protected long first, cur; Progression() { cur = first = 0; protected long firstvalue() { cur = first; return cur; protected long nextvalue() { return ++cur; public void printprogression( int n ) { System.out.println( firstvalue()); for( int i = 2; i <= n; i++ ) { System.out.print( + nextvalue()); System.out.println(); ArithProgression subclass class ArithProgression extends Progression { protected long inc; // increment // parameter inc masks or overrides field inc ArithProgression( long inc ) {this.inc = inc; ArithProgression() {this( 1 ); // calls other constructor // this method overrides Progression.nextValue() protected long nextvalue() { cur += inc; return cur; // inherits methods firstvalue() and printprogression() March 2005 Object Oriented Design 5 March 2005 Object Oriented Design 6 FibonacciProgression class FibonacciProgression extends Progression { long prev; FibonacciProgression( long value1, long value2 ) { first = value1; prev = value2 - value1; FibonacciProgression() { this( 0, 1 ); protected long nextvalue() { long temp = prev; prev = cur; cur += temp; return cur; March 2005 Object Oriented Design 7 Test Progression class Tester { public static void main( String[] args ) { Progression prog; prog = new ArithProgression();//default increment prog = new ArithProgression( 5 );// increment 5 prog = new FibonacciProgression(); // default prog = new FibonacciProgression( 4, 6 ); March 2005 Object Oriented Design 8

Test Progression ArithProgression(); //default increment 0 1 2 3 4 5 6 7 8 9 ArithProgression( 5 );// increment 5 0 5 10 15 20 25 30 35 40 45 FibonacciProgression(); // default 0 1 1 2 3 5 8 13 21 34 FibonacciProgression( 4, 6 ); 4 6 10 16 26 42 68 110 178 288 Exceptions when code encounters unexpected conditions, it throws an exception other code must then catch the exception when an exception is thrown in a method, the method may either catch the exception or throw it to the calling method Exceptions are in a hierarchy (and you can define your own) Exceptions other than RuntimeExceptions must be caught March 2005 Object Oriented Design 9 March 2005 Object Oriented Design 10 Exception Hierarchy Object Throwable Exception Error RuntimeException March 2005 Object Oriented Design 11 Copy.java try { filein = new BufferedReader(new FileReader(source)); fileout = new PrintWriter( new FileWriter( dest )); String oneline; while(( oneline = filein.readline())!= null ) { fileout.println( oneline ); filein.close(); fileout.close(); catch( IOException e ) { System.out.print( "Exception: " + e ); System.exit( 1 ); March 2005 Object Oriented Design 12

Interfaces An interface is a collection of method declarations with no data and no code When a class implements an interface, it must implement all methods declared in the interface Each class extends (directly) only one other class, but may implement more than one interface Abstract Classes an abstract class is a class containing empty method declarations abstract class cannot be instantiated subclass of an abstract class must provide implementations for all the abstract methods of the superclass March 2005 Object Oriented Design 13 March 2005 Object Oriented Design 14 Casting and instanceof void printprogression( Progression prog ) {... if( prog instanceof ArithProgression ) { System.out.println( int = + ((ArithProgression) prog ).int ); if( prog instanceof FibonacciProgression ) { System.out.println( prev = + ((FibonacciProgression) prog ).prev ); Javadoc /** * Compute the sigmoid function of a given input * * @param x Any real number * @return y The sigmoid function of x */ Public double sigmoid( double x ) { if( x >= 0.0 ) return 1.0 / ( 1.0 + Math.exp( -x )); else return 1.0 - sigmoid( -x ); March 2005 Object Oriented Design 15 March 2005 Object Oriented Design 16

Design Patterns Position Iterator Template method Composition Comparator Locator Decorator March 2005 Object Oriented Design 17