Skip to main content

Inner Classes Interview Questions in Java


Inner Classes




1.What is a Nested class?

In Java, a Nested class is a class declared inside another class. We can have more than one class declared inside a file.

2. How many types of Nested classes are in Java?

Java provides four types of Nested classes:
1.     Member inner class
2.     Local inner class
3.     Anonymous inner class
4.     Static nested class

3.Why do we use Nested Classes?

There are following reasons for using nested classes:
1.     Logical Grouping: We can logically group classes in one place. If one class is useful to only one other class, then we put smaller class within the larger class and keep them in one file. This kind of nesting "helper classes" in a toplevel class makes the package more streamlined.
2.     Encapsulation: Nested classes increase encapsulation. Let say there are two top-level classes, Foo and Bar. Bar needs access to private members of Foo. We can hide class Bar within class Foo. In this way, private members of Foo can be accessed by class Bar. So class Foo remains encapsulated. Also, class Bar remains hidden from the outside world.
3.     Code Clarity: Nested classed make the code more readable and well organized. Only Top-level classes are exposed. The helper classes are kept hidden and closer the code where it is used by a Top-level class.

4.What is the difference between a Nested class and an Inner class in Java?

An Inner class in Java is non-static class. It is a type of Nested class that is defined in another class but not qualified with a Static modifier. A Nested class is also a class can be Static Nested class or a non-Static Inner class. An Inner class has access to other members of the enclosing class, even if they are declared private. A Static Nested class can not access the other members of the enclosing class.

5. What is a Nested interface?

A Nested interface is declared inside another interface or a toplevel class. By default it is static. A Nested interface is also known as Static interface.

6. How can we access the non-final local variable, inside a Local Inner class?

Java allows a Local Inner class to access only Constant local members. So we have to make the non-final local variable as final constant to access it inside a Local Inner class.

7.Can an Interface be defined in a Class?

Yes, we can define a Static Nested interface within a class. Only the enclosing class can access it.

8.Do we have to explicitly mark a Nested Interface public static?

A Nested Interface is implicitly public static. So the modifiers public and static are redundant in declaration.

9.Why do we use Static Nested interface in Java?

Only the enclosing class can access a Static Nested interface. Consider following code in which interface Xyz is enclosed in class Abc.
public class Abc
{
public interface Xyz
{
void callback();
}
public static void registerCallback(Xyz xyz) {...}
}                                                           // Client Code
Abc.registerCallback(new Abc.Xyz()
{
public void callback() {...}
});
Any code that cannot access Abc can not access interface Xyz also. So the purpose of declaring an Inner interface is to restrict its access from outside world.






Comments

Popular posts from this blog

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin...

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful ha...

Method Overloading and Overriding Interview Questions

Method Overloading and Overriding 1. What is the another name of Method Overloading? Method Overloading is also known as Static Polymorphism. 2.In java, How will we implement method overloading? We known, a class can have multiple method with same name but different arguments. It is called Method Overloading. To implement method overloading we have to create two methods with same name in a class and do one/more of the following: 1.      Different number of parameters 2.      Different data type of parameters 3.      Different sequence of data type of parameters 3. What kinds of argument variation are allowed in Method Overloading? Method Overloading only allows two methods with same name to differ in: 1.      Number of parameters 2.      Data type of parameters 3.      Sequence of data type of parameters 4. It is not p...