Skip to main content

Posts

Showing posts with the label Inner Classes

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 class...