filmov
tv
JAVA : What is a nested class in Java? SDET Automation Testing Interview Questions & Answers
![preview_player](https://i.ytimg.com/vi/8K_QZqGmRmc/maxresdefault.jpg)
Показать описание
JAVA : What is a nested class in Java?
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
JAVA : What is a nested class in Java?
In Java, a nested class is a class that is defined within another class. It provides a way to logically group classes that are closely related and increases encapsulation and code organization. A nested class can access the members (fields, methods, and nested classes) of its enclosing class, including private members, and can also have its own members.
There are four types of nested classes in Java:
1. Static nested class (or static inner class): A static nested class is a nested class that is declared as `static`. It is associated with its enclosing class, but it does not have access to the instance variables and methods of the enclosing class, unless they are static. It can be accessed using the enclosing class name.
2. Inner class (non-static nested class): An inner class is a nested class that is not declared as `static`. It has access to all members of its enclosing class, including instance variables and methods. It is associated with an instance of the enclosing class and can be accessed only through an instance of the enclosing class.
3. Local class: A local class is a nested class defined within a method, constructor, or an initializer block. It is accessible only within the scope where it is defined. Local classes can access the members of the enclosing class and local variables that are effectively final or declared as `final`.
4. Anonymous class: An anonymous class is a special type of local class that does not have a name. It is defined and instantiated at the same time, typically as a subclass or implementation of an interface. Anonymous classes are often used for event handling or implementing callback interfaces.
Here's an example that demonstrates the usage of nested classes in Java:
public class OuterClass {
private int outerField;
public void outerMethod() {
// Accessing outerField
}
// Static nested class
public static class StaticNestedClass {
private int nestedField;
public void nestedMethod() {
// Accessing nestedField
}
}
// Inner class
public class InnerClass {
private int innerField;
public void innerMethod() {
// Accessing innerField and outerField
}
}
public void createNestedObjects() {
// Creating instances of nested classes
StaticNestedClass staticNestedObj = new StaticNestedClass();
InnerClass innerObj = new InnerClass();
// Accessing methods of nested classes
}
public static void main(String[] args) {
OuterClass outerObj = new OuterClass();
}
}
In the above example, `OuterClass` contains a static nested class `StaticNestedClass` and an inner class `InnerClass`. Both nested classes have access to the `outerField` of the `OuterClass`, but the inner class also has access to its own `innerField`. The `createNestedObjects()` method demonstrates the creation of instances of the nested classes and accessing their methods.
Nested classes provide a way to logically group related classes and can be useful for organizing code, improving encapsulation, and implementing encapsulated behaviors.
SDET Automation Testing Interview Questions & Answers
We will be covering a wide range of topics including QA manual testing, automation testing, Selenium, Java, Jenkins, Cucumber, Maven, and various testing frameworks.
JAVA : What is a nested class in Java?
In Java, a nested class is a class that is defined within another class. It provides a way to logically group classes that are closely related and increases encapsulation and code organization. A nested class can access the members (fields, methods, and nested classes) of its enclosing class, including private members, and can also have its own members.
There are four types of nested classes in Java:
1. Static nested class (or static inner class): A static nested class is a nested class that is declared as `static`. It is associated with its enclosing class, but it does not have access to the instance variables and methods of the enclosing class, unless they are static. It can be accessed using the enclosing class name.
2. Inner class (non-static nested class): An inner class is a nested class that is not declared as `static`. It has access to all members of its enclosing class, including instance variables and methods. It is associated with an instance of the enclosing class and can be accessed only through an instance of the enclosing class.
3. Local class: A local class is a nested class defined within a method, constructor, or an initializer block. It is accessible only within the scope where it is defined. Local classes can access the members of the enclosing class and local variables that are effectively final or declared as `final`.
4. Anonymous class: An anonymous class is a special type of local class that does not have a name. It is defined and instantiated at the same time, typically as a subclass or implementation of an interface. Anonymous classes are often used for event handling or implementing callback interfaces.
Here's an example that demonstrates the usage of nested classes in Java:
public class OuterClass {
private int outerField;
public void outerMethod() {
// Accessing outerField
}
// Static nested class
public static class StaticNestedClass {
private int nestedField;
public void nestedMethod() {
// Accessing nestedField
}
}
// Inner class
public class InnerClass {
private int innerField;
public void innerMethod() {
// Accessing innerField and outerField
}
}
public void createNestedObjects() {
// Creating instances of nested classes
StaticNestedClass staticNestedObj = new StaticNestedClass();
InnerClass innerObj = new InnerClass();
// Accessing methods of nested classes
}
public static void main(String[] args) {
OuterClass outerObj = new OuterClass();
}
}
In the above example, `OuterClass` contains a static nested class `StaticNestedClass` and an inner class `InnerClass`. Both nested classes have access to the `outerField` of the `OuterClass`, but the inner class also has access to its own `innerField`. The `createNestedObjects()` method demonstrates the creation of instances of the nested classes and accessing their methods.
Nested classes provide a way to logically group related classes and can be useful for organizing code, improving encapsulation, and implementing encapsulated behaviors.
Комментарии