Java – Method v/s Constructor

In this article, we will list the difference between method and constructor in Java

Before moving ahead with the differences, read the detailed concepts about method, method-overloading and constructor, constructor-overloading in the following articles

Let us detail out difference between Method v/s Constructor in tabular form below,

1. Method v/s Constructor:

 
Sr. No.
MethodConstructor
1Methods are member function of any class to expose the behavior of an objectConstructor is a special type of method to initialize objects
2Methods are invoked using newly created objectUsing constructor, new objects are created
3Methods are invoked explicitly using newly created objectsConstructor are called implicitly while creating objects using ‘new’ keyword
4Methods should or must have return type although voidConstructor does not have return type not even void
5When class inherits, methods can be overriddenConstructor doesn’t support inheritance and hence overriding is not possible
6There is no such things like compiler provides methods during compilationDefault constructor are provided by compiler after compilation, if there is no explicit constructor available
7Name of the methods are different from class name (99.9 %) but can have same name as that of className of the constructor must be same as that of class name
8There is no such thing for methods in JavaConstructor are called in order and this is known as constructor chaining in Java
9Methods are explicitly invoked using newly created reference objectsTo invoke other constructor in chaining process, this(args) and super(args) keywords are used
10Private methods cannot be overridden in inheritance conceptPrivate constructor are used for singleton design pattern which restricts to create more than one object of that class

2. Method-Overloading & Constructor-Overloading examples:

2.1 Example on Method-Overloading :

TestJavaOverload.java

package in.bench.resources.java.overload;

package in.bench.resources.constructor.example;

public class TestJavaOverload {

	void add(int num1, float num2) {
		System.out.println("The summation of 2 numbers : "
				+ (num1 + num2));
	}

	void add(int num1, float num2, int num3) {
		System.out.println("The summation of 3 numbers : "
				+ (num1 + num2 + num3));
	}

	public static void main(String args[]) {

		TestJavaOverload t1 = new TestJavaOverload();
		t1.add(12, 16f); // invoking 1st method with 2 arguments
		t1.add(10, 20f, 30); // invoking 1st method with 3 arguments
	}
}

Output:

The summation of 2 numbers : 28.0
The summation of 3 numbers : 60.0

2.2 Example on Constructor-Overloading

Employee.java

package in.bench.resources.constructor.example;

public class Employee {

	// member variables
	int employeeId;
	String employeeName;

	// default constructor
	Employee() {
		System.out.println("Employee class - Inside default constructor");
		this.employeeId = 000;
		this.employeeName = "Employee 0";
	}

	// parameterized constructor
	Employee(int id, String name) {
		System.out.println("Employee class - Inside parametrized constructor");
		this.employeeId = id;
		this.employeeName = name;

	}

	// display() method
	void displayEmployeeInfo() {
		System.out.println("Employee details\nId: "
				+ employeeId
				+ "\t Name: "
				+ employeeName + "\n");
	}

	// main() method - entry point to JVM
	public static void main(String args[]) {
		Employee emp0 = new Employee();
		emp0.displayEmployeeInfo();

		Employee emp1 = new Employee(19, "Rahul Dravid");
		emp1.displayEmployeeInfo();
	}
}

Output:

Employee class >> Inside default constructor
Employee details
Id: 0	 Name: Employee 0

Employee class >> Inside parametrized constructor
Employee details
Id: 19	 Name: Rahul Dravid

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Private Constructor
Java - Constructor chaining with example