• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question from khalid's book

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi smriti
each time the "new" operator is used, a new object is created.
and u have 3 reference variables.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
thanks for ur replies.I am going to forward this question to Mock Errata section.Thanks for ur advice niraj.
Smriti Singla
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic