Method vs Constructor In Java: In this article, we will list the difference between method and constructor in Java. Let us detail out the difference between Method v/s Constructor in tabular form below,
Method vs Constructor In Java
| Method | Constructor |
| Methods are the member function of any class to expose the behaviour of an object | The constructor is a special type of method to initialize objects |
| Methods are invoked using the newly created object | Using constructor, new objects are created |
| Methods are invoked explicitly using newly created objects | The constructor is called implicitly while creating objects using ‘new’ keyword |
| Methods should or must have return type although void | The constructor does not have return type not even void |
| When a class inherits, methods can be overridden | The constructor doesn’t support inheritance and hence overriding is not possible |
| There are no such things like compiler provides methods during compilation | The default constructor is provided by the compiler after compilation if there is no explicit constructor available |
| Name of the methods are different from class name (99.9 %) but can have the same name as that of class | Name of the constructor must be the same as that of the class name |
| There is no such thing for methods in Java | The constructor is called in the order and this is known as constructor chaining in Java |
| Methods are explicitly invoked using newly created reference objects | To invoke another constructor in the chaining process, this(args) and super(args) keywords are used |
| Private methods cannot be overridden in the inheritance concept | The private constructor is used for the singleton design pattern which restricts to create more than one object of that class |
Example of method overloading and constructor overloading
1. Example of 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 of 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
Other Search: Java JDK, verify java, java verify, Java 9, JDK 8 download, Java documentation, James Gosling, features of java, java tutorial pdf, java programming pdf