Docsity

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

Toggle sidebar
bg1
Classes๎˜ƒwe๎˜ƒwork๎˜ƒwith๎˜ƒtoday๎˜ƒ
1๎˜ƒ
Object
Animal
Dog Cat
class hierarchy:
Work๎˜ƒwith๎˜ƒa๎˜ƒ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๎˜ƒ
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