0% found this document useful (0 votes)
61 views25 pages

BSE2107 OOP II - Inner Classes in Java-1

The document discusses different types of inner classes in Java, including inner classes, static inner classes, local classes, and anonymous classes. Inner classes can access private members of the outer class, while static inner classes cannot access non-static members. Local and anonymous classes are defined within a block of code and have access to final variables and methods of the outer class.

Uploaded by

Katende Chris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views25 pages

BSE2107 OOP II - Inner Classes in Java-1

The document discusses different types of inner classes in Java, including inner classes, static inner classes, local classes, and anonymous classes. Inner classes can access private members of the outer class, while static inner classes cannot access non-static members. Local and anonymous classes are defined within a block of code and have access to final variables and methods of the outer class.

Uploaded by

Katende Chris
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

1

BSE2107 OOPII
Using Inner Classes in Java
Inner Classes In Java

• Inner (or nested) classes are classes defined within other


classes
– The class that includes the inner class is called the outer class

• There are four categories of inner classes in Java:


1. Inner classes (non-static)
2.Static inner classes
3.Local classes (defined inside a block of Java code)
4.Anonymous classes (defined inside a block of Java code)

13-2
Inner Classes In Java (Cont'd)

public class Test { // OuterClass.java: inner class demo


... public class OuterClass {
} private int data;

public class A { /** A method in the outer class */


... public void m() {
} // Do something
}
(a)
// An inner class
class InnerClass {
public class Test { /** A method in the inner class */
... public void mi() {
// Directly reference data and method
// Inner class // defined in its outer class
public class A { data++;
... m();
} }
} }
}
(b)
(c)

13-3
Inner Classes In Java (Cont'd)

• An inner class definition is a member of the outer


class in the same way that the instance variables
and methods of the outer class are members
– An inner class is local to the outer class definition
– The name of an inner class may be reused for something
else outside the outer class definition
– If the inner class is private, then the inner class cannot
be accessed by name outside the definition of the outer
class

13-4
Simple Uses of Inner Classes

• There are two main advantages to inner


classes
– They can make the outer class more self-
contained since they are defined inside a class
– Both of their methods have access to each other's
private methods and instance variables
• Using an inner class as a helping class is one
of the most useful applications of inner
classes
– If used as a helping class, an inner class should
be marked private

13-5
Inner and Outer Classes Have Access to Each
Other's Private Members

• Within the definition of a method of an inner class:


– It is legal to reference a private instance variable of the outer class
– It is legal to invoke a private method of the outer class
• Within the definition of a method of the outer class
– It is legal to reference a private instance variable of the inner class
on an object of the inner class
– It is legal to invoke a (non-static) method of the inner class as long
as an object of the inner class is used as a calling object
• Within the definition of the inner or outer classes, the
modifiers public and private are equivalent

13-6
Class with an Inner Class

13-7
Class with an Inner Class(Cont'd)

13-8
Class with an Inner Class(Cont'd)

13-9
The .class File for an Inner Class

• Compiling any class in Java produces a .class


file named ClassName.class
• Compiling a class with one (or more) inner classes
causes both (or more) classes to be compiled, and
produces two (or more) .class files
– Such as ClassName.class and
ClassName$InnerClassName.class
Static Inner Classes

A normal inner class has a connection between its


objects and the outer class object that created the
inner class object
This allows an inner class definition to reference an
instance variable, or invoke a method of the outer class
There are certain situations, however, when an
inner class must be static
If an object of the inner class is created within a static
method of the outer class
If the inner class must have static members
Static Inner Classes (Cont'd)

Since a static inner class has no connection to


an object of the outer class, within an inner
class method
Instance variables of the outer class cannot be
referenced
Nonstatic methods of the outer class cannot be
invoked
To invoke a static method or to name a static
variable of a static inner class within the outer
class, preface each with the name of the inner
class and a dot
Public Inner Classes

If an inner class is marked public, then it can be


used outside of the outer class
In the case of a non-static inner class, it must be
created using an object of the outer class
BankAccount account = new BankAccount();
BankAccount.Money amount = account.new
Money("41.99");

Note that the prefix account. must come before new


The new object amount can now invoke methods from
the inner class, but only from the inner class
Public Inner Classes (Cont'd)

In the case of a static inner class, the procedure is


similar to, but simpler than, that for nonstatic inner
classes
OuterClass.InnerClass innerObject =new OuterClass.InnerClass();

Note that all of the following are acceptable


innerObject.nonstaticMethod();
innerObject.staticMethod();
OuterClass.InnerClass.staticMethod();
Tip: Referring to a Method of the Outer Class

If a method is invoked in an inner class


If the inner class has no such method, then it is assumed to
be an invocation of the method of that name in the outer
class
If both the inner and outer class have a method with the
same name, then it is assumed to be an invocation of the
method in the inner class
If both the inner and outer class have a method with the
same name, and the intent is to invoke the method in the
outer class, then the following invocation must be used:
OuterClassName.this.methodName();
using this inside an inner class refers to the object of the
inner class
Nesting Inner Classes

It is legal to nest inner classes within inner classes


The rules are the same as before, but the names get longer
Given class A, which has public inner class B, which has
public inner class C, then the following is valid:
A aObject = new A();
A.B bObject = aObject.new B();
A.B.C cObject = bObject.new C();
Inner Classes and Inheritance

Given an OuterClass that has an


InnerClass
Any DerivedClass of OuterClass will
automatically have InnerClass as an inner class
In this case, the DerivedClass cannot override the
InnerClass
An outer class can be a derived class
An inner class can be a derived class also
Local Classes

A local class is defined within a block of Java code.


Local classes are completely hidden in their
containing block.
When a class name is used only within a block it can
be defined locally.
A local class can access instance variables of the outer
class and only the final local variables of the enclosing
block.
Local Classes: Example
class LocalClassExample{
private String name = "Jesse";

public void method ( ) {


int j = 20;
final int k = 30;

class Local {
public void test ( ) {
//System.out.println(j); //Error as j is not final
System.out.println(k); //OK k is final

//Like an inner class, instance variables of


//the enclosing object can be accessed.
System.out.println ( name ) ;
}
}
Local loc = new Local ( ) ;
loc.test ( ) ;
}

public static void main ( String [ ] args ) {


LocalClassExample obj = new LocalClassExample ( );
obj.method ( ) ;
}
}
Anonymous Inner Classes

It is a local class without a name


If only one object has to be created from a class, and there is
no need to name the class, then an anonymous class
definition can be used
The class definition is embedded inside the expression with the new
operator
Anonymous class has no constructors
It is either derived from a class, or implements an interface.
Like:
–AnInterface i = new AnInterface ( ) { // methods defs. … };
–ASuperclass c = new ASuperclass(…) { // methods defs. … };
Anonymous Inner Classes(Cont'd)

An anonymous inner class must always extend a superclass or
implement an interface, but it cannot have an explicit extends
or implements clause.

An anonymous inner class must implement all the abstract
methods in the superclass or in the interface.

An anonymous inner class always uses the no-arg constructor
from its superclass to create an instance. If an anonymous inner
class implements an interface, the constructor is Object().

An anonymous inner class is compiled into a class named
OuterClassName$n.class. For example, if the outer class Test
has two anonymous inner classes, these two classes are
compiled into Test$1.class and Test$2.class.
Anonymous Inner Classes
Anonymous Inner Classes
Anonymous Inner Classes
Read More

[Read more on inner classes on pages 564-


565 of Java How to Program, 9th Edition
or get a better book]

You might also like