<< 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


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

anyone know any internet site where one can find nanotechnology papers?
Damian Reply
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
is Bucky paper clear?
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
so some one know about replacing silicon atom with phosphorous in semiconductors device?
s. Reply
Yeah, it is a pain to say the least. You basically have to heat the substarte up to around 1000 degrees celcius then pass phosphene gas over top of it, which is explosive and toxic by the way, under very low pressure.
Do you know which machine is used to that process?
how to fabricate graphene ink ?
for screen printed electrodes ?
What is lattice structure?
s. Reply
of graphene you mean?
or in general
in general
Graphene has a hexagonal structure
On having this app for quite a bit time, Haven't realised there's a chat room in it.
what is biological synthesis of nanoparticles
Sanket Reply
what's the easiest and fastest way to the synthesize AgNP?
Damian Reply
types of nano material
abeetha Reply
I start with an easy one. carbon nanotubes woven into a long filament like a string
many many of nanotubes
what is the k.e before it land
what is the function of carbon nanotubes?
I'm interested in nanotube
what is nanomaterials​ and their applications of sensors.
Ramkumar Reply
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!

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?