Constructors

Constructors are a new concept for people doing structured programming. Constructors do not normally exist in non-OO languages such as C and Basic. Earlier we spoke about special methods that are used to construct objects. In Java, C# and C++, as well as in other OO languages, constructors are methods that share the same name as the class and have no return type. For example, a constructor for the Cabbie class would look like this:

public Cabbie(){
    /* code to construct the object */
}

The compiler will recognize that the method name is identical to the class name and consider the method a constructor.

Return Value

Note again that a constructor does not have a return value. If you provide a return value, the compiler will not treat ...

Get Object-Oriented Thought Process, The, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.