Data Structures and Object-Oriented Design V. Spring 2014 Carola Wenk

Similar documents
Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

CSCI 253. Object Oriented Programming (OOP) Overview. George Blankenship 1. Object Oriented Design: Java Review OOP George Blankenship.

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

Data Structures in the Java API

Java Interfaces. Recall: A List Interface. Another Java Interface Example. Interface Notes. Why an interface construct? Interfaces & Java Types

Java Programming Language

Java Interview Questions and Answers

CMSC 202H. ArrayList, Multidimensional Arrays

Habanero Extreme Scale Software Research Project

Data Structures and Algorithms

Class 32: The Java Collections Framework

Chapter 13 - Inheritance

CS 111 Classes I 1. Software Organization View to this point:

Structural Design Patterns Used in Data Structures Implementation

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Java Application Developer Certificate Program Competencies

1.00 Lecture 35. Data Structures: Introduction Stacks, Queues. Reading for next time: Big Java: Data Structures

1. What are Data Structures? Introduction to Data Structures. 2. What will we Study? CITS2200 Data Structures and Algorithms

Object Oriented Software Design II

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Class 16: Function Parameters and Polymorphism

7.1 Our Current Model

CS193j, Stanford Handout #10 OOP 3

Concepts and terminology in the Simula Programming Language

C++ Overloading, Constructors, Assignment operator

Exception Handling. Overloaded methods Interfaces Inheritance hierarchies Constructors. OOP: Exception Handling 1

Datastrukturer och standardalgoritmer i Java

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

Description of Class Mutation Mutation Operators for Java

The Smalltalk Programming Language. Beatrice Åkerblom

Abstract Class & Java Interface

Java EE Web Development Course Program

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

LINKED DATA STRUCTURES

Introduction: Abstract Data Types and Java Review

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Java Programming. Binnur Kurt Istanbul Technical University Computer Engineering Department. Java Programming. Version 0.0.

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

Chapter 1 Fundamentals of Java Programming

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Polymorphism. Why use polymorphism Upcast revisited (and downcast) Static and dynamic type Dynamic binding. Polymorphism.

Java (12 Weeks) Introduction to Java Programming Language

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

CmpSci 187: Programming with Data Structures Spring 2015

Agenda. What is and Why Polymorphism? Examples of Polymorphism in Java programs 3 forms of Polymorphism

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Generic Types in Java. Example. Another Example. Type Casting. An Aside: Autoboxing 4/16/2013 GENERIC TYPES AND THE JAVA COLLECTIONS FRAMEWORK.

Programming Languages

Part I. Multiple Choice Questions (2 points each):

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

AP Computer Science Java Subset

Object Oriented Software Design

11 November

El Dorado Union High School District Educational Services

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Entity-Relationship Model. Purpose of E/R Model. Entity Sets

Collaboration policy. Where to get help (but no code in ) Office hours Lab TAs in Friend 008/009 Bounce ideas (but not code) off classmates

Java the UML Way: Integrating Object-Oriented Design and Programming

Contents. 9-1 Copyright (c) N. Afshartous

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Object-Oriented Programming in Java

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Programming to Interfaces

Formal Engineering for Industrial Software Development

Android Application Development Course Program

Semantic Analysis: Types and Type Checking

University of Twente. A simulation of the Java Virtual Machine using graph grammars

1. Polymorphism in C++...2

7. Object-Oriented Programming

Dsc+Mock: A Test Case + Mock Class Generator in Support of Coding Against Interfaces

iphone SDK Enrolled students will be invited to developer program Login to Program Portal Request a Certificate Download and install the SDK

Stack Allocation. Run-Time Data Structures. Static Structures

SE 360 Advances in Software Development Object Oriented Development in Java. Polymorphism. Dr. Senem Kumova Metin

Programmation 2. Introduction à la programmation Java

Getting Started with the Internet Communications Engine

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Common Data Structures

Software Engineering Techniques

The Interface Concept

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

Software Engineering II. Java 2 Collections. Bartosz Walter <Bartek.Walter@man.poznan.pl>

Building Java Programs

D06 PROGRAMMING with JAVA

The C Programming Language course syllabus associate level

Computing Concepts with Java Essentials

Working with Java Collections

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Assignment 3 Version 2.0 Reactive NoSQL Due April 13

How to create/avoid memory leak in Java and.net? Venkat Subramaniam

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

Programming Languages Featherweight Java David Walker

JAVA - INHERITANCE. extends is the keyword used to inherit the properties of a class. Below given is the syntax of extends keyword.

Outline. The Stack ADT Applications of Stacks Array-based implementation Growable array-based stack. Stacks 2

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Transcription:

Data Structures and Object-Oriented Design V Spring 2014 Carola Wenk

Other Data Types/Structures We ve seen that the actual implementation of the data type only matters in the overall performance (and possibly functionality). class BinarySearchTree { public BinarySearchTree() { What operations did binary search trees offer us, and how did it differ in implementation or functionality?

Other Data Types/Structures We ve seen that the actual implementation of the data type only matters in the overall performance (and possibly functionality). class BinarySearchTree { public BinarySearchTree() { public void add(int x { public void remove(int x) { public boolean find(int x) { Do we even need to name this class to refer to its data structure?

Other Data Types/Structures We ve seen that the actual implementation of the data type only matters in the overall performance (and possibly functionality). class OrderedCollection { public OrderedCollection() { public void add(int x { public void remove(int x) { public boolean find(int x) { Do we even need to name this class to refer to its data structure? Not really - the user doesn t need to know how the data is organized.

What about Type Compatibility? So far, our class definitions have been defined to manipulate a single type (usually int). Do we really have to define a different class for a stack of strings? Can we define a general-purpose stack? class intstack { private int[] S = null; private int top; public Stack(int capacity) { S = new int[capacity]; top = capacity; public int pop() { return S[top++]; public void push(int x) { S[--top] = x; class StringStack { private String[] S = null; private int top; public Stack(int capacity) { S = new String[capacity]; top = capacity; public String pop() { return S[top++]; public void push(string x) { S[--top] = x;

Object-Oriented Design In Java, everything is an object and different classes can be defined to be compatible according to functionality. class A { class B extends A {

Object-Oriented Design The best way to think of type compatibility is that it is always acceptable to extend functionality, but never ok to remove it. class B extends A { public void g() { class A { public void f() { A x = new A(); B y = new B(); x.f(); y.f(); y.g(); x = new B(); // not allowed! x.g(); x.f();

Object-Oriented Design Java s type checking is simple: a reference must hold at least as much functionality as it was declared to (more is ok). class B extends A { public void g() { class A { public void f() { A x = new A(); B y = new B(); x.f(); y.f(); y.g(); x = new B(); // not allowed! x.g(); x.f();

Rules of Inheritance class A { public void f(int x) { System.out.println(x); class B extends A { public void f(int x) { super.f(x) public void g() { The class extending functionality is called a subclass, and the class being extended is called the superclass. We can access inherited attributes using the super keyword.

Rules of Inheritance class A { public void f(int x) { System.out.println(x); class B extends A { public void f(int x) { super.f(2*x) public void g() { The class extending functionality is called a subclass, and the class being extended is called the superclass. We can access inherited attributes using the super keyword.

Rules of Inheritance class A { protected int a; public A(int x) { a = x; public void f(int x) { System.out.println(x); class B extends A { private double b; public B(int x, double y) { super(x); b = y; public void f(int x) { super.f(2*x); public void g() { The class extending functionality is called a subclass, and the class being extended is called the superclass. We can access inherited attributes using the super keyword.

Object-Oriented Design The fancy name for how references in Java work is type polymorphism. class B extends A { public void g() { class A { public void f() { A x = new A(1); B y = new B(1, 2.0); x.f(1); y.f(1); y.g(); x = new B(); x.g(); x.f();

Object-Oriented Design These restrictions on references allow us to check for type violations at compile-time - why is this important? class B extends A { public void g() { class A { public void f() { A x = new A(1); B y = new B(1, 2.0); x.f(1); y.f(1); y.g(); x = new B(); x.g(); x.f();

protected access Any attributes that are declared protected are accessible by subclasses, but not the outside world. class B extends A { public void g() { class A { protected void f() { A x = new A(); B y = new B(); x.f(); // not allowed! y.f(); y.g(); x = new B(); x.g();

Java Access Rules class Package Subclass World private <none> protected public

Big Picture We can be flexible about how we assign objects, as long as these assignments respect the defined hierarchy of compatibility: A B

Big Picture We can be flexible about how we assign objects, as long as these assignments respect the defined hierarchy of compatibility: A B compatible with type A compatible with type B

Big Picture We can be flexible about how we assign objects, as long as these assignments respect the defined hierarchy of compatibility: compatible with type A compatible with type A and type B B A

Using Inheritance Note that references are essentially unidirectional. How general-purpose can we make types using Java s object model? class Stack { private Object[] S = null; private int top; public Stack(int capacity) { S = new Object[capacity]; top = capacity; public Object pop() { return S[top++]; public void push(object x) { S[--top] = x;

Limitations Inheritance is useful for extending functionality, but it can t do everything. By defining Stack to hold Objects, we lose functionality when we remove things from the stack: Stack S = new Stack(10); S.push(new Integer(15)); S.push(new String( foo )); // this is the only legal way to // retrieve items - why? Object a = S.pop(); Object b = S.pop(); // what are the types of a and b?

Type Casting Java actually allows us to regain functionality by casting the returned Object into the correct type. This helps us use one class declaration to create different kinds of Stacks, but does not allow a heterogeneous Stack. Stack S = new Stack(10); S.push(new Integer(15)); S.push(new String( foo )); // this is the only legal way to // retrieve items - why? Integer a = (Integer) S.pop(); String b = (String) S.pop(); // what are the types of a and b?

Java Generics Java also provides a mechanism to make classes generic, which avoids the need for casting: class MyClass<T> { private T member_variable; public T foo(t x) { This way, we can use the same class definition for multiple types (without losing functionality), and errors in type usage can still be caught at compile-time.

Java Generics Java also provides a mechanism to make classes generic, which avoids the need for casting: class MyClass<T> { private T member_variable; public T foo(t x) { Given the way Java expects us to declare everything up front - is there a potential problem with using generic types?

Another Problem Specialized classes can implement similar functionality - but our rules (so far) for references don t allow us to refer to such instances interchangeably. A B B C C C What if these classes implement some similar functionality?

Java Interfaces We can specify that a Java class implements a particular kind of functionality defined as an interface. interface Collection { boolean add(object o); boolean remove(object o); boolean contains(object o); boolean equals(object o); class Foo implements Collection { class Bar implements Collection { Foo X = new Foo(); Bar Y = new Bar(); Collection C; C = X; C = Y; Interfaces in Java can be extended like classes, and follow the same inheritance rules.

Recap: Object-Oriented Design In Java, everything is an Object - what does this mean? What are the rules of inheritance for class attributes? What are the rules for declaring and using references to class instances? What are the differences between generic types and polymorphic types? What gap in the object-oriented paradigm do interfaces help address?