A constructor is a special method (not appropriate to call it a method but for the sake of analogy) whose task is to initialize the object of its class.It is special because its name is the same as the class name.They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created.
S.No. | Constructors | Methods | |
1. | Purpose | Create an instance of a class | Group Java statements |
2. | Modifiers | Cannot be abstract, final, native, static, or synchronized | Can be abstract, final, native, static, or synchronized |
3. | Return Type | No return type, not even void | void or a valid return type |
4. | Name | Same name as the class (first letter is capitalized by convention) — usually a noun | Any name except the class. Method names begin with a lowercase letter by convention — usually the name of an action |
5. | this | Refers to another constructor in the same class. If used, it must be the first line of the constructor | Refers to an instance of the owning class. Cannot be used by static methods. |
6. | super | Calls the constructor of the parent class. If used, must be the first line of the constructor | Calls an overridden method in the parent class |
7. | Inheritance | Constructors are not inherited | Methods are inherited |