Kate Gregory Week 6 Lab 3 due now –Rating your group Inheritance Multiple Inheritance Polymorphism Midterm Review.

Slides:



Advertisements
Similar presentations
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Advertisements

CS 2430 Day 28. Announcements Program 5 was posted (on Tuesday, 4/2/2013) Can work with a partner (sign up by today at 3:52pm) Exam 2 handed back on Monday.
Week 2 Concepts normally in week 1 Software Development Life Cycle Use Cases Lab project Lab 1 Kate Gregory.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 3 Advanced Object-Oriented Concepts.
Inheritance Inheritance Reserved word protected Reserved word super
Inheritance Java permits you to use your user defined classes to create programs using inheritance.
9. Inheritance 9.1 Subclasses 9.2 Polymorphism 9.3 Abstract Classes 9.4 Modifiers and Access 9.6 Object-Oriented Design with Use Cases and Scenarios.
Kate Gregory1 Week 3 Lab 1 due now Finding Classes in your System CRC Cards Collaboration Diagrams Lab 2.
ITEC200 – Week03 Inheritance and Class Hierarchies.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
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.
Inheritance (notes for 10/26 lecture). Inheritance Inheritance is the last of the relationships we will study this semester. Inheritance is (syntactically)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter N - 1 Chapter 13 Polymorphism is-a relationships Interfaces.
Abstract Classes and Interfaces
Chapter 11: Inheritance and Polymorphism Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
1 Data Structures - CSCI 102 CS102 C++ Polymorphism Prof Tejada.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
Inheritance and Class Hierarchies Ellen Walker CPSC 201 Data Structures Hiram College.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
1 Object-Oriented Programming: Polymorphism 10.1 Introduction 10.2 Relationships Among Objects in an Inheritance Hierarchy Invoking Superclass Methods.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Kate Gregory1 Week 4 Lab 2 due now Why document? Unified Modeling Language Class diagrams.
Kate Gregory Week 10 State diagrams Activity Diagrams Summary and Conclusion / The Future Critique discussion Lab 4 is marked.
Polymorphism, Abstraction and Virtual Functions. In this slide, we introduce virtual functions and two complex and powerful uses for derived classes that.
Week 9 Lab 4 due now Design Patterns Good Design Modules and Packages –Deployment diagram –Component diagram SOLID Principles Metrics Lab 5 available –
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
Kate Gregory Week 8 Midterm discussion Interaction / Sequence Diagrams Design Patterns RAII Lab 4.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Software Design Principles
Object Oriented Software Development
Java Programming Dr. Randy Kaplan. Abstract Classes and Methods.
Kate Gregory Week 5 Static Object Model –Attributes –Methods –Associations –Visibility Lab 3 available –Rating your group.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CPS Inheritance and the Yahtzee program l In version of Yahtzee given previously, scorecard.h held information about every score-card entry, e.g.,
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CSE 143 Lecture 12 Inheritance slides created by Ethan Apter
Java Programming: From Problem Analysis to Program Design, 3e Chapter 11 Inheritance and Polymorphism.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Subclasses CS 21a. 6/28/2004 Copyright 2004, by the authors of these slides, and Ateneo de Manila University. All rights reserved L16:
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.
CompSci Reading from Files  import java.io.File;  Declare a file File fileOfCats = new File(”cats.txt”);  Use file – pass it as an argument to.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
Object-Oriented Programming: Polymorphism Chapter 10.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
CSE 143 Lecture 13 Inheritance slides created by Ethan Apter
Inheritance ITI1121 Nour El Kadri.
Lecture 12 Inheritance.
The Object-Oriented Thought Process Chapter 1
Week 4 Object-Oriented Programming (1): Inheritance
Programming Language Concepts (CIS 635)
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Domain Class Diagram Chapter 4 Part 2 pp
MSIS 670 Object-Oriented Software Engineering
Week 6 Object-Oriented Programming (2): Polymorphism
Advanced Programming Behnam Hatami Fall 2017.
Inheritance Inheritance is a fundamental Object Oriented concept
Chapter 9 Carrano Chapter 10 Small Java
Overview of C++ Polymorphism
Object Oriented Analysis and Design
Extending Classes Through Inheritance
Computer Science II for Majors
Presentation transcript:

Kate Gregory Week 6 Lab 3 due now –Rating your group Inheritance Multiple Inheritance Polymorphism Midterm Review

DateWeekTopicHand OutDue BackTest 6-Sep-131Administrivia / Overview / Motivation, benefits of OO 13-Sep-132Use CasesLab 1: Use cases 20-Sep-133CRC Cards, collab graphsLab 2: CRC cardslab 1 5% 27-Sep-13 4start class diaglab 2 5% 4-Oct-135Finish class diag, AssociationsLab 3: Class Diag 11-Oct-136Inh & Polymorphism / midterm review lab 3 5% 18-Oct-137midtermMidterm 25% 25-Oct-13Reading Break 1-Nov-138Interaction diag / Design PatternsLab 4: Interaction Diag 8-Nov-139Good Design / Modules & Packages / Deployment and component diagrams /Metrics / SOLID Lab 5: Critiqueslab 4 5% 15-Nov-1310State diagrams / Activity diagrams / Summary and Conclusion / The Future 22-Nov-1311Critiquescritique lab (before class) 15% 29-Nov-1312Critiques TBDFinal ExamFinal 40%

Kate Gregory Inheritance Programming by extension One class is based on (is derived from) another class. Inheritance allows classes to share and reuse behaviors and attributes. –Code and design Build on existing work –Designing, coding, testing

Kate Gregory From the General to the Specific The base class or super class is general: animal, vehicle, bank account The derived classes or subclasses are specific: mammal, car, savings account You add functionality as you move from the general to the specific: mammals can do things that not all animals can You cannot remove functionality

Kate Gregory Inheritance Example

Kate Gregory IS A Inheritance examples should make sense with “is a”. –A mammal is an animal –An ungulate is a mammal –A savings account is a bank account –A rush order is an order –A car is a vehicle Is a car an engine with metal wrapped around it?

IS A Problems Square and Rectangle have obvious similarities –Location in space –Can calculate area (similar formula) –Rectangle has height and width, Square has only height Square IS A Rectangle? –Can’t suppress functionality Rectangle IS A Square? –Doesn’t make any sense Kate Gregory

Square and Rectangle Square Location: Point Width: int GetArea(): int Move(Point) GetWidth(): int SetWidth(int) Rectangle Location: Point Width: int Height: int GetArea(): int Move(Point) GetWidth(): int SetWidth(int) GetHeight(): int SetHeight(int)

Square and Rectangle Square Rectangle Square inherits all Rectangle functions including GetHeight() and SetHeight()

Square and Rectangle Rectangle Square Rectangle adds functions including GetHeight() and SetHeight(), adds attribute height

Abstract Base Class Quadrilateral –Has location in space, idea of a function for area Rectangle IS A Quadrilateral –With height and width –Implements area function Square IS A Quadrilateral –With height –Implements area function Kate Gregory

Abstract Base Class Square Width: int GetArea(): int GetWidth(): int SetWidth(int) Rectangle Width: int Height: int GetArea(): int GetWidth(): int SetWidth(int) GetHeight(): int SetHeight(int) Quadrilateral Location: Point GetArea(): int Move(Point)

Abstract Classes An abstract class can’t be used to generate an object –Can you open just a bank account? Subclasses must implement the un-implemented methods or they are also abstract You instantiate objects of the subclasses Kate Gregory

Not all Base Classes are Abstract Use it when no complete (working) class has all the common functionality Don’t go out of your way to invent ABC when a concrete class can be a good base class –Eg Rush Order inherits from Order, don’t need to create abstract Order with Rush Order and Regular Order as subclasses Kate Gregory

Multiple Inheritance Some OO systems (C++, not Java or C#) permit a class to inherit from more than one superclass. This kind of inheritance is referred to as multiple inheritance.

Kate Gregory Multiple Inheritance Example Utility vehicle inherits from Car and Truck classes.

Kate Gregory Diamond Problem: Bad M.I. Disambiguating override Appliance +turnOn() : boolean +turnOff() : boolean -poweredup : boolean Clock +setAlarm() : boolean +turnOn() : boolean -time : int Radio +turnOn() : boolean -station : int ClockRadio

Diamond Problem What happens here? ClockRadio cr; cr.turnOn(); Need a disambiguating override Kate Gregory

Good Multiple Inheritance Problem : Track RCMP transportation –Includes: Car, PowerBoat, Bike, Canoe, SeaDoo and Horse. –Track schedules for changing oil, rotating tires, feeding, checkingForHoles etc. –Use inheritance as much as possible since we only want to write the code once !

Kate Gregory

Vehicle is still base class for these three. LandSeaAir CarHorseBikePowerBoatCanoeSeaDoo Motorized +changeOil() +rotateTires()

What if there’s no MI allowed? No need to inherit code, just design: –Interface –Keep substitutability, IS-A, polymorphism Need to inherit (reuse, maintain) code: –Use aggregation instead –Wrapper functions delegate to helper class –Drawbacks if helper class adds a method you must add the wrapper by hand to all who use it No polymorphism

Kate Gregory

Polymorphism Motor Vehicle +colour: ? +price: ? +model: ? +go() +stop() +turn left() +turn right() BusTruckCar Ford Mustang +stop() +go() Toyota CorollaPontiac Sunfire

Kate Gregory Polymorphism Imagine an array of MotorVehicles holding objects of Car, Truck and Bus, as well as, Mustang, Corolla and Sunfire. All are subclasses of Motor Vehicle. Code asks each vehicle in the array to stop. When the vehicle is a Mustang, the Mustang override of Stop without the calling code having to figure out which specific subclass the vehicle belongs. The correct version of a method will be called even though the object is being referred to in more generic terms.

Kate Gregory Uses of Polymorphism Here are a whole pile of shapes - draw them on the screen Here are a whole pile of bank accounts - print their statements Here are a whole pile of employees - print their paycheques

Kate Gregory Polymorphism benefit The old way: struct shape { int type; struct point reference; int dimension1; int dimension2; int dimension3; /*...*/ };

Kate Gregory Using the struct struct shape circle, square; /* fill them with values somehow*/ /* put pointers to them into an array */ for (i=0; i<numshapes; i++) { switch (array[i].type) { case CIRCLE: drawcircle(array[i]); break; case SQUARE: drawsquare(array[i]); break; /* etc */ }

Kate Gregory Without Polymorphism What is involved in adding a new shape? –#define for type –another case statement in the switch –new drawing method –the code that created the shape and added it to the array Four different files

Kate Gregory With Polymorphism class Shape { protected: Point reference; public: virtual void draw(); };

Kate Gregory class Circle: public Shape { private: int radius; public: void draw(); }; class Square: public Shape { private: int width; public: void draw(); };

Kate Gregory Circle circle; Square square; /* fill them with values somehow*/ /* put pointers to them into an array */ for (i=0; i<numshapes; i++) { array[i]->draw(); } Using the classes

Kate Gregory With Polymorphism What is involved in adding a new shape? –New class definition –code for draw() method –the code that created the shape and added it to the array Two or three different files –can get compiler to remind you to write the function

Inheritance and Relationships On your class diagram, if a Person can own any kind of Vehicle, draw the line to Vehicle, not to each subclass If for some reason a Company can own any kind of Vehicle but a Person can only own a Car, draw the lines to show that. Kate Gregory

Next Weeks Oct 18 th - midterm –Covers till the end of this lecture Oct 25 th, no lecture –Reading Week Nov 1 st –Interaction diagrams, design patterns –Lab 4 on interaction diagrams (due Nov 8 th )

Midterm Location Science Complex Lecture Hall (SC 137) Not This Room! We will start shortly after 9am and run for one hour There will not be a lecture afterwards Kate Gregory

Midterm Worth 25% of final grade One hour Closed book Covers up to today’s class True/False – Multiple Choice 20% Short Answer -- <= One sentence 20% Long Answer – diagrams, paragraphs etc. 60%