Java : Inner classes -1

Achraf Housni
4 min readSep 26, 2023

Rules about inner classes

A member inner class is a class defined at the member level of another class, which means declaring a class inside another class.

  • It is a nested type declaration, meaning it is a type declaration defined within another type.
  • A top-level type declaration is a type declaration that is not defined within another type.
  • For example, in the file “Outer.java,” we declared a top-level class like this:
class Outer {

}

Top-level classes and interfaces can only be declared with public or package-private (default) access. This means that in a single Java file, you should have at least one public or default class.

For inner classes, there are several access modifiers available:

  • abstract
  • public
  • final
  • private
  • protected
  • static
  • strictfp

The main advantage of using inner classes is abstraction, which means that inner classes hide their members from the outer class. This implies that the members of inner classes are not directly accessible to outer classes.

Example:

public class Outer {…

--

--