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
  • Limit
  • Additional Restrictions
  • Difference Between this and super keyword
  • Difference Between this() and super() Constructor

Was this helpful?

  1. Java
  2. OOPS
  3. Constructor

This() & Super()

Constructors are special methods used to initialize objects in Java. When you create an instance of a class, its constructor gets called to set up the object.

Within a constructor, you can call:

  • this() - To call another constructor from the same class. It allows constructor chaining, reusing initialization logic.

  • super() - To call the parent class constructor. It executes the parent's constructor before the child constructor body.a

Limit

There is no limit on how many times super() can be called in a constructor. However, there are some restrictions on using this():

  • this() can only be used to call another constructor in the same class.

  • this() can only be called once in a constructor.

  • this() must be the first statement in a constructor.

For example:

public class Main {

  public Main() {
    this(5); // OK - calling another constructor in Main
  }
  
  public Main(int x) {
    this(); // NOT OK - `this()` already called  
  }

  public Main(int x, int y) {
    this(x); // OK - first statement
    this(y); // NOT OK - can only call `this()` once
  }
  
}

So in summary:

  • super() can be called multiple times

  • this() can only be called once and must be the first statement

The reason for the this() restriction is to avoid infinite recursion by constructors repeatedly calling each other in a loop.

Additional Restrictions

There are a couple additional restrictions on using this() in constructors:

  • Cannot use this() and super() together - It's either one or the other as the first statement.

  • Cannot call this() from a default no-arg constructor - Since the no-arg constructor is the default, it doesn't make sense to call this() from it.

  • Cannot call private constructors - this() can only call visible constructors in the same class.

  • Cannot call constructors of other classes - this() can only reference other constructors in the same class.

Some examples:

public MyClass() {
  super(); // NOT OK - can't use both super() and this() 
  this(); // NOT OK - can't use both
}

public MyClass() {
  this(); // NOT OK - can't call this() from default constructor
} 

public MyClass() {
  this(5); // OK
}

private MyClass(int x) {
  // Private constructor
}

public MyClass() {
  this(5); // NOT OK - can't call private constructor
}

public MyClass() {
  OtherClass(5); // NOT OK - can't call other class constructor 
}

So in summary, the main additional restrictions are:

  • Can't use both this() and super()

  • Can't call from default constructor

  • Can't call private constructors

  • Can't call constructors of other classes

The this keyword refers to the current object instance. It is used to differentiate between attributes/methods of the current object and its parent class.

For example, if a subclass shares a variable name with its parent class, this.variable refers to the subclass variable, while super.variable refers to the parent class variable.

The this keyword is also commonly used to pass the current object instance as a parameter to constructors or methods. Like this.method().

The super keyword is used to reference parent class properties/methods from a subclass. It allows inheriting and extending the parent class.

For example, a subclass may want to call or extend the constructor of its parent class. This uses super() or super(parameters) inside the subclass constructor.

Super can also be used to explicitly call a parent method implementation if the subclass does not override it.

So,

  • this refers to current object

  • super refers to parent class

  • this distinguishes between subclass/parent members

  • super calls parent class code from subclass

Difference Between this and super keyword

this
super

The current instance of the class is represented by this keyword.

The current instance of the parent class is represented by the super keyword.

In order to call the default constructor of the current class, we can use this keyword.

In order to call the default constructor of the parent class, we can use the super keyword.

It can be referred to from a static context. It means it can be invoked from the static context.

It can't be referred to from a static context. It means it cannot be invoked from a static context.

We can use it to access only the current class data members and member functions.

We can use it to access the data members and member functions of the parent class.

Difference Between this() and super() Constructor

this()
super()

The this() constructor refers to the current class object.

The super() constructor refers immediate parent class object.

It is used for invoking the current class method.

It is used for invoking parent class methods.

It can be used anywhere in the parameterized constructor.

It is always the first line in the child class constructor.

It is used for invoking a super-class version of an overridden method.

It is used for invoking a super-class version of an overridden method.

PreviousInterview QuestionsNextInterview Questions

Last updated 1 year ago

Was this helpful?

๐Ÿผ
๐ŸŽƒ
๐Ÿ“