<< Chapter < Page Chapter >> Page >

Exercise Modify the class to include a constructor with one parameter, the name, and with a default song length of three minutes. Can this constructor call the two-parameter constructor which in turn calls the three-parameter constructor? Can a constructor call two other constructors, one after another?

Explicit default constructors

Concept When no constructor is explicitly written in a class, a default implicit constructor with no parameters exists; this constructor does nothing. If, however, one or more explicit constructors are given, there is no longer a constructor with no parameters. Should you want one, you have to write it explicitly.

Program: Constructor05.java

// Learning Object Constructor05 //    explicit default constructorsclass Song {     String name;    int seconds;     double pricePerSecond;    double price;      Song(String n, int s, double p) {         name = n;        seconds = s;         pricePerSecond = p;        price = computePrice();     }      Song() {        this("No song", 0, 0.0);     }      private double computePrice() {        return seconds * pricePerSecond;     }}  public class Constructor05 {     public static void main(/*String[] args*/) {         Song song1 = new Song();    } }

This program includes an explicit constructor with no parameters that calls the constructor with three parameters to perform initialization.

  • The variable song1 is allocated and contains the null value.
  • Memory is allocated for the four fields of the object and default values are assigned to the fields.
  • The constructor is called with no actual parameters; the call is resolved so that it is the second constructor that is executed.
  • Three constant values are used to call the first constructor. The method name this means: call a constructor from this class. This constructor initializes the first three fields from the parameters, and the value of the fourth field is computed by calling the method computePrice .
  • The constructor returns a reference to the object, which is stored in the variable song1 .

Exercise Modify the class so that the constructor without parameters obtains initial values from the input.

Constructors for subclasses

Concept Constructors are not inherited. You must explicitly define a constructor for a subclass (with or withoutparameters). As its first statement, the constructor for the subclass must call a constructor for the superclass using the method super .

Program: Constructor06A.java

// Learning Object Constructor06A //    constructors for subclassesclass Song {     String name;    int seconds;     double pricePerSecond;    double price;      Song(String n, int s, double p) {         name = n;        seconds = s;         pricePerSecond = p;        price = computePrice();     }      private double computePrice() {        return seconds * pricePerSecond;     }}  class DiscountSong extends Song {     double discount;      DiscountSong(String n, int s, double p, double d) {        super(n, s, p);         discount = d;    }      private double computePrice() {         return seconds * pricePerSecond * discount;    } }  public class Constructor06A {    public static void main(/*String[] args*/) {        DiscountSong song1 = new DiscountSong("Waterloo", 164, 0.01, 0.8);         double price = song1.price;    } }

Questions & Answers

how does hormones produce in our body
Shubham Reply
what is an organelle
Shemia Reply
it is the membranious componet within a cell... eg mitochondrion
Joseph
what is the Endothelial cell?
Jumana Reply
classification of heart
NARESH Reply
classification of heart 5 points
NARESH
wow
Ashley
wym by classification?
Ashley
differences between meiosis and mitosi
Daniel Reply
how much function it have
Kerlon Reply
what is a cell membrane
Kerlon
call membrane is outer most part of cell
PRIYA
cell membrane consists from phospholipids and proteins
Jumana
what is elastic constant in muscle in human of body
which topic you at now
Jodyann
Cells
david
what is chromosome
jeff Reply
a thread-like structure of nucleic acids and protein found in the nucleus of most living cells, carrying genetic information in the form of genes
Niola
what are the medical application of hooks law experiment in human body
what is the physical reason that make people get fractured in bones or rupture of tissues
Hooke's Law is the Law of Elasticty. In scientific terms, Hooke's Law is the displacement or size of the deformation is directly proportional to the deforming force or load. Under these conditions the object returns to its original shape and size upon removal of the load
Niola
what is DNA
abdala
deoxyribonucleic acid, a self-replicating material which is present in nearly all living organisms as the main constituent of chromosomes. It is the carrier of genetic information.
Niola
what is prokaryotic cell
Farid
greetings
Nsoh
am Modesta
Nsoh
what is deamination
Mohamed
what is defusion
Rashima Reply
diffusion is the movement of molecules from a region of higher concentration to a region of lower concentration through a permeable membrane
Dein-aboh
and osmosis is the same thing ?
Rashima
nope
Ashley
The spreading of something more widely
R0se
Osmosis is the movement of water molecules from a region of higher concentration to a region of lower concentration
Lavern
What is a color of blood before it is exposed to oxygen?
Lavern
hello there....I have a problem concerning an infection in my feet...so can any one help me?
Ngole Reply
what sort of infection?
Dein-aboh
is the blood pressure increase? why?
Jumana Reply
what is the reason for cardiac arrest
Oseni Reply
stop the heat suddenly
It's usually triggered when there's a malfunction in the heart which causes arrhythmia..hope u get more clarity online
Dein-aboh
what is cardiovascular system
Mohamed
y
cardiovascular system in a lame mans under
Dein-aboh
what y
Mohamed
the heart
Ashley
cardiovascular system is a system that circulates blood and lymph through the body, consisting of the heart, blood vessels, blood, lymph, and the lymphatic vessels and glands
Dein-aboh
thanks dein-aboh perfect
Mohamed
your welcome bro
Dein-aboh
what is the difference between white blood cells and red blood cells?
Gecious Reply
what is the difference between white blood cell and red blood cells
Gecious
idk enlightening me... the red is for oxygen the white is for the immune system
Ashley
enlighten&
Ashley
what are nephrons and their types
Note Reply
when is the actual time that woman can be pregnant?
Jackson Reply
What symptoms occur during menstruation?
Maxey
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!





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