QA Interview Handbook
  • ๐Ÿ Home Page
  • About Testing
    • ๐Ÿ’–Qualities of A Great Tester
  • Manual Testing
    • ๐Ÿ’กOverview
      • โœ‹Demand for Software Testing
      • ๐Ÿ˜„Tester's Role in Manual Testing
      • 7๏ธTesting Principles
      • ๐ŸšจV & V
      • โ”Interview Questions
    • โ™ป๏ธSDLC
      • ๐Ÿ“’Phase 1: Planning
      • ๐Ÿ”Phase 2: Requirement Analysis
      • ๐Ÿ‘”Phase 3: Design
      • โ›‘๏ธPhase 4: Development
      • ๐ŸงชPhase 5: Testing
      • ๐ŸššPhase 6: Deployment
      • ๐Ÿ–ฑ๏ธPhase 7: Maintenance
      • โš”๏ธCommon Challenges
      • โ”Interview Questions
    • ๐ŸŒ€STLC
    • ๐ŸŒŠWaterfall
    • โœณ๏ธAgile
      • ๐Ÿ˜Tester's Role in Scrum
    • ๐Ÿ”ขTypes
      • โฌœWhite Box Testing
      • โฌ›Black Box Testing
        • ๐Ÿ”ฐTechniques Used in Black Box Testing
        • ๐Ÿš˜Functional Testing
          • 1๏ธโƒฃUnit Testing
          • 2๏ธโƒฃIntegration Testing
            • ๐Ÿ”ฐTechniques Used in Integration Testing
          • 3๏ธโƒฃSystem Testing
            • ๐Ÿ“ผTypes of System Testing
            • ๐ŸŒŠPhases of System Testing
            • ๐ŸŒ€Regression Testing
            • ๐ŸŒซ๏ธSmoke Testing
          • 4๏ธโƒฃAcceptance Testing
            • โš™๏ธUser Acceptance Testing
            • ๐Ÿ…ฐ๏ธAlpha Testing
            • ๐Ÿ…ฑ๏ธBeta Testing
        • ๐Ÿ•ณ๏ธNon Functional Testing
      • ๐Ÿ“‘Grey Box Testing
    • ๐Ÿช„User Story
      • โบ๏ธSample User Stories
    • ๐Ÿ““Test Cases
      • โบ๏ธSample Test Cases
      • โ”Interview Questions
    • โœ–๏ธDefect Life Cycle
      • โ˜ฃ๏ธPriority + Severity
      • โบ๏ธSample Defect Reports
      • โ”Interview Questions
      • ๐Ÿ›Buggy Questions
    • ๐ŸŒAtlassian JIRA
      • ๐ŸžJIRA Issues
      • โ”Interview Questions
    • โ”Interview Questions
  • Accessibility Testing
    • ๐Ÿ’กOverview
    • ๐Ÿค“Tester's Role in Accessibility Testing
    • ๐Ÿ“šWCAG Principles
      • ๐Ÿ‘๏ธPerceivable
      • ๐ŸนOperable
      • ๐Ÿง Understandable
      • ๐Ÿค–Robust
    • ๐Ÿ”งAxe DevTools
      • โ”Interview Questions
    • ๐Ÿ““Test Cases
    • โ”Interview Questions
  • API Testing
    • ๐Ÿ’กOverview
    • ๐Ÿ˜€Tester's Role in API Testing
    • ๐ŸŠHTTP Methods & CRUD
      • ๐Ÿ‚HTTP Status Codes
    • ๐ŸAPI Tools
      • ๐ŸŸ Postman
        • โ˜„๏ธSending your first API request
        • ๐Ÿ”ฌHTTP Requests with Java
        • ๐ŸŽฒGitHub Sample
        • โ”Interview Questions
      • โ›‘๏ธREST Assured
        • ๐ŸŽ‡Dependency
        • โ”Interview Questions
    • ๐Ÿ““Test Cases
    • ๐ŸฆงAPI Cheatsheet
    • โ”Interview Questions
  • Database Testing
    • ๐Ÿ’กOverview
    • ๐Ÿ˜†Tester's Role in Database Testing
    • ๐Ÿ”ตSQL
      • โ›“๏ธConstraints
      • ๐Ÿ›ข๏ธReferencing a Column
      • ๐Ÿ”ผDDL Commands
      • ๐Ÿ”ผDML Commands
        • ๐Ÿ–Œ๏ธOperators
        • ๐Ÿ› ๏ธFunctions
          • โฏ๏ธAggregate Functions
        • ๐ŸŽ…Clauses
          • โซJoin Clauses
          • ๐Ÿ”ตFilter Clauses
          • โฌSet Operations
      • ๐ŸƒWildcard Character
      • โ”Interview Questions
    • ๐Ÿ““Test Cases
    • ๐ŸงคSQL Practice Sites
    • ๐ŸซSQL Cheatsheet
    • โ”Interview Questions
  • Java
    • โ›ฉ๏ธIntroduction
    • ๐Ÿ˜„Tester's Reason to Learn Java
    • โ“‚๏ธMain Method
      • โ”Interview Questions
    • ๐Ÿ“Variables & Types
      • ๐ŸชขSpecial Types
    • ๐ŸฅModifiers
    • ๐Ÿ…พ๏ธOperators
    • ๐ŸชกString
      • ๐ŸฉบString Methods
        • String Method Problems
      • ๐ŸšจDelimiter
      • โ”Interview Questions
    • ๐Ÿ–‡๏ธConditionals
      • ๐Ÿ’ŽCommon If Statements
      • ๐Ÿ’ŽCommon Ternary Operator Statements
    • โ“‚๏ธMath Class
    • ๐ŸŒŠLoops
      • ๐Ÿ’ŽCommon Loop Examples
      • ๐Ÿ”ƒNested For Loops
    • ๐ŸผOOPS
      • ๐Ÿ›๏ธClasses and Objects
        • โ”Interview Questions
      • ๐ŸŽƒConstructor
        • โšกStatic
          • โ”Interview Questions
        • ๐Ÿ“This() & Super()
          • โ”Interview Questions
        • ๐Ÿ€Finalization
      • ๐Ÿ”“Encapsulation
      • ๐ŸฅInheritance
      • ๐Ÿฆ‹Polymorphism
      • ๐Ÿ•ธ๏ธAbstraction
    • ๐ŸฎJava Practice Sites
    • โ˜‘๏ธData Structures + Algorithms
      • ๐Ÿ…พ๏ธBig O
      • โ˜‘๏ธData Structures
        • ๐Ÿ”ธArray
        • ๐Ÿ”ณArray Problems
        • Page
      • ๐ŸชŸSliding Window Technique
        • ๐ŸชŸSliding Window Problems
        • ๐ŸฅLeetCode #53
        • ๐ŸฅLeetCode #209
    • โ”Interview Questions
  • Automation Testing
    • ๐ŸšฐFlow
      • ๐Ÿ’กOverview
      • ๐ŸคฉTester's Role in Automation Testing
      • ๐Ÿ€Selenium
        • ๐Ÿ•ธ๏ธSelenium WebDriver
          • ๐Ÿ•ท๏ธWebDriver Commands
            • ๐ŸŒWebElement
              • ๐Ÿ”†HTML Tags
              • ๐Ÿ”ฌFind Element(s)
              • ๐ŸฆŽLocators
                • โŒXpath
                • ๐ŸฐCSS Selector
                • ๐Ÿ“€DOM
                • ๐Ÿ Quick Reference for XPath + CSS
            • โœ‹Waits
            • Browser Management
            • ๐ŸŽ๏ธNavigation
            • Alerts
          • ๐Ÿท๏ธAdvanced User Interactions
            • ๐Ÿ—ฏ๏ธAction vs. Actions
            • ๐Ÿ’งDrop Down
            • โœ…Check Box
            • ๐Ÿ–‡๏ธForms
          • โš ๏ธExceptions
        • ๐ŸOOPS + Selenium
        • ๐ŸšขFrameworks
          • โš“Module Based Framework
          • ๐ŸŽนKeyword Driven Framework
          • ๐ŸŽ‹Data Driven Framework
          • ๐ŸŒบHybrid Framework
          • ๐ŸŒดLog4j
          • ๐Ÿ“„Page Object Model
        • ๐ŸงชTesting Frameworks
          • ๐Ÿ’กTestNG
          • ๐Ÿ‰‘JUnit
          • ๐Ÿฅ’BDD
            • ๐Ÿฅ’Cucumber
        • ๐ŸŒ‰Selenium Grid
          • โœ–๏ธDesired Capabilities
        • โ”Interview Questions
      • ๐Ÿ”„API Testing with Selenium
      • โชDatabase Testing with Selenium
      • โ“‚๏ธMaven
      • ๐Ÿ™Git
        • โ”Interview Questions
      • ๐Ÿ•ต๏ธโ€โ™‚๏ธJenkins
        • โ”Interview Questions
      • ๐ŸณDocker
        • โ”Interview Questions
      • ๐Ÿ“™AWS
        • โ”Interview Questions
  • Behavioral
    • ๐Ÿ“ฃMixed Interview Questions
    • โญSTAR Method
      • ๐ŸŒŸSample Responses
Powered by GitBook
On this page
  • ๐Ÿ‘ทโ€โ™‚๏ธ What is a Constructor?
  • ๐Ÿ—๏ธ Rules for Constructors in Java ๐Ÿ—๏ธ
  • ๐Ÿ  Rules for Default Constructors ๐Ÿ 
  • ๐Ÿ› ๏ธ Rules for Parameterized Constructors ๐Ÿ› ๏ธ
  • ๐Ÿ”จ Key Notes
  • ๐Ÿ› ๏ธ In Simple Terms
  • Two Types of Constructors ๐Ÿ—
  • Default Constructor ๐Ÿ—๏ธ
  • Parameterized Constructor โš™๏ธ
  • Constructor Chaining โ›“

Was this helpful?

  1. Java
  2. OOPS

Constructor

PreviousInterview QuestionsNextStatic

Last updated 1 year ago

Was this helpful?

๐Ÿ‘ทโ€โ™‚๏ธ What is a Constructor?

A constructor in Java is a special method that is used to initialize objects. The constructor is called when an object of a class is instantiated.

It runs automatically whenever we create a new Person object using the new keyword.

Inside the constructor we can put code to set initial values for object attributes. For example:

public Person(String name) {

this.name = name; }

Now when we do:

Person p = new Person("Adam");

The constructor assigns "Adam" to the name attribute.

For example:

public class Person {

  // Constructor
  public Person() {
    // Initialization code goes here
  }
  
}

The constructor always has the same name as the class and no return type.

๐Ÿ—๏ธ Rules for Constructors in Java ๐Ÿ—๏ธ

Constructors are special methods used to initialize objects in Java. There are some important rules around defining and using constructors.

๐Ÿ  Rules for Default Constructors ๐Ÿ 

A default constructor has the following rules:

  • It has the same name as the class

  • It has no parameters

  • It is generated automatically if no constructors are defined

  • The default constructor initializes all member variables to default values

For example:

public class Person {

  private String name;

  // Default constructor generated automatically
  public Person() {
    name = ""; 
  }

}

The default constructor sets name to an empty string.

๐Ÿ› ๏ธ Rules for Parameterized Constructors ๐Ÿ› ๏ธ

Some rules for parameterized constructors are:

  • Can have any number of parameters, with any types

  • Must have the same name as the class

  • Are defined by the programmer

  • Initialize fields using values passed as parameters

For example:

public class Person {

  private String name;
  
  // Parameterized constructor
  public Person(String name) {
    this.name = name;
  }

}

This constructor initializes the name field using a passed parameter.

So following the rules for default and parameterized constructors allows proper initialization of objects in Java.

๐Ÿ”จ Key Notes

  • A constructor is a special method with the class name

  • It is called automatically when an object is created

  • It is used to initialize the object's state

  • If no constructor is defined, Java provides a default one

So constructors allow objects to be initialized right after instantiation.

๐Ÿ› ๏ธ In Simple Terms

The constructor is the initialization method of a class which sets up the new object when it is created. It runs automatically when a new object is made to get the object ready for use.

So in Java, constructors setup new objects!

Two Types of Constructors ๐Ÿ—

Constructors initialize an object when it is created. They have the same name as the class and differs by the parameter list.

There are two types of constructors in Java:

Default Constructor ๐Ÿ—๏ธ

  • Created automatically if no constructor is defined

  • Has no parameters

  • Can be defined explicitly

class Person {
   String name;

   Person() {      
     // Default constructor
   }
}

Person p = new Person(); 

Parameterized Constructor โš™๏ธ

  • Created by the programmer

  • Has parameters to initialize the object

class Person {
   String name;

   Person(String name) {  
     this.name = name;     
   }
}

Person p = new Person("John");

Here name parameter is used to initialize the name field.

this keyword refers to the current object.

Constructor Chaining โ›“

When one constructor calls another, it is known as constructor chaining. This allows reusing code and improving maintainability.

class Person {
  
  String name;
  int age;  
  
  Person() { 
    this("John Doe");     // Invoking another constructor
  }
  
  Person(String name) {    
    this(name, 0);       
  }
  
  Person(String name, int age) {
    this.name = name;     
    this.age = age;   
  }   
}

Here this() is used to invoke another constructor of the same class.

The constructor chain finally invokes the most specific constructor which initializes all fields.

๐Ÿผ
๐ŸŽƒ