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

Similar documents
type conversion polymorphism (intro only) Class class

COMP 250. Lecture 30. inheritance. overriding vs overloading. Nov. 17, 2017

The class Object. Lecture CS1122 Summer 2008

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

INHERITANCE. Spring 2019

Inheritance. Transitivity

Object-Oriented Design Lecture 14 CSU 370 Fall 2007 (Pucella) Friday, Nov 2, 2007

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

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

COMP 250 Fall inheritance Nov. 17, 2017

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

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

Java Magistère BFA

COMP 250. inheritance (cont.) interfaces abstract classes

Announcements/Follow-ups

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Distributed Systems Recitation 1. Tamim Jabban

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

Abstract Classes and Interfaces

Introduction to Inheritance

CS111: PROGRAMMING LANGUAGE II

Advanced Java Concepts Unit 2: Linked Lists.

Java Classes, Inheritance, and Interfaces

More about inheritance

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

CLASS DESIGN. Objectives MODULE 4

Check out Polymorphism from SVN. Object & Polymorphism

Inheritance. The Java Platform Class Hierarchy

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

COMP200 ABSTRACT CLASSES. OOP using Java, from slides by Shayan Javed

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

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

Some client code. output. subtype polymorphism As dynamic binding occurs the behavior (i.e., methods) follow the objects. Squarer

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

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Birkbeck (University of London) Software and Programming 1 In-class Test Mar 2018

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

Lecture 4: Extending Classes. Concept

ITI Introduction to Computing II

ITI Introduction to Computing II

Solutions to Sample JAC444 Midterm Test

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

Object Oriented Programming Part II of II. Steve Ryder Session 8352 JSR Systems (JSR)

What is Inheritance?

CS111: PROGRAMMING LANGUAGE II

Distributed Systems Recitation 1. Tamim Jabban

Some @SuppressWarnings

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

C09: Interface and Abstract Class and Method

Programming Language Concepts: Lecture 10

COE318 Lecture Notes Week 9 (Week of Oct 29, 2012)

Java Fundamentals (II)

Everything is an object. Almost, but all objects are of type Object!

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

Inheritance & Polymorphism. Object-Oriented Programming

Introduction to Reflection

Object-Oriented ProgrammingInheritance & Polymorphism

Software and Programming I. Classes and Arrays. Roman Kontchakov / Carsten Fuhs. Birkbeck, University of London

The Object Class. java.lang.object. Important Methods In Object. Mark Allen Weiss Copyright 2000

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

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Inheritance. Notes Chapter 6 and AJ Chapters 7 and 8

Introduction to Programming Using Java (98-388)

JAVA.LANG.CLASS CLASS

OBJECT ORIENTED PROGRAMMING. Course 4 Loredana STANCIU Room B616

Methods Common to all Classes

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Building Java Programs. Inheritance and Polymorphism

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

Super-Classes and sub-classes

Intro to Computer Science & Programming. A Cranes Club Initiative

Rules and syntax for inheritance. The boring stuff

Inheritance (Outsource: )

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

Informatik II. Tutorial 6. Mihai Bâce Mihai Bâce. April 5,

University of Cape Town ~ Department of Computer Science. Computer Science 1015F ~ 2007

CSEN401 Computer Programming Lab. Topics: Object Oriented Features: Abstraction and Polymorphism

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Reflection. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 28

Object Oriented Programming: Based on slides from Skrien Chapter 2

CSC9T4: Object Modelling, principles of OO design and implementation

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

Inheritance & Polymorphism

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

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

Last class. -More on polymorphism -super -Introduction to interfaces

COMP 250: Java Object Oriented Programming

Lesson 10. Work with Interfaces and Abstract Classes. Copyright all rights reserved

Inheritance, polymorphism, interfaces

Overriding methods. Changing what we have inherited from e.g. Object

Java Class Design. Eugeny Berkunsky, Computer Science dept., National University of Shipbuilding

CS 1331 Fall 2016 Exam 2

Inheritance in java is a mechanism in which one object acquires all the properties and behaviors of parent object.

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

Introduction to Software Testing Chapter 2.4 Graph Coverage for Design Elements Paul Ammann & Jeff Offutt

Informatik II Tutorial 6. Subho Shankar Basu

Creating an Immutable Class. Based on slides by Prof. Burton Ma

Polymorphism. COMP 401, Fall 2017 Lecture 08

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

COMP 110/L Lecture 20. Kyle Dewey

Transcription:

COMP 250 Lecture 32 polymorphism Nov. 25, 2016 1

Recall example from lecture 30 class String serialnumber Person owner void bark() {print woof } : my = new (); my.bark();?????? extends extends class void hunt () void bark() {print aowwwuuu } class void fight () void bark() {print Arh! Arh! Arh! }

Recall example from lecture 30 class String serialnumber Person owner void bark() {print woof } : my = new (); my.bark(); aowwwuuu extends extends class void hunt () void bark() {print aowwwuuu } class void fight () void bark() {print Arh! Arh! Arh! }

Polymorphism poly = multiple morph = form We will look at sub-type polymorphism. More general discussion about this in higher level courses e.g. COMP 302.

Polymorphism Compile time: Suppose a reference variable has a declared type: class C. C var; Runtime: That reference variable can reference any of class C or that extends class C.

Polymorphism Compile time: Suppose a reference variable has a declared type: abstract class A. A var; Runtime: That reference variable can reference any whose class extends abstract class A.

Polymorphism Compile time: Suppose a reference variable has a declared type: interface I. I var; Runtime: That reference variable can reference any whose class implements interface I.

boolean b; Object obj; : if ( b ) obj = new float[23]; else obj = new (); System.out.print( obj ); // calls tostring()

How does polymorphism work? How does all this class relationship stuff work in a running program? (Sketch only.)

Java Class Descriptors A is an that contains all the information about a class that is used in a running program. String LinkedList

Class Descriptors class name fields (names, types) methods (names, parameters, implementation) reference to superclass.

Q: Each is an instance of a class. So, if class descriptors are s, then what class(es) are they instances of? String LinkedList other s

Q: Each is an instance of a class. So, if class descriptors are s, then what class(es) are they instances of? A: the Class class https://docs.oracle.com/javase/8/docs/api/java/lang/class.html String LinkedList other s

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Class getclass() extends class Class Class getsuperclass() Method[ ] getmethods( ) Field[ ] getfields( ) String getname( ) : class extends (automatic) class Animal : class : class : extends extends

All s inherit the Object.getClass() method. This method the for that. String getclass() getclass() LinkedList getclass()

All s inherit the Object.getClass() method. This method the for that. String getclass() Class getclass() Object getclass() getclass() LinkedList getclass()

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Class getclass() extends class Class Class getsuperclass() Method[ ] getmethods( ) Field[ ] getfields( ) String getname( ) : class extends (automatic) class Animal : class : class : extends extends

getsuperclass() getsuperclass() Object getsuperclass()

Object getsuperclass() getclass() getsuperclass() getclass()

Object my = new (); System.out.println( my ); getsuperclass() getsuperclass() getclass()

Object Test What else do we need to run a program? e.g. Consider running a Test class which has a main() method. other s

Object Call Stack goforwalk() anothermethod() somemethod() Test.main() Test other s

Object Call Stack my = new (); // in main() method Test () Test.main() my other s

Object Call Stack my = new (); // after instruction is done. Test Test.main() my other s

Object Call Stack my = new (); my.bark() TestProgram bark() this Test.main() my Look for the bark() method in the other s

Class Descriptors Methods are here Object Call Stack Local variables and parameters of methods are here Objects Object instance fields are here bark() TestProgram Test.main() other s