<< Chapter < Page Chapter >> Page >

Program: Inheritance03.java

// Learning Object Inheritance03 //    dynamic dispatchingclass 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 Inheritance03 {     public static void main(/*String[] args*/) {         Particle  p = new Particle(10);        AParticle a = new AParticle(20, 2.0);         BParticle b = new BParticle(30, 3);        CParticle c = new CParticle(40, 4, true);          p.newPosition(10);         int pPosition = p.position;        p = a;         p.newPosition(10);        int aPosition = p.position;         p = b;        p.newPosition(10);         int bPosition = p.position;        p = c;         p.newPosition(10);        int cPosition = p.position;     }}
  • The objects are created.
  • The references to the objects are assigned one-by-one to the variable p , and then the method newPosition is invoked using p . The modified value of position is assigned to a variable.
  • Check that the call on p invokes the method defined in class Particle .
  • After assigning a to p , check that the call invokes the method defined in the class AParticle ; this method overrides the method declared in class Particle . Although the type of p is class Particle , it holds a reference to an object whose type is class AParticle so the method of that class is called.
  • Note that as a result of the assignment, the object of type Particle has become garbage.
  • After assigning b to p , check that the call invokes the method defined in the superclass Particle ; since the method was not overridden in BParticle , the method called is the one inherited from the superclass.
  • After assigning c to p , check that the call invokes the method defined in the class CParticle ; this method overrides the method declared in class Particle . Although the type of p is class Particle , it holds a reference to an object whose type is class CParticle so the method of that class is called.

Exercise Add an assignment of c to b and call b.newPosition(10) . What is the value now of b.position ?

Downcasting

Concept A variable of the type of a class can reference an object of the type of a subclass , but this variable cannot be used to access fields declared in the subclass.Nevertheless, the object “remembers” its type, even if it is assigned to a variable of the type of its superclass, and the type can be “recovered”by casting to a variable of the type of the subclass. This is called downcasting because the cast is “down” the derivation hierarchy.

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Learning objects for java (with jeliot). OpenStax CNX. Dec 28, 2009 Download for free at http://cnx.org/content/col10915/1.2
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Learning objects for java (with jeliot)' conversation and receive update notifications?

Ask