type conversion polymorphism (intro only) Class class

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

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

COMP 250 Fall inheritance Nov. 17, 2017

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

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

The class Object. Lecture CS1122 Summer 2008

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

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

First IS-A Relationship: Inheritance

Inheritance & Polymorphism

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

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

Lecture Set 4: More About Methods and More About Operators

Lecture Set 4: More About Methods and More About Operators

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

Create a Java project named week10

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

More Relationships Between Classes

Operators and Expressions

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

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

Exercise: Singleton 1

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Introduction to Inheritance

Example: Count of Points

Introduction to Programming Using Java (98-388)

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Casting. References. References

Introduction to Object-Oriented Programming

Example: Count of Points

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

Java Primer 1: Types, Classes and Operators

Lecture 4: Extending Classes. Concept

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

CSE 431S Type Checking. Washington University Spring 2013

What is Inheritance?

Informatik II Tutorial 6. Subho Shankar Basu

CS111: PROGRAMMING LANGUAGE II

PIC 20A Number, Autoboxing, and Unboxing

Chapter 5 Object-Oriented Programming

Inheritance and Polymorphism

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

Announcements/Follow-ups

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Inheritance. The Java Platform Class Hierarchy

CS/ENGRD 2110 SPRING 2018

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

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

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

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

C09: Interface and Abstract Class and Method

CS100J, Fall 2003 Preparing for Prelim 1: Monday, 29 Sept., 7:30 9:00PM

Polymorphism. Arizona State University 1

COE318 Lecture Notes Week 8 (Oct 24, 2011)

Inheritance -- Introduction

Informatik II (D-ITET) Tutorial 6

INHERITANCE. Spring 2019

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

CLASS DESIGN. Objectives MODULE 4

COMP 250. inheritance (cont.) interfaces abstract classes

Basic Object-Oriented Concepts. 5-Oct-17

Object-Oriented ProgrammingInheritance & Polymorphism

Java Object Oriented Design. CSC207 Fall 2014

Inheritance, polymorphism, interfaces

1 Shyam sir JAVA Notes

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

Interfaces. Java interface. Notes. Example of code duplication. Why an interface construct? Interfaces & Types. good software engineering

Inheritance. Transitivity

Inheritance (Outsource: )

Polymorphism. Agenda

ECE 122. Engineering Problem Solving with Java

25. Generic Programming

Abstract Classes and Interfaces

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

Chapter 6 Introduction to Defining Classes

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

Java Magistère BFA

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

CS121/IS223. Object Reference Variables. Dr Olly Gotel

ESc101 : Fundamental of Computing

Material Java type system Reflection

Programming in C# Inheritance and Polymorphism

2. The object-oriented paradigm

Implements vs. Extends When Defining a Class

Organization of Classes

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

CS-202 Introduction to Object Oriented Programming

Java Fundamentals (II)

COMP 110/L Lecture 19. Kyle Dewey

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CS260 Intro to Java & Android 03.Java Language Basics

Derived and abstract data types. TDT4205 Lecture 15

Inheritance and Polymorphism

Check out Polymorphism from SVN. Object & Polymorphism

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Distributed Systems Recitation 1. Tamim Jabban

Subtype Polymorphism

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Java Session. Day 2. Reference: Head First Java

Transcription:

COMP 250 Lecture 33 type conversion polymorphism (intro only) Class class Nov. 24, 2017 1

Primitive Type Conversion double float long int short char byte boolean non-integers integers In COMP 273, you will learn details of how number representations are related to each other. But you should have some intuitive ideas. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html 2

Primitive Type Conversion number of bytes wider Here, wider usually (but not always) means more bytes. double float long int short char byte boolean 8 4 8 4 2 2 1 1 narrower 3

Examples int i = 3; double d = 4.2; d = i ; // widening 4

Examples int i = 3; double d = 4.2; d = i ; // widening d = 5.3 * i; // widening (by "promotion") i = (int) d; // narrowing (by casting) float f = (float) d; // narrowing (by casting) For primitive types, both widening and narrowing change the bit representation. (See COMP 273.) For narrowing conversions, you get a compiler error if you don t cast. 5

Examples int i = 3; double d = 4.2; d = i ; // widening d = 5.3 * i; // widening (by "promotion") i = (int) d; // narrowing (by casting) float f = (float) d; // narrowing (by casting) char c = 'g'; int index = c; // widening c = (char) index; // narrowing 6

wider reference type class Dog extends narrower reference type class Beagle Although a subclass is narrower, it has more fields and methods than the superclass (in that it inherits all fields and methods from superclass). 7

extends class Poodle void show() : class Dog String serialnumber Person owner void bark() : extends class Beagle void hunt () : extends class Doberman void fight () : 8

Dog mydog = new Beagle(); // upcast, widening This is similar to: double mydouble = 3; // from int to double. 9

Dog mydog = new Beagle(); // Upcast, widen. Poodle mypoodle = mydog; mydog.show() 10

Dog mydog = new Beagle(); // Upcast, widen. Poodle mypoodle = mydog; // Compiler error. // Implicit downcast Dog to Poodle is not allowed. mydog.show() // Compiler error. // Poodle has show() method, // but Dog does not. 11

Dog mydog = new Beagle(); // Upcasting. Poodle mypoodle = (Poodle) mydog; // Downcast // Narrowing mypoodle.show() ((Poodle) mydog).show() 12

Dog mydog = new Beagle(); // Upcasting. Poodle mypoodle = (Poodle) mydog; // allowed by compiler mypoodle.show() // allowed by compiler // but runtime error: Dog // does not have show() method ((Poodle) mydog).show() // allowed by compiler, but runtime error for same reason 13

Most of examples above concerned compile time issues. We next examine runtime issues.

COMP 250 Lecture 33 type conversion polymorphism (intro only) Class class Nov. 24, 2017 15

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

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

Polymorphism poly = multiple morph = form We have seen the idea already: The type (run time) can be the same or narrower than the declared type (compile time). More general discussion about polymorphism in higher level courses e.g. COMP 302.

Polymorphism (the following is an important idea, not a formal definition) Compile time: Suppose a reference variable has a declared type: C varc ; // C is a class A vara ; // A is an abstract class I vari ; // I is an interface Runtime: varc can reference any of class C or any of a class that extends C. vara can reference any whose class extends abstract class A. vari can reference any whose class implements interface I.

boolean b; Object obj; (See Exercises for more examples.) : if ( b ) obj = new Cat(); else obj = new Dog(); : System.out.print( obj ); // Which tostring() method that gets called // depends on the referenced by obj.

How does (runtime) polymorphism work? To answer this question, I first need to explain how classes are represented in a running program.

COMP 250 Lecture 33 type conversion polymorphism (intro only) Class class Nov. 24, 2017 22

Java code (.java text file) compiler.class file

Java.class file ( byte code ) It has a specific format for information such as: the class name fields (names, types) methods (signature, return type, instructions) superclass. https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html

Example Dog.java text file compiler Dog.class class file runtime The class is loaded into the JVM What is this?

Dog.java text file compiler Dog.class class file Runtime The class is loaded into the JVM. Dog

The term is not standard. So don t look it up. It is an that contains all the information about a class. If it is an, then what class is it an instance of? Dog Beagle String LinkedList

A is an instance of the Class class. It has many methods: class Class Class getsuperclass() Method[ ] getmethods( ) Field[ ] getfields( ) String getname( ) :

A Dog is an instance of the Dog class. A String is an instance of the String class. An Object is an instance of the Object class. A Class ( ) is an instance of the Class class.

Each is an instance of the Class class. This figure shows s in a running Java program. String Dog Beagle Beagle LinkedList Beagle Dog Doberman Doberman other s Doberman

This figure shows classes in the Java class hierarchy. class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Class getclass() extends class Class Class getsuperclass() Method[ ] getmethods( ) Field[ ] getfields( ) String getname( ) : extends (automatic) class Animal : class Dog : class Beagle : extends extends

All classes inherit the Object.getClass() method which returns the class descriptor for that. This figure shows s in a running Java program. Dog getclass() Dog Beagle String Beagle getclass() Beagle Doberman LinkedList Doberman getclass() Doberman

All classes inherit the Object.getClass() method, which returns the class descriptor for that. This figure shows s in a running Java program. getclass() Object Class Dog getclass() Dog Beagle String getclass() Beagle getclass() Beagle Doberman LinkedList Doberman getclass() Doberman

class Object boolean equals( Object ) int hashcode( ) String tostring( ) Object clone( ) Class getclass() extends class Class Class getsuperclass() Method[ ] getmethods( ) Field[ ] getfields( ) String getname( ) : extends (automatic) This figure shows classes in the Java class hierarchy. class Animal : class Dog : class Beagle : extends extends

This figure shows s in a running Java program. getsuperclass() Object Class getsuperclass() Dog Beagle Dog getsuperclass() getsuperclass() Beagle Doberman Doberman Beagle Doberman The getsuperclass() method cannot be invoked by the s above. Why not?

This figure shows s in a running Java program. getsuperclass() Object Class getsuperclass() getclass() Dog Beagle Dog getsuperclass() getclass() Beagle Doberman Doberman Beagle Doberman getclass()

We ll see more about how this works next week