









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 16
This page cannot be seen from the preview
Don't miss anything!










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()
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
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.
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
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
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)
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!
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
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.
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
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