Understanding Static and Dynamic Types in Java: Animal Class Hierarchy and Casting, Lecture notes of Object Oriented Programming

The concept of static and dynamic types in java through the animal class hierarchy and casting. It covers the components common to all animals, array declaration and assignment, function calls, static/dynamic types, implicit and explicit up/downward casting, and the equals function. Students will gain a better understanding of java's class hierarchy and type system.

Typology: Lecture notes

2012/2013

Uploaded on 08/20/2013

yumni
yumni 🇮🇳

5

(2)

25 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Classesweworkwithtoday
1
Object
Animal
Dog Cat
class hierarchy:
WorkwithaclassAnimalandsubclasseslikeCatandDog
ComponentscommontoallanimalsareputinclassAnimal
Don’tshowpartitionforclassObject,butitsthere
a0
Animal
Cat
Cat(String, int)
getNoise() toString()
getWeight()
age
Animal(String, int)
isOlder(Animal)
5
a1
Animal
Dog
Dog(String, int)
getNoise() toString()
age
Animal(String,
int)
isOlder(Animal)
6
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Static and Dynamic Types in Java: Animal Class Hierarchy and Casting and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Classes

we^ work

with^ today

Work^ with^ a class hierarchy:^ ObjectAnimalDog^ Cat

class^ Animal

and^ subclasses

like^ Cat^ and

Dog

Components

common

to^ all^ animals

are^ put^ in

class^ Animal

Don’t^ show

partition

for^ class

Object,^ but

it’s^ there a

Animal Cat (^5) ageAnimal(String, int)isOlder(Animal)Cat(String, int)getNoise() toString()getWeight()

a

Animal Dog age^6 Animal(String,int)isOlder(Animal)Dog(String, int)getNoise() toString()

Animal[]

v=^ new

Animal[3];

2

declaration

of array^ v^

v^ null

Create^ array

of 3 elements

a6 Animal[] 0 null 1 null 2 null

Assign^ value

of new‐exp

to^ v

a

Assign^ and

refer^ to^

elements

as^ usual:

v[0]=^ new

Animal(…); … a=^ v[0].getAge();

null^ null

null 0 1

v

Sometimes

use^ horizontal picture^ of

an^ array:

Each element v[k] is oftype Animal.It’s declared type:static type —known at

compile-time apparent type

Static/apparent

type

4

a0^ null

a (^0) v

a

Animal Cat (^5) ageAnimal(String, int)isOlder(Animal)Cat(String, int)getNoise() toString()getWeight()

a

Animal Dog age^6 Animal(String,int)isOlder(Animal)Dog(String, int)getNoise() toString() Should this call be allowed?Should program compile?v[0].getWeight()

Each element v[k] is of(static) type Animal.From v[k], see only what isin partition Animal andpartitions above it.

View^ of

object

from^

static^

type

5

a0^ null

a (^0) v

a

Animal Cat (^5) ageAnimal(String, int)isOlder(Animal)Cat(String, int)getNoise() toString()getWeight()

a

Animal Dog age^6 Animal(String,int)isOlder(Animal)Dog(String, int)getNoise() toString() getWeight() not in class Animal orObject. Calls are illegal, programdoes not compilev[0].getWeight() v[k].getWeight()

Componentsstill in lowerpartitions, butcan’t see them

Implicit

upward

cast

7

a

Animal Cat (^5) ageAnimal(String, int)isOlder(Animal)Cat(String, int)getNoise() toString()getWeight() a Animal Dog age^6 Animal(String,int)isOlder(Animal)Dog(String, int)getNoise() toString()

public^ class

Animal^ { /**^ =^ "this

is^ older^ than^ h"^ */ public^ boolean

isOlder(Animal

h)^ {

return^ this

.age^ >^ h.age; } Call^ c.isOlder(d) h is^ created.

a1^ is^ cast

up^ to^ class Animal^ and

stored^ in

h

a1 d Dog c^ a0Cat Upward^ casts^ done automatically^ whena1h^ Animal

needed

Static/dynamic

types

8 a

Animal Dog age^6 Animal(String,int)isOlder(Animal)Dog(String, int)getNoise() toString()

public^ class

Animal^ { /**^ =^ "this

is^ older^ than^ h"^ */ public^ boolean

isOlder(Animal

h)^ {

return^ this

.age^ >^ h.age; } a1^ h^

Animal Static^ or^

apparent

type^ of^ h

is

Animal.^ Syntactic

property Determines

at^ compile

‐time what^ components

can^ be used:^ those

available

in^ Animal

Dynamic

or^ real^ type

of^ h^ is Dog.^ Semantic

or^ runtime property If^ a^ method

call^ is^ legal, determines

which^ one

is called^ (overriding

one)

Explicit

downward

cast

10

public^ class a0^ h^ Animal

Animal^ { //^ If^ Animal

is^ a^ cat,^

return^ its

weight;

otherwise,

return^ 0. public^ int

checkWeight(Animal

h)^ {

if^ (^!^

return^ 0; // {^ h^ is^ a^ Cat

Cat^ c=^ (Cat)

h^ ;^ //^ downward

cast

return^ c.getWeight(); }

a

Animal Cat (^5) ageAnimal(String, int)isOlder(Animal)Cat(String, int)getNoise() toString()getWeight() (Dog) h^

leads to runtime error. Don’t try to cast an object tosomething that it is not!

Operator

instanceof,

explicit

downward

cast

11

public^ class a0^ h^ Animal

Animal^ { //^ If^ Animal

is^ a^ cat,^

return^ its

weight;

otherwise,

return^ 0. public^ int

checkWeight(Animal

h)^ {

if^ (^!^ (h^ instanceof

Cat)^ ) return^ 0; // {^ h^ is^ a^ Cat

Cat^ c=^ (Cat)

h^ ;^ //^ downward

cast

return^ c.getWeight(); }

a

Animal Cat (^5) ageAnimal(String, int)isOlder(Animal)Cat(String, int)getNoise() toString()getWeight() object^ instanceof

cla ss true iff object is an instance of theclass —if object has a partition forclass

Overriding

function

equals

Override function equals in a class to give meaning to:“these two (possibly different) objects of the class havethe same values in some of their fields”

13

For^ those

who^ are

mathematically

inclined,

like

any^ equality

function,

equals^ should

be^ reflexive,

symmetric,

and^ transitive.

Function

equals

in^ class

Animal

Animal^14 a0 name^

age Animal(String, int)equals()toString()…

Object equals(Object)

public^ class

Animal { /** =^ “h is an Animal with the samevalues in its fields as this Animal” */ public^ boolean

equals (Object h) { if^ (!(h^ instanceof

Animal)) return false

Animal ob= (Animal) h;^ return^ name.equals(ob.name) &&age == ob.age;}

toString()

1.^ Because

of^ h^ is^ an

Animal^ in

spec,

need^ the

test^ h^ instanceof

Animal

Function

equals

in^ class

Animal

Animal^16 a0 name^

age Animal(String, int)equals()toString()…

Object equals(Object)

public^ class

Animal { /** =^ “h is an Animal with the samevalues in its fields as this Animal” */ public^ boolean

equals (Object h) { if^ (!(h^ instanceof

Animal)) return false

Animal ob= (Animal) h;^ return^ name.equals(ob.name) &&age == ob.age;}

toString()

3.^ Use^ String

equals^ function

to^ check

for^ equality

of^ String

values.^ Use

==^ for^ primitive

types