• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo » Difference » Difference Between Method vs Constructor In Java With Example

Difference Between Method vs Constructor In Java With Example

Last Updated on: April 11, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Method vs Constructor In Java

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,

Read Also: Default constructor vs Parametrized constructor In Java

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

    Difference Between
    Difference Between
    Difference Between Unit Testing Vs Functional Testing
    Difference Between Unit Testing Vs Functional Testing
    Difference Between System and Integration Testing
    Difference Between System and Integration Testing
    Positive and Negative Testing
    Differences Between Positive and Negative Testing in Software Testing
    DataProvider Annotation
    TestNG – Difference between @Factory and @DataProvider Annotation
    Difference Between SDLC and STLC
    Difference Between SDLC and STLC
    Difference Between Regression Testing vs Functional Testing
    Difference Between Regression Testing vs Functional Testing
    Difference Between Functional and Non functional Testing
    Difference Between Functional and Non functional Testing
    Difference Between Agile vs Waterfall
    Difference Between Agile vs Waterfall Methodology
    Difference Between Severity Vs Priority
    Difference Between Severity Vs Priority In Testing

    Filed Under: Difference

    Reader Interactions

    Leave a Reply Cancel reply

    Your email address will not be published. Required fields are marked *

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers