Like every general method, a constructor must also be defined in a class and can be overloaded. However, in terms of functionality and syntax, it is different from a general method. How is a constructor different from a general method?
In terms of functionality and syntax constructor is different from other general methods.
1. In terms of syntax,
(i) Constructor does not have any return type
while other methods does have a return type.
(ii) Constructor name is same as the class name
while other methods can have any name.
(iii) Constructor is invoked implicitly, when the
object of that class is created using the new keyword while other
methods needs to be called by the object of that class.
2. In terms of functionality, constructor is used to initialize the variables of the class while general methods are used to apply operations on the variables.
Hence, we can say, in terms of syntax and functionality, constructor and other general methods are totally different.
Get Answers For Free
Most questions answered within 1 hours.