Inner class

Inner class

In object-oriented programming (OOP), an inner class or nested class is a class declared entirely within the body of another class or interface. It is distinguished from a subclass.

Contents

Overview

An instance of a normal or top-level class can exist on its own. By contrast, an instance of an inner class cannot be instantiated without being bound to a top-level class.

Let us take the abstract notion of a Car with four Wheels. Our Wheels have a specific feature that relies on being part of our Car. This notion does not represent the Wheels as Wheels in a more general form that could be part of a vehicle. Instead, it represents them as specific to this one. We can model this notion using inner classes as follows:

We have the top-level class Car. Instances of Class Car are composed of four instances of the class Wheel. This particular implementation of Wheel is specific to the car, so the code does not model the general notion of a wheel that would be better represented as a top-level class. Therefore, it is semantically connected to the class Car and the code of Wheel is in some way coupled to its outer class.

Inner classes provide us with a mechanism to accurately model this connection. We say that our Wheel class is Car.Wheel, Car being the top-level class and Wheel being the inner class.

Inner classes therefore allow for the object orientation of certain parts of the program that would otherwise not be encapsulated into a class.

Larger segments of code within a class might be better modeled or refactored as a separate top-level class, rather than an inner class. This would make the code more general in its application and therefore more re-usable but potentially might be premature generalization. This may prove more effective, if code has many inner classes with the shared functionality.

Types of inner classes

In Java there are four types of nested class:

Static
  • Static member class, also called static nested classes[1] - They are declared static. Like other things in static scope (i.e. static methods), they do not have an enclosing instance, and cannot access instance variables and methods of the enclosing class. They are almost identical to non-nested classes except for scope details (they can refer to static variables and methods of the enclosing class without qualifying the name; other classes that are not one of its enclosing classes have to qualify its name with its enclosing class's name). Nested interfaces are implicitly static.
Non-Static / Inner Classes

Inner class - The following categories are called inner classes. Each instance of these classes has a reference to an enclosing instance (i.e. an instance of the enclosing class), except for local and anonymous classes declared in static context. Hence, they can implicitly refer to instance variables and methods of the enclosing class. The enclosing instance reference can be explicitly obtained via EnclosingClassName.this. Inner classes may not have static variables or methods, except for compile-time constant variables. When they are created, they must have a reference to an instance of the enclosing class; which means they must either be created within an instance method or constructor of the enclosing class, or (for member and anonymous classes) be created using the syntax enclosingInstance.new InnerClass().[1]

  • Member class - They are declared outside a function (hence a "member") and not declared "static".
  • Local class - These are classes that are declared in the body of a function. They can only be referred to in the rest of the function. They can use local variables and parameters of the function, but only ones that are declared "final". (This is because the local class instance must maintain a separate copy of the variable, as it may out-live the function; so as not to have the confusion of two modifiable variables with the same name in the same scope, the variable is forced to be non-modifiable.)
  • Anonymous class - These are local classes that are automatically declared and instantiated in the middle of an expression. They can only directly extend one class or implement one interface. They can specify arguments to the constructor of the superclass, but cannot otherwise have a constructor (however, this is not a limitation, since it can have an instance initializer block to perform any initialization).

Why can't an inner class have static members?

Inner class can't have static member in it because of following reasons: Consider that you have inner class with some static member field in it. Now this field is belonging of inner class only, outer class can't access it anyway.

Now to access this static field you will have to create instance of outer class (say outer_instance1), then you can access this field as 'outer_instance1.InnerClass.staticField' Note that though all the instances of inner class associated with outer_instance1 will share this static field, it won't be exposed to other instances of outer class like suppose i have outer_instance2 and if access this field using 'outer_instance2.InnerClass.staticField' then it won't be the value of 'outer_instance1.InnerClass.staticField'. So declaring this field as static won't be useful at outerclass level.

An article by Tushar Baviskar, PVGian 14:57, 25 October 2011 (UTC)

Programming Languages

GUI code

Local inner classes are often used in Java to define callbacks for GUI code. Components can then share an object that implements an event handling interface or extends an abstract adapter class, containing the code to be executed when a given event is triggered.

Anonymous inner classes are also used where the event handling code is only used by one component and therefore does not need a named reference.

This avoids a large monolithic actionPerformed(ActionEvent) method with multiple if-else branches to identify the source of the event. This type of code is often considered messy and the inner class variations are considered to be better in all regards.

External links

References

  1. ^ a b (Oracle) Nested Classes, Oracle.Com - The Java Tutorials.

Wikimedia Foundation. 2010.

Игры ⚽ Нужен реферат?

Look at other dictionaries:

  • Class (computer programming) — In object oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable …   Wikipedia

  • Class (computer science) — In object oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.More technically, a class is a cohesive… …   Wikipedia

  • Inner sphere electron transfer — Inner sphere or bonded electron transfer[1] proceeds via a covalent linkage between the two redox partners, the oxidant and the reductant. In Inner Sphere (IS) electron transfer (ET), a ligand bridges the two metal redox centers during the… …   Wikipedia

  • Inner German border — Innerdeutsche Grenze North and central Germany …   Wikipedia

  • Class of 1984 — Theatrical release poster Directed by Mark L. Lester Produced by …   Wikipedia

  • Class-responsibility-collaboration card — Class Responsibility Collaboration (CRC) cards are a brainstorming tool used in the design of object oriented software. They were proposed by Ward Cunningham and Kent Beck. [1] They are typically used when first determining which classes are… …   Wikipedia

  • Class-Responsibility-Collaboration card — Class Responsibility Collaborator (CRC cards) are a brainstorming tool used in the design of object oriented software. They were proposed by Ward Cunningham and Kent Beck. [Citation last1 = Beck | first1 = Kent author1 link = Kent Beck last2 =… …   Wikipedia

  • Inner-grazer — is a class of solar orbits. This diagram shows the six possible types of orbits one planet can have in relation to another. The Sun is shown in the middle (the orange dot) and the outer planet s orbital band is in yellow. The orbit of the other… …   Wikipedia

  • Class traitor — is a term used by many Socialist organizations to refer to a member of the proletariat class who works directly or indirectly against their class interest, or what is against their economic benefit as opposed to that of the bourgeoisie. It… …   Wikipedia

  • inner lamella — (Class Ostracoda): Inner layer of epidermis and cuticle of the dorsal body fold forming the valve, entirely uncalcified or calcified only around the free margin [Cohen, Peterson, and Maddocks, in press]. (Class Ostracoda): Relatively thin,… …   Crustacea glossary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”