









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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