• 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 Default constructor vs Parametrized constructor

Difference Between Default constructor vs Parametrized constructor

Last Updated on: May 20, 2020 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Default Constructor vs Parametrized Constructor
  • Example for Default constructor vs Parametrized constructor

Default constructor vs Parametrized constructor: In this article, we will list the difference between the default constructor and parameterized constructor in Java. Let us detail out the difference between the Default constructor v/s Parametrized constructor in the tabular form below.

Read Also: Static Initialization blocks vs Instance Initialization blocks In Java

Default Constructor vs Parametrized Constructor

 Default constructor  Parameterized constructor
A constructor which takes no arguments is known as the default constructor A constructor which takes one or more arguments is known as parameterized constructor
The compiler inserts a default no-arg constructor after compilation if there is no explicit constructor defined in a class When the parameterized constructor is defined in a class, then the programmer needs to define default no-arg constructor explicitly if required
No need to pass any parameters while constructing new objects using the default constructor At least one or more parameters need to be passed while constructing new objects using argument constructors
A default constructor is used for initializing objects with the same data Whereas parametrized constructor is used to create distinct objects with different data

Example for Default constructor vs Parametrized constructor

Read Also: Difference Between Final Finally and Finalize

Employee.java

package in.bench.resources.constructor.example;
public class Employee
{
    // member variables
    int employeeId;
    String employeeName;
    String employeeOrg;
    
    // default constructor
    Employee() 
   {
      // an implicit super() constructor call to java.lang.Object is always present until we specify explicitly
      System.out.println("Employee class >> Inside default constructor");
      this.employeeOrg = "Google Corporation Inc.";
       }
    
   // parametrized constructor with 2 argument (String, int)
   Employee(String name, int id) 
   {
      this(); // to invoke another constructor from same class, this() constructor is used
      System.out.println("Employee class >> Inside parametrized constructor with 1 argument (String)");
      this.employeeName = name;
      this.employeeId = id;
   }
    
   //display() method
   void displayEmployeeInfo() 
   {
      System.out.println("\nEmployee details:\n\nOrgnaization:" + employeeOrg + "\nId: "+employeeId +"\nName: " + employeeName + "\n");
   }
    
   public static void main(String args[]) 
   {
         Employee emp = new Employee("Rahul Dravid", 19);
         emp.displayEmployeeInfo();
       }
}
Read Also: Static Block  In Java

Output:

Employee class >> Inside default constructor
Employee class >> Inside parametrized constructor with 1 argument (String)
 
Employee details: 
 
Orgnaization: Google Corporation Inc.
Id: 19
Name: Rahul Dravid
Check Also: interview questions on selenium

Your comments/suggestions/feedback of Interview Questions in this post will help a lot of people in the QA community for helping them in cracking the interviews.

    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

    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