Posts

Showing posts with the label default constructor

Constructor in Java: Types, Rules, and Examples

Constructor in Java A constructor in Java is a special type of method that is used to initialize the instance variables of a class. It is automatically called when an object is created. Unlike normal methods, constructors are mainly used to set initial values for an object. Rules of Constructor   A constructor must follow these rules: The constructor name must be the same as the class name A constructor does not have any explicit return type (not even void) It is called automatically during object creation It is used to initialize instance-level data Types of Constructors in Java     Constructors are classified into three types: Default Constructor Zero Parameter Constructor Parameterized Constructor 1. Default Constructor ✅ Definition A default constructor is a constructor that is implicitly created by the Java compiler if no constructor is defined by the programmer. It has the same name as the class It has no parameters Its body contains a super() statement It supports ...