March 2004Object Oriented Design1 Object-Oriented Design.

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Data Structures Lecture 2 Fang Yu Department of Management Information Systems National Chengchi University Fall 2011.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
July Overview1 COMP 2011 Data Organisation 2004 Session 2 Lecturer: Alan Blair Head Tutor: Loc Huynh.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
S Overview1 COMP 2011/2711 Data Organisation 2006 Session 1 Lecturer: Norman Foo Head Tutor: Loc Huynh; others to be nominated.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
UCD Computer Science COMP Object-oriented design Learning the basic building blocks of programming is straightforward... On the other hand.... Designing/implementing.
Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 10 Inheritance, Polymorphism, and Scope. 2 Knowledge Goals Understand the hierarchical nature of classes in object-oriented programming Understand.
1 Introduction to CS Agenda Syllabus Schedule Lecture: the management of complexity.
Introduction to Software Design Chapter 1. Chapter 1: Introduction to Software Design2 Chapter Objectives Intro - Software OOP Inheritance, interfaces,
Object-Oriented Design Running Time Recursion - Ed. 2 and 3.: Chapter 2, 3 - Ed. 4: Chapter 2, 3, 4.
Principles of Computer Programming (using Java) Review Haidong Xue Summer 2011, at GSU.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Inheritance using Java
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
Object Oriented Design. OOP Object Oriented programming (OOP) is the idea of developing programs by defining objects that interact with each other Java.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Lecture objectives  Differences between Java and C#  Understand and use abstract classes  Casting in Java: why is it important?  Master exceptions.
Week 1 - Friday.  What did we talk about last time?  Basic programming model  Other Java stuff  References  Static  Inner classes  Exceptions.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
Chap. 2. Object-Oriented Design. Inheritance Building ApartmentHouse Commercial Building Low-rise Apartment High-rise Apartment Two-story House RanchSkyscraper.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Lecture 20 April 13, Review for Exam All Exercises and Sample Code from Lectures Object Based Programming Object Oriented Programming String and.
1 Some uses of Inheritance: A Review CSC 335 Object-Oriented Programming and Design ©Rick Mercer.
1 Review of Java Basic Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – conditional statements –Flow of Control.
1 COSC2007 Data Structures II Chapter 9 Class Relationships.
Object-Oriented Programming © 2013 Goodrich, Tamassia, Goldwasser1Object-Oriented Programming.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
1 A few uses of Inheritance in Java CSC 335 Object-Oriented Programming and Design ©Rick Mercer.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
AD Lecture #1 Object Oriented Programming Three Main Principles 1 Inheritance Encapsulation Polymorphism.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Basic Object-Oriented concepts. Concept: Classes describe objects Every object belongs to (is an instance of) a class An object may have fields –The class.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures and Algorithms in JAVA Chapter 2.
Modern Programming Tools And Techniques-I
Object-Oriented Concepts
Object-Oriented Programming
Interface, Subclass, and Abstract Class Review
Inheritance and Polymorphism
COMP 2011 Data Organisation 2005 Session 1
Object-Oriented Programming
Computer Science II Exam 1 Review.
null, true, and false are also reserved.
Java Programming Language
Object-Oriented Programming
Exceptions.
Exceptions.
Presentation transcript:

March 2004Object Oriented Design1 Object-Oriented Design

March 2004Object Oriented Design2 Goals Robustness Adaptability Flexible code reuse Principles Abstraction Encapsulation Modularity

March 2004Object Oriented Design3 Class Hierarchy Double Number Integer Object Reader InputStreamReader FileReader String

March 2004Object Oriented Design4 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 2004Object Oriented Design5 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(); }

March 2004Object Oriented Design6 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 2004Object Oriented Design7 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 2004Object Oriented Design8 Test Progression class Tester { public static void main( String[] args ) { Progression prog; prog = new ArithProgression();// default increment prog.printProgression( 10 ); prog = new ArithProgression( 5 );// increment 5 prog.printProgression( 10 ); prog = new FibonacciProgression(); // default prog.printProgression( 10 ); prog = new FibonacciProgression( 4, 6 ); prog.printProgression( 10 ); }

March 2004Object Oriented Design9 Test Progression ArithProgression(); // default increment ArithProgression( 5 );// increment FibonacciProgression(); // default FibonacciProgression( 4, 6 );

March 2004Object Oriented Design10 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 2004Object Oriented Design11 Exception Hierarchy Error Throwable Exception RuntimeException Object

March 2004Object Oriented Design12 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 2004Object Oriented Design13 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

March 2004Object Oriented Design14 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 2004Object Oriented Design15 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 ); }

March 2004Object Oriented Design16 Javadoc /** * Compute the sigmoid function of a given input * x Any real number y The sigmoid function of x */ Public double sigmoid( double x ) { if( x >= 0.0 ) return 1.0 / ( Math.exp( -x )); else return sigmoid( -x ); }

March 2004Object Oriented Design17 Design Patterns Position Iterator Template method Composition Comparator Locator Decorator