<< Chapter < Page Chapter >> Page >

The discussion for Question 4 explained that if you define any constructor in a new class, you must define all constructors that will ever beneeded for that class. When you define one or more constructors, the default noarg constructor is no longer provided by the system on your behalf.

Question 4 illustrated a simple manifestation of a problem arising from the failure to define a noarg constructor that would be needed later. The reason that it was needed later was that the programmer attempted to explicitlyuse the non-existent noarg constructor to create an instance of the class.

A more subtle problem

The problem in this program is more subtle. Unless you (or the programmer of the superclasses) specifically write code to cause the system to behave otherwise, each time you instantiate an object of a class, the systemautomatically calls the noarg constructor on superclasses of that class up to and includingthe class named Object . If one or more of those superclasses don't have a noarg constructor, unless the author of the subclass constructor has taken this intoaccount, the program will fail to compile.

Calling a non-existing noarg constructor

This program attempts to instantiate an object of a class named Subclass , which extends a class named Superclass . By default, when attempting to instantiate the object, the system will attempt to call a noarg constructor defined in Superclass .

Superclass has no noarg constructor

The Superclass class defines a parameterized constructor that requires a single incoming parameter of type int . However, it does not also define a noarg constructor. Because the parameterized constructor is defined, the default noarg constructor does not exist. As a result, JDK 1.3 produces the following compiler error:

Ap094.java:40: cannot resolve symbol symbol : constructor Superclass ()location: class Superclass public Subclass(){

Back to Question 5

Answer 4

A. Compiler Error

Explanation 4

Constructors

Java uses the following kinds of constructors:

  • Those that take arguments, often referred to as parameterized constructors , which typically perform initialization on the new object using parameter values.
  • Those that don't take arguments, often referred to as default or noarg constructors, which perform default initialization on the new object.
  • Those that don't take arguments but perform initialization on the new object in ways that differ from the default initialization.

Constructor definition is optional

You are not required to define a constructor when you define a new class. If you don't define a constructor for your new class, a default constructor will beprovided on your behalf. This constructor requires no argument, and it is typically used in conjunction with the new operator to create an instance of theclass using statements such as the following:

NewClass obj = new NewClass();

The default constructor

The default constructor typically does the following:

  • Calls the noarg constructor of the superclass
  • Assists in the process of allocating and organizing memory for the new object
  • Initializes all instance variables of the new object with the following four default values:
    • numeric = 0,
    • boolean = false,
    • char = all zero bits
    • reference = null

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, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask