Does the class HelloObject have a constructor?

A good answer might be:

Yes. It had to have a constructor since one was used to construct an object.

An Inherited Constructor

Here is where the constructor was used in the main() method:

    HelloObject anObject = new HelloObject();     // 2.  a HelloObject 
                                                  //     is created.
But here is the class definition of HelloObject:
class HelloObject                                  
{                                                  
  void speak()                                     
  { 
    System.out.println("Hello from an object!");
  }
}
There is no constructor described in the class definition. This is OK. All classes will have a constructor even if one is not explicitly included in the class definition. A constructor will be automatically supplied. The way this works is called inheritance and will be discussed in a future chapter.

The constructor you get by inheritance does a great deal of work "behind the scenes" when it is used. It works with the operating system to find main memory the object, sets up that memory as an object, puts in it the variables and methods specified in the class definition, and returns an object reference to your program. All of this is quite complicated, and it is lucky that you don't have to write it.

You will need to include a constructor in the class definition when you need to initialize some variables. This can't be done automatically because, of course, the compiler can't know what you with to initialize them to. When you include a constructor in the class definition, all you need to do is write the statements that initialize variables. The constructor will still do all the "behind the scenes" work because of inheritance.

QUESTION 13:

(Design Question: ) So far, the speak method of the HelloObject class always prints the same string to the monitor. How could this class be improved?

Click Here after you have answered the question