Posts

Showing posts with the label Constructors in Java. Types of constructors .

Constructors in Java. Types of constructors and constructor overloading.

Constructors in Java: Definition : It is a special type of method which is used to creating and initializing the objects . It is called when an instance(object) of the class is created.(instance means object).Every class has a constructor either implicitly or explicitly. Every time an object is created using the new() keyword, at least one constructor is called. Rules for creating Java constructor: 1.Constructor name must be the same as its class name. 2.A Constructor must have no explicit return type. 3.A Java constructor cannot be abstract, static, final, and synchronized. 4.constructor can be overloaded. But it cannot be overriden. Types of constructors: 1.Default Constructor (no-arg) 2.Parameterized Constructor. 3.Copy constructor. 1.Default Constructor: Default constructor is the constructor which doesn’t take any argument.  It has no parameters. Also known as empty constructor. If we do not create any constructor, the Java compiler automatically create a no-...