<< Chapter < Page | Chapter >> Page > |
Program: Inheritance04.java
// Learning Object Inheritance04
// downcastingclass Particle {
int position;
Particle(int p) { position = p;
}
void newPosition(int delta) { position = position + delta;
}}
class AParticle extends Particle {
double spin;
AParticle(int p, double s) { super(p);
spin = s; }
void newPosition(int delta) {
if (spin < delta)
position = position + delta; }
}
class BParticle extends Particle { int charge;
BParticle(int p, int c) {
super(p); charge = c;
}}
class CParticle extends BParticle {
boolean strange;
CParticle(int p, int c, boolean s) { super(p, c);
strange = s; }
void newPosition(int delta) {
if (strange) position = position * charge;
}}
class Inheritance04 {
public static void main(/*String[] args*/) {
Particle p = new Particle(10); AParticle a = new AParticle(20, 2.0);
int pPosition = p.position;
p = a; int paPosition = p.position;
a = (AParticle) p; int aPosition = a.position;
double sSpin = a.spin; }
}
In this program, we take an object of the type of the
subclass
AParticle
and assign its reference to the variable
p
of the type of the superclass
Particle
. The object's
actual type is recovered by downcasting from
p
to
a
.
p.position
is stored in a variable.a
is assigned to
p
.
Note that the arrows from the representation of both variables point tothe same object of type
AParticle
, and that the other objectis garbage.p.position
is accessed, it refers to the value that
is in
a.position
.p
can be cast to the type
AParticle
and assigned to
a
. Although
p
is declared to hold references to objects of
type
Particle
, the object was really of the subclass
AParticle
.position
and
spin
can be accessed
through
a
.
Exercise What happens if you try to access
p.spin
after
a
has been assigned to
p
?
Exercise Add the statement
BParticle b = (BParticle) p
after the assignment of
a
to
p
. Does the program compile
successfully? Does it run successfully? Explain the results.
Concept A heterogeneous data structure is one that can hold elements of different types. A data structure whose elements are of the type of a class can holdreferences to objects of any subclass of that class.
Program: Inheritance05.java
// Learning Object Inheritance05
// heterogeneous data structuresclass Particle {
int position;
Particle(int p) { position = p;
}
void newPosition(int delta) { position = position + delta;
}}
class AParticle extends Particle {
double spin;
AParticle(int p, double s) { super(p);
spin = s; }
void newPosition(int delta) {
if (spin < delta)
position = position + delta; }
}
class BParticle extends Particle { int charge;
BParticle(int p, int c) {
super(p); charge = c;
}}
class CParticle extends BParticle {
boolean strange;
CParticle(int p, int c, boolean s) { super(p, c);
strange = s; }
void newPosition(int delta) {
if (strange) position = position * charge;
}}
class Inheritance05 {
public static void main(/*String[] args*/) {
Particle[] p = new Particle[4]; p[0] = new Particle(10); p[1] = new AParticle(20, 2.0); p[2] = new BParticle(30, 3); p[3] = new CParticle(40, 4, true); int i = 0;
p[i++].newPosition(10);
p[i++].newPosition(10);
p[i++].newPosition(10);
p[i].newPosition(10);
}}
Notification Switch
Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?