Tuesday, September 26, 2023

Difference between Method and Constructor in Java and OOP? Example

What is the difference between method and constructor in Java is a very common question in beginner-level Java interviews with 2 to 3-year experience. Since the constructor is kind of special and it has its own properties that separate it from any normal Java method, this question makes sense. The main difference between a Constructor and a Method is that you need to call the method explicitly but the constructor is called implicitly by the Java programming language during object instantiation. This is one of the special properties of constructors in Java and that's why all the object initialization code is put inside the constructor. 

By the way, It doesn't mean you can not call Constructor; if you have an overloaded constructor then you can call them using this keyword as this() constructor, you can even call superclass constructor using super() keyword, in-fact that is done automatically by Java compiler if no explicit constructor is called and that is known as constructor chaining in Java

Like many other beginner-level questions like Vector vs ArrayList, We will see the difference between method and constructor in Java in point form to understand the important properties of each of them clearly.

By the way, if you are new to Java and Object-Oriented Programming then I suggest you first go through a comprehensive Java course or training class like The Complete Java Masterclass for more structured learning. 

It's one of the most up-to-date courses and with 80 hours of content, it's also the most comprehensive course, but it's very affordable. You can get this course for just $9.9 on Udemy sales which happens every now and then. 




Difference between Method vs Constructor in Java

Both method and constructor wrap a 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 the calling mechanism of the Constructor is different from the method. you can call an overloaded constructor as this(argument) or super(argument) based upon whether it's declared on the same class or superclass 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 the access modifier in Java then see the difference between public, protected, and private keywords in Java.

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

1. The first difference between method vs constructor in Java is that name of the constructor must be the same as 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 it's 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.

4., Unlike a method, a constructor yet declared in class doesn't consider a member of the 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 the class on which they are declared. 

Similarly, a private constructor means you can not create an object of that class from outside, this is one of the techniques used to implement the Singleton pattern in Java.


5. Another difference between method and constructor in Java is that the 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.




Method and Constructor Example

Now, let's see a simple example of a method and constructor in Java:

public class Prime {
    private int number;

    // constructor
    public Prime(int number) {
        //initiliaztion
        this.number = number;
    }

    // a method
    public boolean isPrime(int number) {
        return true;
    }
}

You can see that method has a return type as shown in the above example while the constructor doesn't have any return type, but both method and constructor can take arguments. Also, the constructor name must be the same as the class name. 


Here is a nice summary of the difference between method and constructor in Java:

Difference between Method and Constructor in Java and OOP?

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


Other Java fundamental tutorial from java67 site

Thanks for reading this article so far. If you like this 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 the JAVA Interviews course on Udemy. It's completely free and you just need a free Udemy account to join this course. 

Now it's your turn, let's see if you read this article carefully, what is the return type of a Constructor in Java? Let me know your answer in comments. 

1 comment:

  1. I think the most meaningful difference between a method and a constructor in Java is that method has return type while constructor doesn't have any return type. Also the name of method can be anything but constructor name must be same as class name.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.