4.3. Constructors

Constructors are a special breed of methods that are used for creating object instances. In C# and Java, objects can have multiple constructors. In the absence of a constructor, the JVM and CLR runtimes provide a default public no-argument constructor. Constructors can call each other.

In Listing 4.6, the class has three constructors. The least specific constructor in turn calls the next more specific constructor and so on, thereby leaving the actual implementation in one constructor (the most specific one).

Listing 4.6. Multiple Constructors (C#)
 using System; public class Cube { int width, height, length; public Cube() : this(60,60,60) {} public Cube(int width, int length) : this(width,length,60) {} public Cube(int width, ...

Get .NET for Java Developers: Migrating to C# 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.