This is a very basic question mainly asked in Beginner level interviews (1 to 2 years of experience).
The interviewer can twist the question as below as well.
Answer:
Here are few differences between Constructor and methods in bullet points.
Constructor:
1. Name of constructor should be same as class name
2. Constructor is used to initialize class members (variables). It is used to create an instance of a class.
3. Returns nothing. Hence no return type.
4. Called implicitly at the time of object creation
Method:
1. Name is independent from class name
2. Method is having some functionality. It is used to perform some operations.
3. Must return something. If nothing to return, explicitly return type should be void.
4. Need to be called explicitly
The interviewer can twist the question as below as well.
- Difference between method and constructor in c#
- Difference between constructor and method in dotnet with example
- Difference between methods and constructors in object oriented programming
Answer:
Here are few differences between Constructor and methods in bullet points.
Constructor:
1. Name of constructor should be same as class name
2. Constructor is used to initialize class members (variables). It is used to create an instance of a class.
3. Returns nothing. Hence no return type.
4. Called implicitly at the time of object creation
Method:
1. Name is independent from class name
2. Method is having some functionality. It is used to perform some operations.
3. Must return something. If nothing to return, explicitly return type should be void.
4. Need to be called explicitly