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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.