Back to Top

Sunday, 15 January 2012

JAVA : What is the difference between a constructor and a method?

  A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.

  A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.

1 Responses to “ JAVA : What is the difference between a constructor and a method? ”

jitendrakumar pandey said...
8 September 2013 at 18:18

Constructor:
Constructor must have same name as Class name.
Constructor does not have return type not even void.
Constructor does not support inheritance.
Constructor does not support overriding concept.
No need to call constructor explicitly it is called automatically at the time of object creation.
Generally we need constructor to write initialization logic.not business logic.
if we didn't provide constructor then JVM automatically add default constructor.
-----------------------------------------------
Method:
Method can have any name.
Method can have any return type.
We write method for modular programming.
In Program when repetitive statement occurred better to go for method.
it can be used to write business logic.
Need explicit calling by using object or Class name.


Post a Comment

Popular Posts

Subscribe via Email
Subscribe Java Interview Questions via Email
All Rights Reserved JAVA INTERVIEW QUESTIONS | Privacy Policy | Anijit Sarkar