Tuesday, November 6, 2012

Difference between method and constructor in Java

What is difference between method and constructor in Java is very common questions on beginner level Java interviews with 2 to 3 year experience. Since constructor is kind of special and it has it's own properties which separates it from any normal Java method, this question make sense. Main difference between Constructor and Method is that, you need to call method explicitly but constructor is called implicitly by Java programming language during object instantiation. It doesn't mean you can not call Constructor; if you have overloaded constructor than you can call them using  this keyword as this() constructor, you can even call super class constructor using super() keyword, in-fact that is done automatically by Java compiler if no explicitly constructor is called and that is known as constructor chaining in Java. Like many other beginner level questions e.g. Vector vs ArrayList, We will see difference between method and constructor in Java in point form to understand important properties of each of them clearly.

Method vs Constructor in Java

Difference between method vs constructor in Java interview question
Both method and constructor wraps bunch of code but constructor is very different than normal method in Java. Before seeing difference between Constructor and Method, let's see some common things between them.

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 than see Difference between public, protected and private keyword in Java.

Now let's see some important difference between method and constructor in Java :

1) First difference between method vs constructor in Java is that name of constructor must be same with name of the Class but there is no such requirement for method in Java. methods can have any arbitrary name in Java.

2) Second difference between method and constructor in Java is that constructor doesn't have any return type but method has return type and return something unless its void.

3) Third difference between constructor and method in Java is that Constructors are chained and they are called in a particular order, there is no such facility for methods.

4) Unlike method, constructor, yet declared in class doesn't considered as member of Class. Constructors are not inherited by child classes but methods are inherited by child classes until they are made private. on which case they are only visible in class on which they are declared. Similarly private constructor means you can not create object of that class from outside, this is one of the technique used to implement Singleton pattern in Java.

5) Another difference between method and constructor in Java is that special keyword this and super is used to call constructor explicitly. no such thing for method, they have there own name which can be used to call them.

That's all on difference between method and constructor in Java. You can compare method vs Constructor on different points but  main thing is that they are used for object initialization, while method is used to perform a small unit of task.

Other Java fundamental tutorial from java67 site
Difference between Serializable and Externalizable in Java

No comments:

Post a Comment

Java67 Headline Animator