Summary: Part of a self-assessment test designed to help you determine how much you know about classes, constructors, and accessor methods in Java.
Note: You are viewing an old version of this document. The latest version is available here.
This module is part of a self-assessment test designed to help you determine how much you know about object-oriented programming using Java.
The test consists of a series of questions with answers and explanations of the answers.
The questions and the answers are connected by hyperlinks to make it easy for you to navigate from the question to the answer and back.
I recommend that you open another copy of this document in a separate browser window and use the links to under Listings to easily find and view the listings while you are reading about them.
What output is produced by the program shown in Listing 1 ?
public class Ap090{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = NewClass();
System.out.println(obj);
}//end makeObj()
}// end class
class NewClass{
public String toString(){
return "An Object";
}//end toString()
}//end NewClass
What output is produced by the program shown in Listing 2 ?
public class Ap091{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass();
System.out.println(obj);
}//end makeObj()
}// end class
Class NewClass{
public String toString(){
return "An Object";
}//end toString()
}//end NewClass
What output is produced by the program shown in Listing 3 ?
public class Ap092{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass();
System.out.println(obj);
}//end makeObj()
}// end class
class NewClass{
public String toString(){
return "An Object";
}//end toString()
}//end NewClass
What output is produced by the program shown in Listing 4 ?
public class Ap093{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass();
System.out.println(obj);
}//end makeObj()
}// end class
class NewClass{
private int x = 2;
public NewClass(int x){
this.x = x;
}//end constructor
public String toString(){
return "Object containing " + x;
}//end toString()
}//end NewClass
What output is produced by the program shown in Listing 5 ?
public class Ap094{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
Subclass obj = new Subclass();
System.out.println(obj);
}//end makeObj()
}// end class
class Superclass{
private int x;
public Superclass(int x){
this.x = x;
}//end constructor
public String toString(){
return "Object containing " + x;
}//end toString()
public void setX(int x){
this.x = x;
}//end setX()
}//end Superclass
class Subclass extends Superclass{
public Subclass(){
setX(2);
}//end noarg constructor
}//end Subclass
What output is produced by the program shown in Listing 6 ?
public class Ap095{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass(5);
System.out.println(obj);
}//end makeObj()
}// end class
class NewClass{
private int x = 2;
public NewClass(){
}//end constructor
public NewClass(int x){
this.x = x;
}//end constructor
public String toString(){
return "Object containing " + x;
}//end toString()
}//end NewClass
What output is produced by the program shown in Listing 7 ?
public class Ap096{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass();
System.out.println(obj);
}//end makeObj()
}// end class
class NewClass{
private int x;
private double y;
private boolean z;
public String toString(){
return "Object containing " +
x + ", " +
y + ", " + z;
}//end toString()
}//end NewClass
What output is produced by the program shown in Listing 8 ?
public class Ap097{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass(5);
System.out.println(obj.getX());
}//end makeObj()
}// end class
class NewClass{
private int x = 2;
public NewClass(){
}//end constructor
public NewClass(int x){
this.x = x;
}//end constructor
public int getX(){
return x;
}//end getX()
}//end NewClass
What output is produced by the program shown in Listing 9 ?
public class Ap098{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass();
obj.setX(10);
System.out.println(obj.getX());
}//end makeObj()
}// end class
class NewClass{
private int y;
public void setX(int y){
this.y = y;
}//end setX()
public int getX(){
return y;
}//end getX()
}//end NewClass
What output is produced by the program shown in Listing 10 ?
public class Ap099{
public static void main(
String args[]){
new Worker().makeObj();
}//end main()
}//end class definition
class Worker{
public void makeObj(){
NewClass obj = new NewClass(5);
obj.x = 10;
System.out.println(obj.x);
}//end makeObj()
}// end class
class NewClass{
private int x = 2;
public NewClass(){
}//end constructor
public NewClass(int x){
this.x = x;
}//end constructor
public void setX(int x){
this.x = x;
}//end setX()
public int getX(){
return x;
}//end getX()
}//end NewClass
I recommend that you open another copy of this document in a separate browser window and use the following links to easily find and view the listings while you are reading about them.
This section contains a variety of miscellaneous information.
Financial : Although the Connexions site makes it possible for you to download a PDF file for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please be aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.
Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
A. Compiler Error
Variables declared private really are private
The code in the following fragment attempts to ignore the setter and getter methods and directly access the private instance variable named x in the object referred to by the reference variable named obj .
obj.x = 10;
System.out.println(obj.x);This produces a compiler error. The compiler error produced by JDK 1.3 is reproduced below.
Ap099.java:19: x has private access in
NewClass
obj.x = 10;
^
Ap099.java:20: x has private access in
NewClass
System.out.println(obj.x);
^
C. 10
A setter and a getter
This is a very simple program that uses a setter (modifier or mutator) method named setX to set the value 10 in a property named x that is stored in an instance variable named y in an object instantiated from the class named NewClass ..
The program also uses a getter (accessor) method named getX to get and display the value of the property named x . (Note that according to JavaBeans design patterns, the name of the property is unrelated to the name of variable in which the property value is stored.)
D. 5
Hide your data and expose your methods
For reasons that I won't go into here, good object-oriented design principles state that in almost all cases where an instance variable is not declared to be final , it should be declared private . (A final variable behaves like a constant.)
What is private access?
When an instance variable is declared private , it is accessible only by methods of the class in which it is defined. Therefore, the only way that the "outside world" can gain access to a private instance variable is by going through an (usually public ) instance method of the object.
Accessor, modifier, mutator, setter, and getter methods
Historically, methods that have been defined for the purpose of exposing private instance variables to the outside world have been referred to as accessor and modifier methods. (Modifier methods are also sometimes called mutator methods.)
(Note that since the advent of Sun's JavaBeans Component design patterns, these methods have also come to be known as getter methods and setter methods in deference to the design-pattern naming conventions for the methods.)
A private instance variable with an initializer
The class named NewClass declares a private instance variable named x and initializes its value to 2, as shown in the following code fragment:
private int x = 2;Two constructors
The class contains both a noarg constructor and a parameterized constructor as shown in the following fragment:
public NewClass(){
}//end constructor
public NewClass(int x){
this.x = x;
}//end constructorCalling the noarg constructor
If an object of the class is instantiated by calling the noarg constructor, the initial value of 2 remains intact, and that object contains an instance variable with an initial value of 2.
Calling the parameterized constructor
If an object of the class is instantiated by calling the parameterized constructor, the initial value of 2 is overwritten by the value of the incoming parameter to the parameterized constructor. In this case, that value is 5, because the object is instantiated by the following code fragment that passes the literal value 5 to the parameterized constructor. Thus, the initial value of the instance variable in that object is 5.
NewClass obj = new NewClass(5);A getter method
Because the instance variable named x is private , it cannot be accessed directly for display by the code in the makeObj method of the Worker class. However, the NewClass class provides the following public getter or accessor method that can be used to get the value stored in the instance variable.
(The name of this method complies with JavaBeans design patterns. If you examine the name carefully, you will see why Java programmers often refer to methods like this as getter methods.)
public int getX(){
return x;
}//end getX()Calling the getter method
Finally, the second statement in the following code fragment calls the getter method on the NewClass object to get and display the value of the instance variable named x .
NewClass obj = new NewClass(5);
System.out.println(obj.getX());C. Object containing 0, 0.0, false
Default initialization values
The purpose of this question is to confirm that you understand the default initialization of instance variables in an object when you don't write code to cause the initialization of the instance variable to differ from the default.
By default, all instance variables in a new object are initialized with default values if you don't provide a constructor (or other mechanism) that causes them to be initialized differently from the default.
C. Object containing 5
A parameterized constructor
This program illustrates the straightforward use of a parameterized constructor.
The class named NewClass defines a parameterized constructor that requires an incoming parameter of type int .
(For good design practice, the class also defines a noarg constructor, even though it isn't actually used in this program. This makes it available if needed later when someone extends the class.)
Both constructors are shown in the following code fragment.
public NewClass(){
}//end constructor
public NewClass(int x){
this.x = x;
}//end constructorThe parameterized constructor stores its incoming parameter named x in an instance variable of the class, also named x .
(The use of the keyword this is required in this case to eliminate the ambiguity of having a local parameter with the same name as an instance variable. This is very common Java programming style that you should recognize and understand.)
Call the parameterized constructor
The following code fragment calls the parameterized constructor, passing the literal int value of 5 as a parameter.
NewClass obj = new NewClass(5);Hopefully you will have no difficulty understanding the remaining code in the program that causes the value stored in the instance variable named x to be displayed on the computer screen.
A. Compiler Error
If you define any constructors, ...
The discussion for Question 4 explained that if you define any constructor in a new class, you must define all constructors that will ever be needed 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 explicitly use 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 system automatically calls the noarg constructor on superclasses of that class up to and including the 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 into account, 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(){
A. Compiler Error
Constructors
Java uses the following kinds of constructors:
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 be provided on your behalf. This constructor requires no argument, and it is typically used in conjunction with the new operator to create an instance of the class using statements such as the following:
NewClass obj = new NewClass();The default constructor
The default constructor typically does the following:
Are you satisfied with default values?
As long as you are satisfied with the default initialization of all instance variables belonging to the object, there is no need for you to define a constructor of your own.
However, in the event that you have initialization needs that are not satisfied by the default constructor, you can define your own constructor. Your new constructor may or may not require arguments. (In case you have forgotten, the name of the constructor is always the same of the name of the class in which it is defined.)
A non-default noarg constructor
If your new constructor doesn't require arguments, you may need to write code that performs initialization in ways that differ from the default initialization. For example, you might decide that a particular double instance variable needs to be initialized with a random number each time a new object is instantiated. You could do that with a constructor of your own design that doesn't take arguments by defining the constructor to get initialization values from an object of the Random class.
A parameterized constructor
If your new constructor does take arguments, (a parameterized constructor) you can define as many overloaded versions as you need. Each overloaded version must have a formal argument list that differs from the formal argument list of all of the other overloaded constructors for that class.
(The rules governing the argument list for overloaded constructors are similar to the rules governing the argument list for overloaded methods, which were discussed in a previous module.)
Use parameter values for initialization
In this case, you will typically define your parameterized constructors to initialize some or all of the instance variables of the new object using values passed to the constructor as parameters.
What else can a constructor do?
You can also cause your new constructor to do other things if you so choose. For example, if you know how to do so, you could cause your constructor (with or without parameters) to play an audio clip each time a new object is instantiated. You could use a parameter to determine which audio clip to play in each particular instance.
The punch line
So far, everything that I have said is background information for this program. Here is the punch line insofar as this program is concerned.
If you define any constructor in your new class, you must define all constructors that your new class will ever need.
If you define any constructor, the default constructor is no longer provided on your behalf. If your new class needs a noarg constructor (and it probably does, but that may not become apparent until later when you or someone else extends your class) you must define the noarg version in addition to the other overloaded versions that you define.
A violation of the rule
This program violated the rule given above. It defined the parameterized constructor for the class named NewClass shown below
public NewClass(int x){
this.x = x;
}//end constructorHowever, the program did not also define a noarg constructor for the NewClass class.
Calling the noarg constructor
The code in the makeObj method of the Worker class attempted to instantiate a new object using the following code:
NewClass obj = new NewClass();Since the class definition didn't contain a definition for a noarg constructor, the following compiler error was produced by JDK 1.3.
Ap093.java:18: cannot resolve symbol
symbol : constructor NewClass
()
location: class NewClass
NewClass obj = new NewClass();
C. An Object
We finally got it right!
Did you identify the errors in the previous two programs before looking at the answers?
This program declares the class named NewClass correctly and uses the new operator correctly in conjunction with the default noarg constructor for the NewClass class to create a new instance of the class (an object) .
Making the class public
One of the things that I could do differently would be to make the declaration for the NewClass class public (as shown in the following code fragment) .
public class NewClass{
public String toString(){
return "An Object";
}//end toString()
}//end NewClassI am a little lazy
The reason that I didn't declare this class public (and haven't done so throughout this series of modules) is because the source code for all public classes and interfaces must be in separate files. While that is probably a good requirement for large programming projects, it is overkill for simple little programs like I am presenting in this group of self-assessment modules.
Dealing with multiple files
Therefore, in order to avoid the hassle of having to deal with multiple source code files for every program, I have been using package-private access for class definitions other than the controlling class (the controlling class is declared public) . Although I won't get into the details at this point, when a class is not declared public, it is common to say that it has package-private access instead of public access.
A. Compiler Error
Java is a case-sensitive language
Java keywords must be written exactly as specified. The keyword class cannot be written as Class , which is the problem with this program.
The inappropriate use of the upper-case C in the word Class caused the following compiler error.
Ap091.java:25: 'class' or 'interface' expectedClass NewClass{The solution to the problem
This problem can be resolved by causing the first character in the keyword class to be a lower-case character as shown in the following code fragment.
class NewClass{
public String toString(){
return "An Object";
}//end toString()
}//end NewClassA. Compiler Error
Instantiating an object
There are several ways to instantiate an object in Java:
Of all of these, the last two are by far the most common.
What you cannot do!
You cannot instantiate a new object using code like the following code fragment that was extracted from this program.
NewClass obj = NewClass();This program produces the following compiler error:
Ap090.java:18: cannot resolve symbol
symbol : method NewClass ()
location: class Worker
NewClass obj = NewClass();
The solution to the problem
This problem can be solved by inserting the new operator to the left of the constructor as shown in the following code fragment.
NewClass obj = new NewClass();-end-