<< Chapter < Page Chapter >> Page >

In this simple program objects of all four classes are created and their fields are read.

  • The objects are created.
  • For each field of each object, a variable is declared in the main method and the value of the field is assigned to it. Check that each value originatesfrom the correct field.

Exercise In CParticle , add the declaration int charge = -1; . Compile and run the program. Is the output different? Explain what happens.

Inheriting methods

Concept Subclasses inherit the methods of its superclasses and can add new methods of its own. You can override an inherited method by writing a new methodwith the same signature as the inherited method.

Program: Inheritance02.java

// Learning Object Inheritance02 //    inheriting and overriding methodsclass 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 Inheritance02 {     public static void main(/*String[] args*/) {         Particle  p = new Particle(10);        AParticle a = new AParticle(10, 2.0);         BParticle b = new BParticle(10, 3);        CParticle c = new CParticle(10, 4, true);          p.newPosition(10);         int pPosition = p.position;        a.newPosition(10);         int aPosition = a.position;        b.newPosition(10);         int bPosition = b.position;        c.newPosition(10);         int cPosition = c.position;    } }

This program calls the method newPosition , which is overridden in AParticle and CParticle but not in BParticle .

  • The objects are created.
  • Method newPosition is invoked for each object and the modified value of position is assigned to a variable.
  • Check that the call on p calls the method defined in class Particle .
  • Check that the call on a calls the method defined in the class AParticle ; this method overrides the method declared in class Particle .
  • Check that the call on b calls 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.
  • Check that the call on c calls the method defined in the class BParticle ; this method overrides the method declared in class Particle .

Exercise Remove the method newPosition from CParticle . Which method is invoked for c.newPosition ?

Exercise Remove the method newPosition from CParticle and add a method with the same signature to BParticle . Which method is invoked for c.newPosition ?

Dynamic dispatching

Concept A variable v of type T can contain a reference to an object of type T or of the type of any subclass of T . When invoking v.m for some method m that is overridden in a subclass, it is the type of the object currently referenced by v (not the type of the variable v ) that determines which method is called. This is called dynamic dispatching because the call is dispatched at runtime.

Questions & Answers

what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
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
what is cell divisoin?
Aron Reply
Diversity of living thing
ISCONT
what is cell division
Aron Reply
Cell division is the process by which a single cell divides into two or more daughter cells. It is a fundamental process in all living organisms and is essential for growth, development, and reproduction. Cell division can occur through either mitosis or meiosis.
AI-Robot
What is life?
Allison Reply
life is defined as any system capable of performing functions such as eating, metabolizing,excreting,breathing,moving,Growing,reproducing,and responding to external stimuli.
Mohamed
what is ecology
Philip Reply
study of interaction between living things and their environment
AI-Robot
the same
Menseh
ecology
Ahmed
so what are the sub topics under it
Lukmon
the draw of microscope and lebel it
Saidu Reply
What's atomic
John Reply
atomic? or atom🙄
joyce
related to or concerning an atom.
Nakita
what is biology?
Wine Reply
what are the important of biology?
Wine
what is Biology
Akech 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