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

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • 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

    Filed Under: Difference, Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy