Java Interview Questions SET-01
Java Interview Questions Set-02 ARRAY
Java Interview Questions Set-03 MAP
Java Interview Question Set-04
What are Java Generics?
- Java generics are like special boxes that can hold different types of things.
- Just like you have different boxes for toys, books, and clothes, Java generics allow us to create boxes that can hold different types of data.
- Java generics provide type safety and enable code reusability by allowing classes, interfaces, and methods to work with different types of objects.
- Generics eliminate the need for type casting and ensure compile-time type checking.
- They are widely used in collections to enforce type constraints and provide compile-time type safety.
- Bounded type parameters in generics allow for restricting acceptable types, enhancing flexibility in type handling.
- Java generics enhance code readability and maintainability by promoting cleaner and more concise code.
Why do we use Java Generics?
- Imagine you have a magic box that can hold any kind of toy: cars, dolls, or action figures.
- Java generics work similarly. They allow us to create code that can work with different types of data without duplicating the code.
How to use Java Generics?
- Let's say we want to create a box that can hold any kind of toy. We use the angle brackets "<>" to create a generic class or method.
Example: Creating a generic class
\
- Here, "T" is a placeholder for any type of toy. It could be a Car, Doll, or any other toy.
Example: Using the generic class
Benefits of Java Generics:
- Code reusability:
We can write generic code that can work with different types of data without duplicating the code. - Type safety:
Generics provide compile-time checks to ensure that we are using the correct types, reducing errors and bugs.
Key points:
- Java generics are like special boxes that can hold different types of things.
- Generics help us write code that can work with different types of data without duplicating code.
- We use angle brackets "<>" to create generic classes or methods.
- Generics provide code reusability and type safety.
Java Generics in Test Automation
In this example, we have a class called AssertionUtils
that contains a generic method called assertListsEqual()
. This method takes two parameters: expected
and actual
, which are both lists of type T
.
The <T>
before the return type void
represents the generic type parameter. It allows the method to work with any type of list.
Inside the method, we first check if the sizes of the expected and actual lists are equal. If they are not, we throw an AssertionError
indicating that the lists are not of the same size.
Then, we iterate over the elements of both lists and compare them using the equals()
method. If any pair of elements differs, we throw an AssertionError
indicating the index at which the lists differ, along with the expected and actual values.
How to use Java ENUM in Test Automation Framework
Here's an example usage of the assertListsEqual()
method:
In this example, we have two lists: expected
and actual
. We call the assertListsEqual()
method from AssertionUtils
and pass the lists as arguments. If the lists are not equal, an AssertionError
will be thrown with the appropriate message indicating the index and the expected/actual values.
By using generics in this way, you can create a single generic assertion method that can compare two lists of any type, making your test assertions more reusable and flexible.
Basic Linux Commands for Automation QA
****************************************
TOP 15 BDD - CUCUMBER Interview Q&A
************************************************
✍️AUTHOR: LinkedIn Profile
************************************************
Learn (API-Microservice)Testing+ Selenium UI Automation-SDET with Self Paced Videos prepared by FAANG employees and LIVE Doubt Session
*************************************************
SeleniumWebdriver Automation Testing Interview Questions:
https://automationreinvented.blogspot.com/search/label/SeleniumWebdriver
API Testing Interview Question Set:
https://automationreinvented.blogspot.com/2022/03/top-80-api-testing-interview-questions.html
DevOps Interview Q&A:
https://automationreinvented.blogspot.com/2021/11/top-11-devops-interview-questions-and.html
Kubernetes Interview Question Set
https://automationreinvented.blogspot.com/search/label/Kubernetes
Docker Interview Question Set
https://automationreinvented.blogspot.com/Docker
Linux Interview question Set
https://automationreinvented.blogspot.com/search/label/Linux
Automation Testing/SDET Framework Design
https://automationreinvented.blogspot.com/search/label/FrameworkDesign
Java Related Interview Question Set
https://automationreinvented.blogspot.com/search/label/Java
GIT Interview Question Set:
https://automationreinvented.blogspot.com/2021/09/top-40-git-interview-questions-and.html
Coding Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Coding%20Questions
Mobile Testing Interview Question Set:
https://automationreinvented.blogspot.com/search/label/Mobile%20Testing
Python Interview Question Set for QAE - SDET - SDE:
https://automationreinvented.blogspot.com/search/label/Python
No comments:
Post a Comment