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

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Searching for Information. A Simple Method for Searching. Simple Searching. Class #21: Searching/Sorting I

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Review: Using Imported Code. What About the DrawingGizmo? Review: Classes and Object Instances. DrawingGizmo pencil; pencil = new DrawingGizmo();

} Each object in a Java program has an identifier (name) } This includes:

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

Review: Classes and Object Instances. Review: Creating an Object. Using Multiple Objects. DrawingGizmo pencil; pencil = new DrawingGizmo();

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

CSE 113 A. Announcements - Lab

CS11 Introduction to C++ Fall Lecture 7

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

Building custom components IAT351

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Software Design and Analysis for Engineers

C++ Important Questions with Answers

Programming Exercise 14: Inheritance and Polymorphism

University of British Columbia CPSC 111, Intro to Computation Jan-Apr 2006 Tamara Munzner

CS 251 Intermediate Programming Inheritance

CS18000: Problem Solving And Object-Oriented Programming

Inheritance. COMP Week 12

Final Exam CS 251, Intermediate Programming December 10, 2014

Introduction to Computer Science II CS S-10 Inheritance

CMSC202 Computer Science II for Majors

Java Object Oriented Design. CSC207 Fall 2014

Project 1 Balanced binary

EECS 211 Lab 8. Inheritance Winter Getting the code. Inheritance. General Idea. An Example

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

Objects and Classes. Amirishetty Anjan Kumar. November 27, Computer Science and Engineering Indian Institue of Technology Bombay

Arrays Classes & Methods, Inheritance

CompSci 125 Lecture 20. Inheritance: Introduction, Overrides UML: Introduction to Class Diagrams

And Even More and More C++ Fundamentals of Computer Science

Material: Specification and Reasoning. Book: Logic in Computer Science, M.Huth, M.Ryan, Cambridge University Press. Lectures mondays here,

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

COMP 110/L Lecture 19. Kyle Dewey

CS61BL. Lecture 1: Welcome to CS61BL! Intro to Java and OOP Testing Error-handling

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

Encapsulation. Administrative Stuff. September 12, Writing Classes. Quick review of last lecture. Classes. Classes and Objects

COP 3330 Final Exam Review

Object Orientated Programming Details COMP360

More on Inheritance. Interfaces & Abstract Classes

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

Inheritance. For example, to zoom in on the deer family (cervid), we could make a tree like the following.

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

Lecture 10: building large projects, beginning C++, C++ and structs

Oracle Field Service/Laptop

Inheritance and Interfaces

Computer Science II. OO Programming Classes Scott C Johnson Rochester Institute of Technology

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

Inheritance STL. Entity Component Systems. Scene Graphs. Event Systems

BINARY SEARCH TREES CS 200 RECITATION 7

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

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

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

CS Programming I: Inheritance

Google Drive: Access and organize your files

CS2102: Lecture on Abstract Classes and Inheritance. Kathi Fisler

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

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Tutorial 2 Today we will have a quick introduction to ArgoUML and how to use it, as well as look at some examples of class diagrams.

Topic 5 Polymorphism. " Inheritance is new code that reuses old code. Polymorphism is old code that reuses new code.

Computer Science 2 Lecture 4 Inheritance: Trinidad Fruit Stand 02/15/2014 Revision : 1.7

public Twix() { calories = 285; ingredients = "chocolate, sugar, cookie, caramel"; }

Operator overloading

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

Inheritance -- Introduction

Carleton University COMP1406/1006, Summer 2013 Tutorial 2

Lesson 10B Class Design. By John B. Owen All rights reserved 2011, revised 2014

ITI Introduction to Computing II

CSE115 Lab 2 Fall 2016

CS445 Week 9: Lecture

Week 11: Class Design

INHERITANCE AND EXTENDING CLASSES

Inheritance & Polymorphism

Part VIII. More classes. Philip Blakely (LSC) C++ Introduction 226 / 370

ECE 122. Engineering Problem Solving with Java

Data Structures (list, dictionary, tuples, sets, strings)

UML & OO Fundamentals. CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 3 09/04/2012

CS Week 15 Page 1

CS1004: Intro to CS in Java, Spring 2005

Generic BST Interface

Java Basics. Object Orientated Programming in Java. Benjamin Kenwright

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

Chapter 10 Classes Continued. Fundamentals of Java

CSE 113. Announcements

Tips from the experts: How to waste a lot of time on this assignment

ITI Introduction to Computing II

Starting to Program in C++ (Basics & I/O)

CS1004: Intro to CS in Java, Spring 2005

Inheritance and Polymorphism in Java

Data Structures and Algorithms Dr. Naveen Garg Department of Computer Science and Engineering Indian Institute of Technology, Delhi.

Lecture 14: more class, C++ streams

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

UML & OO FUNDAMENTALS CSCI 4448/5448: OBJECT-ORIENTED ANALYSIS & DESIGN LECTURE 3 08/30/2011

Sorting. Sorting. Selection sort

Derived Classes in C++

COSC 121: Computer Programming II. Dr. Bowen Hui University of Bri?sh Columbia Okanagan

CS 200 More Classes Jim Williams, PhD

Procedural Abstraction

CS61BL Summer 2013 Midterm 2

Transcription:

Class #02: Inheritance and Object-Oriented Design Software Design II (CS 220): M. Allen, 23 Jan. 18 Basic Class Diagrams Describes a class and how it can be used properly Sketch of properties and behaviors of objects in the class No extra details about how they actually work Class Name attributes (instance variables) operations (methods) + (String, int) 2 Attributes What does the minus sign here mean? class has two simple attributes, storing basic facts about each individual car Each different object instance in a program will have its own version of these different attributes + (String, int) class instance variables both have private access Store information, but that info cannot be accessed or altered from anywhere outside the class How do we read or write to such private variables? + (String, int) 3 4 1

Class Methods What does the plus sign mean here? Update methods Change something about state of an object These return void (take an action but don t give output) Query methods Tell us about object state non-void return: actually give us some info back + (String, int) These methods all have public access Can be used outside of the class (by other class code) Allows programmer to control exactly how variables are changed, and what information is revealed by the class + (String, int) 5 6 Code for a Lot Management Application Constructor Constructor method Used to create an object Has same name as class Has no return type in front Almost always public (WHY?) This constructor takes two parameters as input + (String, int) Manny Citron s Used s wants you to write an inventory-management program for them It needs to track their current stock of vehicles Needs to add and remove vehicles from the stock 7 8 2

Object-Oriented Design The Lot class uses instance variables of the class type It is an aggregate class, made up of other class objects We can sketch relationships between class diagrams Diamond arrow signifies a used by relationship Also known as a has-a relationship, in the sense that the Lot class has some instances as part of its own specification car1 car2 Lot + Lot() + add() + remove() + void prints() <<attributes>> <<methods>> A Hitch: Changing Problem Specification Manny soon realizes that your basic class isn t enough for his needs Also sells commercial vehicles (trucks/vans), with different carrying capacities (1/5/10 ton, ) 9 10 One Solution: Create a New Class Another Solution + (String, int) Truck int capacity + Truck(String, int, int) + void setcapacity(int) + int getcapacity() We could just duplicate the class for the Truck class (and maybe more), adding in the features we need A number of drawbacks: 1. Code repetition: we duplicate our work in multiple classes 2. Harder to write general-purpose code: our car-lot code will have to handle Truck and objects, and we may not know which ones we are going to get at any specific point (e.g. when a new one is added to the stock) Instead, we will use inheritance structures 11 12 3

Common Features in Classes Certain types of objects have things in common s/trucks/motorcycles Savings/checking/investment accounts In Java, such similarities are exploited by inheritance Code can be made simpler and more useful Similarities are written into the super-class (parent) Each sub-class (child) extends the parent Inherits all similarities, and can have particular differences of its own Child inherits methods and variables If these things are private, then they are not accessible to child If they are public (or protected), then the child can directly access them as if they were its very own Inheritance in Pictures and in Code Inheritance can be represented as a diagram, with openheaded arrows from children to parents Each child is a more specific kind of parent object This is called an is-a relationship, since each, for example, is actually a kind of Vehicle, too Vehicle Truck class Vehicle { class extends Vehicle { // data and methods // more data and methods class Truck extends Vehicle { // more data and methods 13 14 Object Diagrams for Inheritance Implementing the Vehicle Class Vehicle - String makemodel - int mileage + Vehicle(String, int) + (String, int) Truck int capacity + Truck(String, int, int) + void setcapacity(int) + int getcapacity() public class Vehicle { private String makemodel; private int mileage; public Vehicle( String mod, int miles ) { makemodel = mod; mileage = miles; public void setmakemodel( String s ) { makemodel = s; public void setmileage( int m ) { mileage = m; public String getmakemodel() { return makemodel; public int getmileage() { return mileage; Other than the necessary Vehicle() constructor, this is identical to the code that was originally part of the class If the class doesn t need to add any more features, then there we won t bother to do so 15 16 4

The Sub-Class Implementing the Class extends Vehicle for inheritance needs own constructor, but nothing else! + (String, int) Every other method and variable it needs can be inherited directly from the Vehicle class, without need of repetition public class extends Vehicle { public ( String mod, int miles ) { super( mod, miles ); The () constructor simply takes its inputs and passes them along to the super() Vehicle constructor, which itself then writes their values to the instance variables 17 18 Constructors and the super Command All methods and variables of a parent are inherited, except for the constructor Main reason: constructor must have same name as class itself Parent constructor must be called first to instantiate any necessary attribute variables, before child constructor can do any necessary work of its own The child runs its own constructor, and if it wants to use the parent constructor it uses the super reference super() is the parent constructor super.methodname() runs a method from the parent explicitly (often optional, as long as there is no other method with the same name, due to over-riding, etc.) Implementing the Truck Class Truck has extra instance variable Constructor runs super() to set common variables, then saves extra capacity variable locally public class Truck extends Vehicle { private int capacity; public Truck( String mod, int miles, int cap ) { super( mod, miles ); capacity = cap; /* post: result == makemodel */ public void setcapacity( int c ) { capacity = c; public int getcapacity() { return capacity; Has its own particular methods, unique to Truck class 19 20 5

This Week Topic: Inheritance and polymorphism Class meetings: Monday Thursday: regular classroom Friday: in the CS Lab (16 Wing) Optional Reading: Chapter 5 & 6 in text Office Hours: Wing 210 Monday/Wednesday/Friday, 10:00 AM 11:00 AM Tuesday/Thursday, 1:30 PM 3:00 PM 21 6