Highlights of Last Week

Similar documents
Highlights of Previous Lecture

Highlights of Last Week

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

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

More Relationships Between Classes

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Inheritance and Polymorphism

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

Inheritance and Interfaces

COMP 110/L Lecture 19. Kyle Dewey

Java Object Oriented Design. CSC207 Fall 2014

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

CS-202 Introduction to Object Oriented Programming

Programming Languages and Techniques (CIS120)

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

COMP 250 Winter 2011 Reading: Java background January 5, 2011

Object Oriented Programming. Java-Lecture 11 Polymorphism

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

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

First IS-A Relationship: Inheritance

Chapter 14 Abstract Classes and Interfaces

Java Fundamentals (II)

MORE OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 4 09/01/2011

JAVA: A Primer. By: Amrita Rajagopal

More OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 4 09/11/2012

Polymorphism. Agenda

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

Inheritance, polymorphism, interfaces

Chapter 5. Inheritance

Building custom components IAT351

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

15: Polymorphism & Virtual Functions

Highlights of Previous Lecture

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

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

Objects. Prof. Clarkson Fall ob-ject: to feel distaste for something Webster's Dictionary. Today s music: Kung Fu Fighting by CeeLo Green

C++ Important Questions with Answers

Basic Principles of OO. Example: Ice/Water Dispenser. Systems Thinking. Interfaces: Describing Behavior. People's Roles wrt Systems

Java Programming Lecture 7

Inheritance (continued) Inheritance

Inheritance & Polymorphism Recap. Inheritance & Polymorphism 1

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

C++ Crash Kurs. Polymorphism. Dr. Dennis Pfisterer Institut für Telematik, Universität zu Lübeck

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Equality for Abstract Data Types

CISC 3115 Modern Programming Techniques Spring 2018 Section TY3 Exam 2 Solutions

ob-ject: to feel distaste for something Webster's Dictionary

CS 61B Discussion 5: Inheritance II Fall 2014

Programming in C# Inheritance and Polymorphism

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

Topic 7: Inheritance. Reading: JBD Sections CMPS 12A Winter 2009 UCSC

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

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

Relationships Between Real Things CSE 143. Common Relationship Patterns. Employee. Supervisor

Example: Count of Points

What is Inheritance?

CS1004: Intro to CS in Java, Spring 2005

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

Administration. Classes. Objects Part II. Agenda. Review: Object References. Object Aliases. CS 99 Summer 2000 Michael Clarkson Lecture 7

COP 3330 Final Exam Review

Relationships Between Real Things. CSE 143 Java. Common Relationship Patterns. Composition: "has a" CSE143 Sp Student.

Polymorphism Part 1 1

Inheritance & Polymorphism

Why use inheritance? The most important slide of the lecture. Programming in C++ Reasons for Inheritance (revision) Inheritance in C++

Relationships Between Real Things CSC 143. Common Relationship Patterns. Composition: "has a" CSC Employee. Supervisor

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

Binghamton University. CS-140 Fall Dynamic Types

Inheritance, Polymorphism, and Interfaces

PRINCIPLES OF SOFTWARE BIM209DESIGN AND DEVELOPMENT 00. WELCOME TO OBJECTVILLE. Speaking the Language of OO

CONSTRUCTOR & Description. String() This initializes a newly created String object so that it represents an empty character sequence.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space.

CS107 Handout 37 Spring 2007 May 25, 2007 Introduction to Inheritance

Programming in Java, 2e Sachin Malhotra Saurabh Choudhary

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

CS1004: Intro to CS in Java, Spring 2005

CS105 C++ Lecture 7. More on Classes, Inheritance

CSE 143 Lecture 12 Inheritance

Programming Exercise 14: Inheritance and Polymorphism

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

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

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

Inheritance. OOP components. Another Example. Is a Vs Has a. Virtual Destructor rule. Virtual Functions 4/13/2017

Rules and syntax for inheritance. The boring stuff

Strict Inheritance. Object-Oriented Programming Winter

CS 251 Intermediate Programming Inheritance

Inheritance -- Introduction

Lecture 2 and 3: Fundamental Object-Oriented Concepts Kenneth M. Anderson

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

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

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

COMP 250 Fall inheritance Nov. 17, 2017

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

Inheritance & Polymorphism. Object-Oriented Programming Spring 2015

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

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

CS445 Week 9: Lecture

Transcription:

Highlights of Last Week Using Collection classes Marking methods protected to permit access only from derived classes Marking classes abstract to force inheritance Marking classes final to prevent inheritance Marking methods final to prevent derived classes from over-riding required behavior Using static fields to maintain information that is common to all objects of a class 1

Inheritance vs. Composition Use inheritance to model IS-A-KIND-OF relationships. Use composition to model HAS-A relationships. When in doubt, try forming a sentence: A Car IS-A-KIND-OF Vehicle - sounds right A Triangle IS-A-KIND-OF Shape - sounds right Inheritance relationship is natural A Car IS-A-KIND-OF Engine - doesn t sound correct A Triangle IS-A_KIND-OF Vertex - bzzt Inheritance relationship isn t natural A Car HAS-A Engine - sounds right A Triangle HAS-A Vertex - yup Composition is the right approach 2

Guidelines - Inheritance Derived classes inherit the interface and, by default, the implementation of their base class(es). A derived class should have some kind of specialized behavior (it should provide the same services as the superclass, only some, at least, are provided differently) A derived class may also offer new services not offered by the base class A derived class should not try to remove services offered by the base class Do not use inheritance unless the IS-A-KIND-OF relationship is true: don t use inheritance simply to get services for free. In those cases use composition instead. 3

Inheritance and method invocation - C++ Consider the following C++ code; what will the result be? Upcast.cc // Example of C++ s rules for method calls when // extending a base class. #include <iostream.h> /** This C++ class is going to be the parent class. ** It defines a tostring member function. **/ class Parent { public: Parent() {; ~Parent() {; char * tostring() { return "I m Parent s tostring()"; ; /** C++ class that inherits from Parent, also defining ** a tostring() member function. **/ class Derived : public Parent { public: Derived() {; ~Derived() {; char *tostring() { return "I m Derived s tostring()"; 4

; /** Test driver */ main() { Derived d; cout << d.tostring() << endl; // Get at parent class s behavoir Parent p = (Parent) d; cout << p.tostring() << endl; // And, of course, there is no Object that everything // derives from in C++ so there are no other // possibilities. I m Derived s tostring() I m Parent s tostring() 5

Inheritance and method invocation - Java Upcast.java // Example of Java s rules for method calls when // extending a base class. /** This class simply over-rides java.lang.object s ** tostring() method. **/ class Parent { public String tostring() { return "I m Parent s tostring()"; /** Class that inherits from Parent, which also ** over-rides tostring() **/ class Derived extends Parent { public String tostring() { return "I m Derived s tostring()"; /** Test driver */ public class Upcast { public static void main(string[] args) { Derived d = new Derived(); System.out.println("Derived s tostring() : " + d.tostring()); // This works because Derived IS-A Parent Parent p = (Parent) d; System.out.println("Parent s tostring() : " + p.tostring()); // And this works because everything IS-A Object 6

Object o = (Object) d; System.out.println("Object s tostring() : " + o.tostring()); System.out.println("explict cast to Object s tostring() : " + ((Object) o).tostring()); Derived s tostring() : I m Derived s tostring() Parent s tostring() : I m Derived s tostring() Object s tostring() : I m Derived s tostring() explict cast to Object s tostring() : I m Derived s tostring() Java is different than C++ If you invoke a method that is defined for both a base class and a derived class on a reference whose actual type is derived, you always get the derived version of the method. Other, external class objects can NOT access base class behavior. The derived class itself can access base class behavior if it chooses to. How? 7

Interfaces "the place at which independent and often unrelated systems meet and act on or communicate with each other" - Merriam-Webster "A set of method signatures... which collectively define what it means to assume a certain role..." Provide flexibility During design During extension of an existing system Interfaces define the behavior of a class (or classes) 8

Java Interfaces In Java, an interface is a way of specifying a set of methods that a class must implement. Interfaces may contain only method signatures static final variables (Java s version of constants.) An interface can not contain any instance variables or method definitions Think of an interface as if it were a small contract. Classes agree to abide by the contract by saying they implement the interface. The compiler will make sure that classes that say they implement an interface abide by the contract. 9

Java interface definition A pair of Java interfaces Billable.java public interface Billable { public void paybill(amount bill, Payable whotopay ); Payable.java public interface Payable { public void pay(amount bill, Billable whopayed ); 10

Interfaces as Types You can define variables of an interface type. In InterfaceType.java: In some other class: or public interface InterfaceType {...... InterfaceType name = methodreturninginterfacetype();... InterfaceType name = methodreturningtypeimplementinginterface(); You can not instantiate an interface type InterfaceType fieldname = new InterfaceType() // compiler error Interface types can be used as method arguments public void dosomething(interfacetype it) {... it.somemethodspecifiedininterface(); Only methods defined in the interface can be accessed via a field of InterfaceType. 11

Interface Guidelines Use Java interfaces to say "What objects can do," or occasionally, "What can be done to an object." - Bill Venners. Java interfaces don t say "What objects are." Java interfaces say what objects can do no matter what the implementation objects are 12

Design with Interfaces When we define a type with a class, we define and bind that type to some particular implementation. When we define a type in terms of an interface, we are defining a type with no requirement on the implementation other than the types of the input (arguments) and output (return type). When designing a system we should be concerned if we find ourselves writing more classes than interfaces. Jim Waldo - Interfaces and Classes 13

Interface Benefits An interface type guarantees certain behavior without needing to know or care about the implementation. One object may have many different types of behavior - polymorphism. 14

A Java interface implementation FullTimeStudent.java public class FullTimeStudent implements Billable { Amount bankbalance; public void paybill(amount amountowed, Payable whotopay) { bankbalance = bankbalance.adjust(amountowed); whotopay.pay(amountowed, this); // mumble // Other methods relating to being a FullTimeStudent that // don t relate to being billed, and how to keep // bankbalance omitted 15

A Java interface in-use Registrar.java import java.util.collection; import java.util.iterator; public class Registrar implements Payable { java.util.collection billpayers; //... public void sendbills(amount tuition) { Iterator i = billpayers.iterator(); while (i.hasnext()) { Billable b = (Billable) i.next(); b.paybill(tuition, this); public void pay(amount bill, Billable billpayer) { // look up billpayer in collection of billpayers // credit their account by the bill Amount //... remaining Registrar operations omitted 16

Notice that both classes are completely de-coupled from one another: they interact only through common interfaces. 17

Abstract Classes vs. Interfaces Interfaces are similar to abstract classes except... abstract classes can have default implementations of methods A Java class can only directly extend one class (abstract or otherwise) A Java class can implement many interfaces Other similarities include: An object of a derived class can be assigned to a reference of a super class, abstract or not AbstractClass abstractclassobject = new DerivedClass(); Similarly, any time a method takes a particular type as an argument, you can pass a more specialized type. void method( Type t ) {... DerivedType dt = new DerivedType(); obj.method(dt); This is good practice, just as using interface references was: keep method arguments as general as possible so that methods are not coupled to overly specialized classes. If you invoke a method that is defined for both a base class and a derived class on a reference whose actual type is derived, you always get the derived version of the method. This is different from C++: You cannot upcast to get at base class behavior As with interfaces, given a reference of a base class type, you can only invoke methods that are defined for the base class type, additional methods that the 18

actual type may support are inaccessible. 19

Polymorphism "the quality or state of being able to assume different forms" - Webster s "the ability to manipulate objects of distinct classes using only knowledge of their common properties without regard for their exact class." An object can expose different sets of capabilities depending on its dynamic type. In Java, polymorphism is accomplished when one class extends another class and implements one or more different interfaces. public class Something extends SomethingElse implements OneInterface, AnotherInterface {...... Something s = new Something(); s.method();... SomethingElse se = s; se.method2();... OneInterface oi = s; oi.imethod();... AnotherInterface ai = s; ai.method();... 20

Homework 6 Mid-term Exam Will be on the evening of 22 October (The final will be on 17 December) Will include material covered in lecture and assigned readings I ll review questions on anything we ve covered so far in next Monday, 15 Oct s lecture. Reading The article Maximize flexibility with interfaces and abstract classes http://www.javaworld.com/javaworld/jw-09-2001/jw-0921-interface_p.html for the theory The article Abstract classes and interfaces practicum http://www.javaworld.com/javaworld/javaqa/2001-08/03-qa-0831-interface_p.html for the practice. Programming The goal of this homework assignment is to explore using inheritance. This assignment will use new versions of the Airplane, Airline, and test classes developed in the second homework assignment. Once again, you can start from my sample solution versions or use your own. Create three classes/types of air planes: BigPlane, MediumPlane, and SmallPlane. All three classes will have the following characteristics: An identifying string ("1", "2",... etc. is sufficient), A total number of seats. Once assigned this does not change for an Airplane though different Airplanes of the same type may have different total number of seats. A number of currently available seats. A cost per seat in each of several categories, see below. 21

A method to change the cost per seat for a seat category. An implementation of the public String tostring() method that will return a String containing the type of airplane (Big, Medium, Small), the airplane ID and the current costs per seat. However, these three classes should differ from each other in the following ways: The total number of seats should be between 200-300 for BigPlanes, 50-200 for MediumPlanes, and 8-50 for SmallPlanes. Bigplanes and MediumPlanes should have three costs per seat, one each for FirstClass, BusinessClass, and EconomyClass. SmallPlanes should have only BusinessClass and EconomyClass cost per seat values. Create an abstract base class, AirPlane. Use this class to model the characteristics that are common to all three air plane classes above. You decide what belongs in the base class and what belongs in the specific classes. You should try to push as much common information into the base class. Unnecessary code duplication in derived classes is to be avoided. The base Airplane class should not have any knowledge of specific derived classes. Modify the Airline class from the previous assignments, if necessary, to use AirPlane references. You may use my sample solution version or your own. The Airline objects should save the Airplane references in a collection class. The Airline class should not have any knowledge of specific classes derived from Airplane. Modify the test class from the second assignment or create a new test class to create several of each kind of airplane. Add these airplanes one at a time through a call to Airline s addairplane method to one or more Airline objects. (An airplane should only be added to one airline s collection.) Have each Airline object list all the Airplanes in its collection: their class, id, and current cost per seat values. This assignment is due next Monday, 15 Oct. Pay strict attention to the requirements above. 22