polymorphism (intro only) Class class Nov. 24, 2017 1 Primitive - - PowerPoint PPT Presentation

polymorphism intro only
SMART_READER_LITE
LIVE PREVIEW

polymorphism (intro only) Class class Nov. 24, 2017 1 Primitive - - PowerPoint PPT Presentation

COMP 250 Lecture 33 type conversion polymorphism (intro only) Class class Nov. 24, 2017 1 Primitive Type Conversion double In COMP 273, you will non-integers learn details of how float number representations long are related to each


slide-1
SLIDE 1

1

COMP 250

Lecture 33

type conversion polymorphism (intro only) Class class

  • Nov. 24, 2017
slide-2
SLIDE 2

Primitive Type Conversion

double float long int short char byte boolean

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html In COMP 273, you will learn details of how number representations are related to each other. But you should have some intuitive ideas….

2

non-integers integers

slide-3
SLIDE 3

Primitive Type Conversion

narrower wider double float long int short char byte boolean 8 4 8 4 2 2 1 1

Here, wider usually (but not always) means more bytes.

number

  • f bytes

3

slide-4
SLIDE 4

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

Examples

4

slide-5
SLIDE 5

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.

Examples

5

slide-6
SLIDE 6

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

Examples

6

slide-7
SLIDE 7

class Dog

extends

class Beagle

narrower reference type wider reference type

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

slide-8
SLIDE 8

class Dog String serialNumber Person owner void bark() : class Beagle void hunt () :

8

extends class Doberman void fight () : extends class Poodle void show() : extends

slide-9
SLIDE 9

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

9

slide-10
SLIDE 10

Dog myDog = new Beagle(); // Upcast, widen. Poodle myPoodle = myDog; myDog.show()

10

slide-11
SLIDE 11

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

slide-12
SLIDE 12

Dog myDog = new Beagle(); // Upcasting. Poodle myPoodle = (Poodle) myDog; // Downcast // Narrowing myPoodle.show() ((Poodle) myDog).show()

12

slide-13
SLIDE 13

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

13

slide-14
SLIDE 14

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

slide-15
SLIDE 15

15

COMP 250

Lecture 33

type conversion polymorphism (intro only) Class class

  • Nov. 24, 2017
slide-16
SLIDE 16

class Dog String serialNumber Person owner void bark() {print “woof”} : class Beagle void hunt () void bark() {print “aowwwuuu”}

Recall example from lecture 30

extends class Doberman void fight () void bark() {print “Arh! Arh! Arh!”} extends

Dog myDog = new Beagle(); myDog.bark();  ?????? (which bark?)

slide-17
SLIDE 17

class Dog String serialNumber Person owner void bark() {print “woof”} : class Beagle void hunt () void bark() {print “aowwwuuu”}

Recall example from lecture 30

extends class Doberman void fight () void bark() {print “Arh! Arh! Arh!”} extends

Dog myDog = new Beagle(); myDog.bark();  “aowwwuuu”

slide-18
SLIDE 18

Polymorphism

“poly” = multiple “morph” = form

We have seen the idea already: The object 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.

slide-19
SLIDE 19

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 object of class C or any object of a class that extends C. varA can reference any object whose class extends abstract class A. varI can reference any object whose class implements interface I.

slide-20
SLIDE 20

boolean b; Object obj; : if ( b )

  • bj = new Cat();

else

  • bj = new Dog();

: System.out.print( obj ); // Which toString() method that gets called // depends on the object referenced by obj.

(See Exercises for more examples.)

slide-21
SLIDE 21

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

slide-22
SLIDE 22

22

COMP 250

Lecture 33

type conversion polymorphism (intro only) Class class

  • Nov. 24, 2017
slide-23
SLIDE 23

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

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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

Example

slide-26
SLIDE 26

Dog.java text file Dog.class class file compiler Dog

“class descriptor”

Runtime The class is “loaded” into the JVM.

slide-27
SLIDE 27

Dog

class descriptor

The term “class descriptor” is not standard. So don’t look it up. It is an object that contains all the information about a class. If it is an object, then what class is it an instance of ? Beagle

class descriptor

String

class descriptor

LinkedList

class descriptor

slide-28
SLIDE 28

class Class

Class getSuperClass() Method[ ] getMethods( ) Field[ ] getFields( ) String getName( ) :

A “class descriptor” is an instance of the Class class. It has many methods:

slide-29
SLIDE 29

A Dog object is an instance of the Dog class. A String object is an instance of the String class. An Object object is an instance of the Object class. A Class object (“class descriptor” object) is an instance

  • f the Class class.
slide-30
SLIDE 30

Dog

  • bject

Beagle

  • bject

Beagle

  • bject

Doberman

  • bject

Doberman

  • bject
  • ther
  • bjects

Dog class descriptor Beagle class descriptor LinkedList class descriptor Doberman class descriptor String class descriptor

Each class descriptor is an instance of the Class class.

This figure shows objects in a running Java program.

slide-31
SLIDE 31

class Animal : class Dog : class Beagle : extends extends

class Object

boolean equals( Object ) int hashCode( ) String toString( ) Object clone( ) Class getClass()

extends (automatic)

class Class

Class getSuperClass() Method[ ] getMethods( ) Field[ ] getFields( ) String getName( ) :

extends

This figure shows classes in the Java class hierarchy.

slide-32
SLIDE 32

All classes inherit the Object.getClass() method which returns the class descriptor for that object.

Dog

  • bject

Beagle

  • bject

Beagle

  • bject

Doberman

  • bject

Doberman

  • bject

Dog class descriptor Beagle class descriptor LinkedList class descriptor Doberman class descriptor String class descriptor getClass() getClass() getClass()

This figure shows objects in a running Java program.

slide-33
SLIDE 33

All classes inherit the Object.getClass() method, which returns the class descriptor for that object.

Dog

  • bject

Beagle

  • bject

Beagle

  • bject

Doberman

  • bject

Doberman

  • bject

Dog class descriptor Beagle class descriptor LinkedList class descriptor Doberman class descriptor String class descriptor getClass() getClass() getClass() Class class descriptor getClass() Object class descriptor getClass()

This figure shows objects in a running Java program.

slide-34
SLIDE 34

class Animal : class Dog : class Beagle : extends extends

class Object

boolean equals( Object ) int hashCode( ) String toString( ) Object clone( ) Class getClass()

extends (automatic)

class Class

Class getSuperClass() Method[ ] getMethods( ) Field[ ] getFields( ) String getName( ) :

extends

This figure shows classes in the Java class hierarchy.

slide-35
SLIDE 35

Dog

  • bject

Beagle

  • bject

Beagle

  • bject

Doberman

  • bject

Doberman

  • bject

Object class descriptor Beagle class descriptor Doberman class descriptor getSuperClass() Dog class descriptor getSuperClass() getSuperClass() Class class descriptor getSuperClass() The getSuperClass() method cannot be invoked by the objects above. Why not?

This figure shows objects in a running Java program.

slide-36
SLIDE 36

Dog

  • bject

Beagle

  • bject

Beagle

  • bject

Doberman

  • bject

Doberman

  • bject

Beagle class descriptor Doberman class descriptor Dog class descriptor getSuperClass() getClass() getClass() Object class descriptor getSuperClass() Class class descriptor getSuperClass() getClass()

This figure shows objects in a running Java program.

slide-37
SLIDE 37

We’ll see more about how this works next week…