0 votes
35.5k views
in Java Interview Question by

Its a common question which is asked in interview every time , how you would like to explain this?

0
by
Constructor cannot be inherited by subclasses but method can.

5 Answers

–1 vote
by (2.9k points)

These are the main difference between constructor and a method :

 

Constructor : 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.

 

 

Method : 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.

0 votes
by
Constructors create an object/instance.  (Non-static) methods require an existing object/instance to operate.  That's one (of many) differences.
0 votes
by

Both Constructor & Method wraps block of code, but Constructor is very different than normal method. Before 

seeing the difference lets see some common things: 

1.Both method and Constructor can be overloaded or overridden in Java. Though calling mechanism of Constructor 

is different than method. you can call an overloaded constructor as this(argument) or super(argument) based upon 

whether its declared on same class or super class but for calling overloaded method or overridden method, you simply 

use method name. 

2.You can make your constructor public, protected, private similar to your method in Java. By the way If you are not 

familiar with access modifier in Java 

Differences: 
---------------- 
1) Constructor is mainly used to initialize the instance whereas method is used to expose the behavior of an object. 
2) Constructor is implicitly invoked whereas method is explicitly invoked. 
3) There must not be any return type in constructor but method must have a return type. 
4) Constructor name must be same as class name but method name may be same or different. 
5) Constructor is implicitly provided by java compiler if you don't specify any constructor in the java class but 

method is never provided by java compiler.

0
by
Constructor is invoked using new operator and method is invoked via dot(.) operator
0 votes
by
Constructor is used to Construct the Object like "Taking account in a bank and depositing initial amount in the account"

Method is nothing but, accessing account later on like , deposits , withdrawls....
0 votes
by
Difference:-  If we don't have default constructor then we can't use newInstance() method of "Class" class(means: The class who's name is Class) to create object. To get it we have to use newInstance() method of "Constructor" class(means: The class who's name is Constructor). That is because Class.newInstance() method always uses default constructor to instantiate it BUT Constructor.newInstance(param) method can use any constructor of class which is specified as param.
AND there is no such case in terms of METHODS.


Similarity:- To access private methods and constructors outside the class or even package the procedure is same ie. setAccessible(true) method in Reflection.

Not a Member yet?

Ask to Folks Login

My Account

Your feedback is highly appreciated