Inner class
| Industry | Software development concepts |
|---|---|
Area served | Programming languages such as Java, C++, Python, D, Visual Basic .NET, Ruby, C# |
| Products | Inner classes, nested classes |
| Parent | Object-oriented programming |
In object-oriented programming (OOP), an inner class or nested class refers to a class that is fully declared inside another class or interface. It is distinct from a subclass.
Overview
While an instance of a top-level class can stand alone, an inner class instance must be associated with an instance of the outer top-level class.
Take, for example, an abstract representation of a Car possessing four Wheels. These Wheels are unique to that particular Car, rather than being generic wheels applicable to any vehicle. Using inner classes to model this, the top-level Car contains four instances of the inner class Wheel, illustrating their compositional and semantic link. Each Wheel is connected to its enclosing Car, symbolizing aggregation instead of an independent object.
Inner classes facilitate accurate representation of such associations, denoted as Car.Wheel, where the Wheel class is nested inside the Car. This form of encapsulation aligns with object-oriented principles by grouping related components within a single class entity.
For larger codebases, defining these components as separate top-level classes may improve reusability and abstraction, especially when multiple inner classes share common features. Nevertheless, it is important to avoid unnecessary generalization too early in development.
Types of nested classes in Java
Within Java, nested classes are categorized into four types:
- Static
- Static member classes, also known as static nested classes, are marked with the
statickeyword. They do not hold a reference to an instance of the enclosing class and cannot directly access its instance fields or methods. Their behavior closely resembles that of non-nested classes but they can freely interact with static members of the outer class without qualification. Nested interfaces are implicitly static by definition.
- Non-static / inner classes
These inner classes maintain a reference to their enclosing instance, granting them implicit access to its instance variables and methods. This reference can be explicitly accessed using EnclosingClassName.this. Inner classes cannot declare static members except for compile-time constants and must be instantiated either within an instance context or through syntax such as enclosingInstance.new InnerClass().
- Member class: Defined as a member within the outer class but without the
staticmodifier. - Local class: Declared inside a method body and accessible only within that method's scope. They can utilize final local variables and parameters, enabling generic type operations confined to the method.
- Anonymous class: A special form of local class declared and instantiated simultaneously within expressions. They extend one superclass or implement one interface, cannot have explicit constructors but may include initializer blocks for setup purposes.
This classification promotes modular design by tightly coupling functionality with its surrounding context, thereby improving maintainability and clarity in Java software development.
Programming languages
- Starting with version 1.1, the Java programming language incorporated inner classes as a feature.
- Nested classes are also supported in languages such as D, Visual Basic .NET, Ruby, C++, and C#.
- In Python, it is possible to define classes within other classes, methods, or functions.
- C++ implements nested classes that function similarly to Java's static member classes, though they are not explicitly declared with the "static" keyword.
- The concept of nested classes was initially introduced by the BETA programming language.
GUI code
In Java, local inner classes are frequently utilized to create callback handlers for GUI elements, allowing event-handling objects to be shared by implementing interfaces or extending adapter classes with tailored event logic.
When event-handling behavior is specific to a single component, anonymous inner classes are preferred as they remove the necessity for explicit naming.
This method prevents the creation of large Template:Javadoc:SE functions cluttered with numerous conditional statements, which can hinder maintainability. Employing inner classes enhances both the readability and structure of the codebase.
External links
- "Inner classes So what are inner classes good for anyway?"
- Nested classes in D