CS18000: Problem Solving And Object-Oriented Programming

Similar documents
CS18000: Programming I Final Exam 8 May 8am-10am Lilly Functional Abstraction. Functional Specification. CS18000: Programming I 4/28/2010

Announcements. CS18000: Problem Solving And Object-Oriented Programming

1 Method Signatures and Overloading (3 minutes, 2 points)

CS18000: Programming I

Rules and syntax for inheritance. The boring stuff

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Inheritance. Transitivity

The class Object. Lecture CS1122 Summer 2008

CS18000: Problem Solving and Object-Oriented Programming

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

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

QUIZ. How could we disable the automatic creation of copyconstructors

CS18000: Programming I

Java Classes, Inheritance, and Interfaces

Super-Classes and sub-classes

JAVA MOCK TEST JAVA MOCK TEST II

Survey #2. Programming Assignment 3. Final Exam. University of British Columbia CPSC 111, Intro to Computation Alan J. Hu.

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

QUIZ. How could we disable the automatic creation of copyconstructors

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Java Object Oriented Design. CSC207 Fall 2014

CS61BL Summer 2013 Midterm 2

Name CPTR246 Spring '17 (100 total points) Exam 3

INHERITANCE. Spring 2019

1 Inheritance (8 minutes, 9 points)

Inheritance (Part 5) Odds and ends

CS 61B, Spring 1999 MT3 Professor M. Clancy

CS18000: Problem Solving And Object-Oriented Programming

CS 1316 Exam 2 Summer 2009

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

CS 61B Data Structures and Programming Methodology. July 2, 2008 David Sun

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Abstract Classes and Interfaces

Inheritance (Outsource: )

Prelim 1 Solutions. CS 2110, March 10, 2015, 5:30 PM Total Question True False. Loop Invariants Max Score Grader

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

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

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Logistics. Final Exam on Friday at 3pm in CHEM 102

INTERFACES IN JAVA. Prof. Chris Jermaine

ECE 122. Engineering Problem Solving with Java

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

CS 251 Intermediate Programming Inheritance

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

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

1B1b Inheritance. Inheritance. Agenda. Subclass and Superclass. Superclass. Generalisation & Specialisation. Shapes and Squares. 1B1b Lecture Slides

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

Basic Object-Oriented Concepts. 5-Oct-17

Introduction to Inheritance

What is Inheritance?

CS260 Intro to Java & Android 03.Java Language Basics

1B1b Classes in Java Part I

Midterm Exam 2 CS 455, Spring 2011

Programming Languages and Techniques (CIS120)

Definition of DJ (Diminished Java)

Lab 4: Super Sudoku Solver CSCI 2101 Fall 2017

CS-140 Fall 2017 Test 2 Version A Nov. 29, 2017

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

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

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Basic Class Diagrams. Class Diagrams, cont d. Class Diagrams, cont d. Car. Car. Car. What does the minus sign here mean?

Goals for Today. CSE1030 Introduction to Computer Science II. CSE1030 Lecture #9. Review is-a versus has-a. Lecture #9 Inheritance I

Problem 1: Building and testing your own linked indexed list

CSE 214 Computer Science II Heaps and Priority Queues

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

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

Programming Exercise 14: Inheritance and Polymorphism

Note that if both p1 and p2 are null, equals returns true.

Midterm Exam CS 251, Intermediate Programming March 6, 2015

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Introduction to Programming Using Java (98-388)

CS 231 Data Structures and Algorithms, Fall 2016

Week 8 Lecture: Programming

CMSC 341 Lecture 7 Lists

Lecture 5: Implementing Lists, Version 1

CMSC 341 Lecture 10 Binary Search Trees

Prelim 1. CS 2110, March 15, 2016, 5:30 PM Total Question Name True False. Short Answer

Computer Science 210 Data Structures Siena College Fall Topic Notes: Trees

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

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

Final Exam CS 251, Intermediate Programming December 13, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

What is software testing? Software testing is designing, executing and evaluating test cases in order to detect faults.

Today. Book-keeping. Inheritance. Subscribe to sipb-iap-java-students. Slides and code at Interfaces.

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

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

CMSC202 Computer Science II for Majors

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

CSE 115. Introduction to Computer Science I

Day 3. COMP 1006/1406A Summer M. Jason Hinek Carleton University

CSE 113. Announcements

CLASS DESIGN. Objectives MODULE 4

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

Lab 4. Out: Friday, February 25th, 2005

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS11 Introduction to C++ Fall Lecture 7

First Examination. CS 225 Data Structures and Software Principles Spring p-9p, Tuesday, February 19

Transcription:

CS18000: Problem Solving And Object-Oriented Programming Data Abstraction: Inheritance 7 March 2011 Prof. Chris Clifton Data Abstraction Continued Abstract data type provides Well-defined interface Separation of specification and representation Ability to use (what) without worrying about how But what if an existing Abstract Data Type isn t enough? Do we have to start from scratch? CS18000 2 2011 Chris Clifton, Aditya P. Mathur 1

Solution 1: Extend the Abstraction We saw this last Wednesday Abstraction didn t have isfull() Implementation did Abstraction specifies what is necessary not everything possible But what if we like the existing implementation? It just doesn t go far enough CS18000 3 Solution 2: Use the abstraction We can use one abstraction when creating an instance of another Even if both implement the same abstraction Nothing special about this Example: Paired Queue StupidQueue runs out Solution: When full, use another StupidQueue CS18000 4 2011 Chris Clifton, Aditya P. Mathur 2

PairedQueue class PairedQueue implements Queue { StupidQueue first, second; public PairedQueue () { first = new StupidQueue(); second = null; public String dequeue () { String result = first.dequeue(); if (first.isempty() && second!= null) { first = second; second = null; return result; public void enqueue ( String item ) { if ( first.isfull() ) { if ( second == null ) second = new StupidQueue(); second.enqueue(item); else { first.enqueue(item); public boolean isempty() { return first.isempty(); Why does PairedQueue enqueue() must check implement if second Queue is null? to A. Can't use create a StupidQueue: a new StupidQueue if second already A. True holds one B. If not B. null, False we ve already created the second one C. There can't be more than one instance of the class StupidQueue at a time CS18000 5 Solution 3: Extend the implementation Inheritance: Define a new class that includes an existing class 1. Same interface (just like using an Interface) 2. Same methods 3. Same representation Three separate concepts But unfortunately not independent CS18000 6 2011 Chris Clifton, Aditya P. Mathur 3

public class Gate { String name; public Gate ( String name ) { this.name = name; public String getname() { return name; public boolean getvalue() { return false; Running Example: Boolean Circuits CS18000 7 Extending the Interface Class UnaryOperator extends Gate { public UnaryOperator(String name) { public void setinput(gate input) { public Gate getinput() { CS18000 8 2011 Chris Clifton, Aditya P. Mathur 4

Extending the Interface Child class must support all features of parent Just like implementing an interface Key difference: Some features already implemented Can be used anywhere parent class can be used Just like an interface Can define new fields and methods But can t be used if the type is the parent class Just like an interface CS18000 9 Extending the Methods Class Not extends UnaryOperator{ public Not() { super( Not ); public boolean getvalue() { return!(getinput().getvalue()); CS18000 10 2011 Chris Clifton, Aditya P. Mathur 5

Extending the Methods Overriding methods If instance of child, use child (even if you think you have parent) Gate g = new Not(); g.getvalue() will use method from Not class Caveat: Class methods (static) not overridden The one you get depends on the type of object CS18000 11 Using methods from Parent Non-overridden methods easy Just use example in Not Overridden methods accessed using super keyword kind of like this Constructor of parent must be called in constructor Exception: Default parent constructor (no arguments) called automatically before child constructor if you don t CS18000 12 2011 Chris Clifton, Aditya P. Mathur 6

Extending the Representation Child class gets all fields of parent class Can access those not declared private or protected Best to access through parent methods Can add new fields Also hide existing by using same name Bad idea works like class methods: Which you get depends on type CS18000 13 Extending the Interface Class UnaryOperator extends Gate { private Gate input; public UnaryOperator(String name) { super (name); public void setinput(gate input) { this.input = input; public Gate getinput() { return input; CS18000 14 2011 Chris Clifton, Aditya P. Mathur 7

CS18000: Problem Solving And Object-Oriented Programming Data Abstraction: Generics 9 March 2011 Prof. Chris Clifton Announcements: Exam 1 scores and solution set out I ll send more on how to interpret what you see once Project 2 scores are posted Don t think too much about your score until you see this email No more labs this week Project 3 do soon CS18000 16 2011 Chris Clifton, Aditya P. Mathur 8

Putting it all together: Inheritance Three concepts: 1. Inherit the interface 2. Inherit the methods 3. Inherit the representation Use inheritance (extends) when you want all three If you just want interface, use interface/implements If you just want methods, create as an instance variable CS18000 17 Getting it Right: ISA UnaryOperator ISA Gate Not ISA UnaryOperator GroceryLine ISA Queue Gate UnaryOperator BinaryOperator Not And Or CS18000 18 2011 Chris Clifton, Aditya P. Mathur 9

Three concepts: 1. Inherit the interface Putting it all together: Inheritance 2. Inherit the methods 3. Inherit the representation Today: Mechanics and Caveats How to use inheritance How to misuse inheritance CS18000 19 Idea 1: Overriding Two implementations power(binaryoperator g) { /* high power */ power(unaryoperator g) { /* low power */ Gate testg = new UnaryOperator(); power(testg); Will this work? Unfortunately, no A: Yes B: No C: Can t Tell Call based on (declared) type of argument There is no power(gate g) CS18000 22 2011 Chris Clifton, Aditya P. Mathur 10

Idea 2: Cast Two implementations power(binaryoperator g) { /* high power */ power(unaryoperator g) { /* low power */ Gate g = new UnaryOperator(); power((unaryoperator) g); Will this work? Close, but not quite What if g was a BinaryOperator? CS18000 23 Solution: Ask an object it s Class Class getclass() defined for all objects Every class (automatically) inherits from Class object Class has several useful methods String getname(); Class getsuperclass(); boolean isinstance(object obj); if(g.getclass().getname().equals( UnaryOperator )) power( (UnaryOperator) g ); CS18000 24 2011 Chris Clifton, Aditya P. Mathur 11

Caveats getclass() can be used as a crutch for bad design It should be used only when you can t find a better way Try to compare Class objects, not names In a large system, same name can be used multiple places (class defined inside a class) CS18000 25 Solution: Generics What if we want to support many types? But only one at a time Generics: Parameter attached to a type Queue<Integer>, Queue<String> instead of IntegerQueue, StringQueue One class (Messy) alternative: ObjectQueue use getclass() to enforce single type CS18000 26 2011 Chris Clifton, Aditya P. Mathur 12

Example: Typed Set Set of objects All must be of the same type But implement once Use: Set<Integer> s; Will only take integers Code example: interface Set<E> class SearchTree<E> extends Set CS18000 27 2011 Chris Clifton, Aditya P. Mathur 13