Você está na página 1de 23

Inner Classes

Lecture Objectives
Introduction to Inner Classes.
Gain an appreciation of the functionality of

inner classes.
Assess the different types of inner classes
Inspect how Java circumvents the issue of
multiple inheritance.

Introduction
Its possible to place a class definition

within another class definition. This is


called an inner class.
The inner class is a valuable feature
because it allows you to group classes that
logically belong together and to control the
visibility of one within the other.
You create an inner class just by placing
the class definition inside a surrounding
class.

Types of Inner Classes


There are four types of Inner classes aka
nested classes.
Regular "Inner classes
Method-local inner classes
Anonymous classes
Static nested class

Regular Inner Class


You define an inner class within the curly

braces of the outer class.


class MyOuter {
class MyInner { }
}
When we compile this class we get two class files
%javac MyOuter.java results in MyOuter.class and
MyOuter$MyInner.class

Regular Inner Class


Cont..
A regular inner class cannot have static

declarations of any kind this implies we


cannot have main() in our inner class.
We access the inner class through a live
instance of the outer class

Example
class MyOuter {
private int x = 7;
// inner class definition
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
}
} // close inner class definition
} // close outer class

The inner class is accessing a private

member of the outer class. This is because


the inner class is also a member of the
outer class. So just as any member of the
outer class (say, an instance method) can
access any other member of the outer
class, private or not, the inner classalso a
membercan do the same.

Instantiating an Inner
Class
To create an instance of an inner class, you

must have an instance of the outer class to


tie to the inner class.
There are no exceptions to this rule: an
inner class instance can never stand alone
without a direct relationship to an instance
of the outer class.
Most often, it is the outer class that
creates instances of the inner class, since it
is usually the outer class wanting to use
the inner instance as a helper for its own

Example

class MyOuter {
private int x = 7;
public void makeInner() {
MyInner in = new MyInner(); // make an inner instance
in.seeOuter();
}
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
public static void main(String[] args) {
MyOuter mo = new MyOuter(); // gotta get an instance!
MyOuter.MyInner inner = mo.new MyInner();
inner.seeOuter();
}
} } } What is the Output

Inner Class Modifiers


final
abstract
public
private
protected
staticbut static turns it into a static nested

class not an inner class


strictfp

Method Inner Class


A regular inner class is scoped inside another

class's curly braces, but outside any method code.


But you can also define an inner class within a
method.

Local Inner Class


Example
class MyOuter2 {
private String x = "Outer2";
void doStuff() {
class MyInner {
public void seeOuter() {
System.out.println("Outer x is " + x);
} // close inner class method
} // close inner class definition
MyInner mi = new MyInner(); // This line
must come
// after the class
mi.seeOuter();

Rules of Local Inner


Classes
A method-local inner class can be instantiated only within

the method where the inner class is defined.


Method-local inner class object shares a special relationship
with the enclosing (outer) class object, and can access its
private members.
However, the inner class object cannot use the local
variables
of the method the inner class is in unless the variable is
marked as final.
You cannot mark a method-local inner class public, private,
protected, static, transient, but it can be marked as final or
abstract.
an inner class in a static method is subject to the same
restrictions as the static method.

Anonymous Inner Classes


These are declared without any class name.
They are usually declared within an argument

of a method.
There are two types

Version One
class Gwadla{
public void pop(){
System.out.println(gwadla);
}
}
class Food {
Gwadla gw = new Gwadla{
public void pop(){
System.out.println(anonymous
gwadla);
}

The 2nd Gwadla does not refer to an

instance of the 1st Gwadla but to an


instance of an anonymous subclass of
Gwadla.
The pop method is being overridden,
which is precisely the point of anonymous
classes to override several methods of the
superclass.
Polymorphism attribute states that we can
only use methods which exist if we are
using the superclass reference.

Version 2
Here we create an anonoymous implementer

of the specified interface, rather than the


subclass of the class.

Example
interface Eatable{
public void eat();
}
Class Fruit{
Eatable e = new Eatable(){
public void eat {
System.out.println(anonymous
eatable implementer);
}
};
}

The line Eatable e = new Eatable (){

Can be interpreted as Declare a reference


variable of type Eatable that refers to an
object from a class that implements the
Eatable interface, butt we dont yet have a
class that implements Eatable so lets make
one right now.

Static Inner Classes


Static nested classes are inner classes named

with the static modifier.


A static nested class in not an inner class its a
top-level nested class.
Because the nested class is static it does not
share any special relationship with an instance of
the outer class.
A static nested class cannot access a non-static
members of the outer class since it does not have
an implicit reference to any outer instance.

class BigOuter {
Static class Nest {
public void go(){
System.out.println(hello);}}
}
Class Broom{
static class B2 {
public void goB2(){
System.out.println(hello2);}}

public static void main(String args){


BigOuter.Nest n = new BigOuter.Nest();
n.go();
B2 b2 = new B2();
b2.goB2();
}
}

Você também pode gostar