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

Both method and constructor wrap bunch of code but the constructor is very different than the normal method in Java. Before seeing the 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 an 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) The first difference between method vs constructor in Java is that name of the constructor must be same with the name of the Class but there is no such requirement for a method in Java. methods can have any arbitrary name in Java.

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

3) The 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.

Difference between method vs constructor in Java interview question4) Unlike a method, constructor yet declared in class doesn't consider as a member of Class. Constructors are not inherited by child classes but methods are inherited by child classes until they are made private. in 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 a constructor explicitly. no such thing as the method, they have their 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.

Further Learning
SOLID Principles of Object Oriented Design
Absolute Introduction to Object Oriented Programming in Java
Java - Object Oriented Programming [For Absolute Beginners]

Other Java fundamental tutorial from java67 site

Thanks for reading this article so far. If you like an object-oriented programming tutorial then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note.

P. S. - If you are serious about learning object-oriented programming and looking for a free online course to start with then you can also check this FREE Object Oriented Programming (OOPs) for JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

No comments:

Post a Comment