Check out Polymorphism from SVN. Object & Polymorphism

Similar documents
CSSE 220 Day 15. Inheritance. Check out DiscountSubclasses from SVN

Introduction to Inheritance

Intro to Computer Science 2. Inheritance

Chapter Goals. Chapter 9 Inheritance. Inheritance Hierarchies. Inheritance Hierarchies. Set of classes can form an inheritance hierarchy

Inheritance (P1 2006/2007)

Inheritance Advanced Programming ICOM 4015 Lecture 11 Reading: Java Concepts Chapter 13

Polymorphism. return a.doublevalue() + b.doublevalue();

INHERITANCE. Spring 2019

CMSC 132: Object-Oriented Programming II

Inheritance. Inheritance

Java Magistère BFA

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

More On inheritance. What you can do in subclass regarding methods:

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

Inheritance & Abstract Classes Fall 2018 Margaret Reid-Miller

Announcement. Agenda 7/31/2008. Polymorphism, Dynamic Binding and Interface. The class will continue on Tuesday, 12 th August

Chapter 10 Inheritance. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

Handout 9 OO Inheritance.

Object Oriented Programming. Java-Lecture 11 Polymorphism

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Lecture 18 CSE11 Fall 2013 Inheritance

CSC9T4: Object Modelling, principles of OO design and implementation

What is Inheritance?

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

CS111: PROGRAMMING LANGUAGE II

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

Java Fundamentals (II)

Inheritance. Transitivity

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

Name Return type Argument list. Then the new method is said to override the old one. So, what is the objective of subclass?

Inheritance -- Introduction

Rules and syntax for inheritance. The boring stuff

Chapter 9 Inheritance

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Abstract Classes and Interfaces

Super-Classes and sub-classes

Overview. Lecture 7: Inheritance and GUIs. Inheritance. Example 9/30/2008

Principles of Software Construction: Objects, Design, and Concurrency

CO Java SE 8: Fundamentals

COMP 250. Lecture 32. polymorphism. Nov. 25, 2016

C12a: The Object Superclass and Selected Methods

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

Polymorphism and Interfaces. CGS 3416 Spring 2018

CS 251 Intermediate Programming Inheritance

This week. Tools we will use in making our Data Structure classes: Generic Types Inheritance Abstract Classes and Interfaces

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

CS111: PROGRAMMING LANGUAGE II

Chapter 5 Object-Oriented Programming

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

Inheritance and Polymorphism. CS180 Fall 2007

CS-202 Introduction to Object Oriented Programming

Java How to Program, 8/e

CS/ENGRD 2110 SPRING 2018

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

type conversion polymorphism (intro only) Class class

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

CISC370: Inheritance

Inheritance and Interfaces

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

Inheritance and Polymorphism

Implements vs. Extends When Defining a Class

ECE 122. Engineering Problem Solving with Java

Programming in C# Inheritance and Polymorphism

Object Model. Object Oriented Programming Spring 2015

Inheritance (continued) Inheritance

25. Generic Programming

Building Java Programs. Inheritance and Polymorphism

More About Objects. Zheng-Liang Lu Java Programming 255 / 282

Inheritance and Polymorphism

The major elements of the object-oriented model

CMSC 132: Object-Oriented Programming II

Inheritance and object compatibility

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

Inheritance & Polymorphism

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

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

More about inheritance

Abstract and final classes [Horstmann, pp ] An abstract class is kind of a cross between a class and an interface.

The class Object. Lecture CS1122 Summer 2008

Pieter van den Hombergh Thijs Dorssers Stefan Sobek. February 10, 2017

CSE 143 Lecture 22. The Object class; Polymorphism. read slides created by Marty Stepp and Ethan Apter

Practice for Chapter 11

IST311. Advanced Issues in OOP: Inheritance and Polymorphism

Binghamton University. CS-140 Fall Chapter 9. Inheritance

CLASS DESIGN. Objectives MODULE 4

ITI Introduction to Computing II

CN208 Introduction to Computer Programming

C09: Interface and Abstract Class and Method

ITI Introduction to Computing II

CS260 Intro to Java & Android 03.Java Language Basics

Object Model. Object Oriented Programming Winter

Java Object Oriented Design. CSC207 Fall 2014

Starting Out with Java: From Control Structures Through Objects Sixth Edition

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

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

Section: Inheritance Heck

Inheritance Motivation

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

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

PROGRAMMING III OOP. JAVA LANGUAGE COURSE

Transcription:

Check out Polymorphism from SVN Object & Polymorphism

Inheritance, Associations, and Dependencies

Generalization (superclass) Specialization (subclass)

Dependency lines are dashed Field association lines are solid Use association lines only when an item is stored as a field.

The superest class in Java

Every class in Java inherits from Object Directly and explicitly: public class String extends Object { } Directly and implicitly: class BankAccount { } Indirectly: class SavingsAccount extends BankAccount { } Q1

String tostring() Often overridden boolean equals(object otherobject) Class getclass() Object clone() Sometimes useful Often dangerous! Q2

Return a concise, human-readable summary of the object state Very useful because it s called automatically: During string concatenation For printing In the debugger getclass().getname() OR getclass().getsimplename() comes in handy here Q3

equals(object foo) should return true when comparing two objects of same type with same meaning How? Must check types use instanceof OR getclass().isassignablefrom(foo.getclass()) Must compare state use cast Recall casting a variable: Taking an Object of one particular type and turning it into another Object type Q4

Review and Practice

A subclass instance is a superclass instance Polymorphism still works! BankAccount ba = new SavingsAccount(); ba.deposit(100); But not the other way around! SavingsAccount sa = new BankAccount(); sa.addinterest(); Why not? BOOM!

Can use: public void transfer(double amount, BankAccount o) { this.withdraw(amount); o.deposit(amount); } in BankAccount To transfer between different accounts: SavingsAccount sa = ; CheckingAccount ca = ; sa.transfer(100, ca);

If B extends or implements A, we can write A x = new B(); Declared type tells which methods x can access. Compile-time error if try to use method not in A. The actual type tells which class version of the method to use. Can cast to recover methods from B: ((B)x).foo() Now we can access all of B s methods too. If x isn t an instance of B, it gives a run-time error (class cast exception)

Step 1: Identify the Declared/Casted Type This is the item to the left of the variable name when the variable was declared: BankAccount sa = new SavingsAccount(); Declared Type Declared Type may be changed due to a cast: ((SavingsAccount)sa).addInterest(); Casted Type If there is a casted type, record that, otherwise use the declared type.

Step 2: Identify the Instantiation/Actual Type This is the type on the right hand side of the equal sign the last time the variable was assigned to: BankAccount sa = new SavingsAccount(); Instantiation Type Record the instantiation type

Step 3: Check for Compilation Errors Calling a method that is not available based on the declared or casted type of the object BankAccount sa = new SavingsAccount(); sa.addinterest(); Compiler Error: BankAccount does not have addinterest Incompatible type assignment SavingsAccount x = new BankAccount(); Compiler Error: BankAccounts can not be stored in SavingAccount typed variables Invalid cast: casting to a type that isn t in the tree below the declaration type. BankAccount sa = new SavingsAccount(); ((SafetyDepositBox)sa).depositItem(); SafetyDepositBox is not below BankAccount. Cannot instantiate interfaces or abstract classes!

Step 4: Check for Runtime Errors Runtime errors are caused by invalid casting. An item may only be cast to a type IF: The instantiation type matches the casted type The casted type is between the declaration type and the instantiation type BankAccount sa = new SavingsAccount(); ((CheckingAccount)sa).deductFees(); Runtime Error: SavingsAccount is not a CheckingAccount Account a = new CheckingAccount(); ((BankAccount)a).deposit(); This is valid because a CheckingAccount is a BankAccount

Step 5: Find Method to Run Find the instantiation type in the hierarchy. 1. If that type implements the given method, then use that implementation. 2. Otherwise, move up to the parent type and see if there s an implementation there. a. If there is an implementation, use that. b. Otherwise, repeat step 2 until an implementation is found.

Do questions 5 through 7 from Quiz. Please hand them in when done and then start reading the BallWorlds specification on your schedule page. Q5-7, hand in when done, then start reading BallWorlds spec

Project Introduction Meet your partner (see link in part 3 of spec) Carefully read the requirements and provided code Ask questions (instructor and TAs).

Look over the BallWorlds UML Class Diagram and start Questions. Check out BallWorlds from SVN

Pulsar, Mover, etc.