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,

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

 

Example for method overloading and constructor overloading

 

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. 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";
	}

	// parametrized 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

 

References:

https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html
http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.7.1-510-D.1
https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Read Also:

Happy Coding !!
Happy Learning !!

Java: Private Constructor
Java: Constructor chaining with example