This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes question from khalid's book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question from khalid Watch "question from khalid New topic
Author

question from khalid's book

smriti singla
Greenhorn

Joined: Nov 14, 2000
Posts: 18
hello
in problem 1.4 (review questions,chapter 1)in Khalid's book it is given that Thing is a class and how many objects and reference variables are created by the following code
Thing item, stuff;
item = new Object();
Thing entity = new Object();
According to me the answer should be no object is created and three reference variables are created as creating an object involves using the new operator togather with a call to constructor of the class(given on page 4 in book under topic "class instantiation") and 'Object()' is not a constructor of class Thing as constructor always has the same name as that of the class.But answers given in the book on page 623 shows that options b and f are true which shows that two objects and three reference variables are created.i could not understand this.please explain this if anybody knows.any help will be highly appreciated.
Thanks
Smriti Singla
lee dalais
Ranch Hand

Joined: Feb 28, 2001
Posts: 67
hi smriti
each time the "new" operator is used, a new object is created.
and u have 3 reference variables.
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320

Corrected a previously reported error on creating a Mammal reference variable and referring it to a Dog object. Thanks for the corerction guys.

Hi
Since all objects are sub classes of "Object" you are indeed calling the constructor of an object.. it just happens to be named Object.
Examples to explain a few points.

You can easily say

Some examples of what will NOT work.

The reason you may be confused is because you are sticking to the rule that the constructor is always the name of the class. In the DEFINITION of a class YES it is always the name of the class (see Mammal and Dog in code above), but when creating an object it can also be the name of the superclass constructor. (also known as the parent class).
One last thing to note about references and object.. if I use the 'all dog' first example I gave you and added the following.

As funny as it my look, the doberman now REFERS to the OBJECT that poodle REFERS to so no new object was created. All that would change in the first example is that we now have:
- 5 references (terrier, collie, poodle, doberman and pitbull)
- 3 objects
(terrier points to an object.)
(pitbull points to an object.)
(poodle AND doberman point to the same object).
Hope this helps.
[This message has been edited by John Bateman (edited March 12, 2001).]


SOURCE CODE should be SURROUNDED by "code" tags.
niraj singh
Ranch Hand

Joined: Feb 07, 2001
Posts: 36
Hi Smriti,
I agree with you that no objects will be created, if what you have asked is as entered in the Khalid book. In fact, it should give a compile error.
I do not agree with John, who has mentioned that the following code will compile -
pitBull = new Mammal();
This code cannot compile.
Mammal is the superclass of Dog, so you can write it as
Mammal pitbull = new Dog();//upcasting Dog object to its superclass.
but you can't write -
Dog pitBull = new Mammal(); // downcasting is not allowed
The basic thing to remember is that, you can upcast an object, you cannot downcast it.
The Object class is the superclass of all classes. All classes are subclasses or the class Object. If you define a class which does not extend any other class, such as-
class MyClass {}
Here, though we are not using the extendskeyword to inherit a class, Java implicitly extends the class MyClass with the class Object.
Now, when we write, Object c = new MyClass();
it is correct, but is incorrect to write -
MyClass c = new Object();
Smriti, could you try posting this question in the Mock Errata section and see what other people have to say about it.
I hope that helps.
Niraj
smriti singla
Greenhorn

Joined: Nov 14, 2000
Posts: 18
hello
thanks for ur replies.I am going to forward this question to Mock Errata section.Thanks for ur advice niraj.
Smriti Singla
Pat Barrett
Ranch Hand

Joined: Jan 03, 2001
Posts: 63
Hello,
I have a version of Khalid's book where question 1.4 is as follows:
Given that Thing is a class, how many objects and reference variables are created by the following code?

The correct answers given are B ( Two objects are created ) and F ( Three reference variables are created ) which are correct.
Is it possible that you are looking at an older version of the book? Mine was published in 2000 and says "covers Java 2 platform" on the lower right hand corner of the cover.
Pat B.
 
GeeCON Prague 2014
 
subject: question from khalid's book