Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views23 pages

Inner Class

The document provides an overview of Java inner classes, including their types such as member inner classes, static nested classes, local inner classes, and anonymous inner classes. It explains the access levels, advantages, and differences between normal inner classes and static nested classes, as well as the concept of nested interfaces. Additionally, it includes examples and syntax for defining and using these classes and interfaces in Java.

Uploaded by

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

Inner Class

The document provides an overview of Java inner classes, including their types such as member inner classes, static nested classes, local inner classes, and anonymous inner classes. It explains the access levels, advantages, and differences between normal inner classes and static nested classes, as well as the concept of nested interfaces. Additionally, it includes examples and syntax for defining and using these classes and interfaces in Java.

Uploaded by

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

1.Java inner class or nested class is a class that is declared inside the class or interface.

2.Nested classes represent a particular type of relationship that is it can access all the members
(data members and methods) of the outer class, including private.
3.Nested classes are used to develop more readable and maintainable code because it logically
group classes and interfaces in one place only.
4.Code Optimization: It requires less code to write.

Non static class:


Member Inner Class A class created within class and outside method.

Anonymous Inner Class A class created for implementing an interface or extending class.
The java compiler decides its name.

Local Inner Class A class was created within the method.

Static Nested Class A static class was created within the class.
Nested Interface An interface created within class or interface.
Inner Class

• Like variables and methods, class can also be defined inside a class.

• An inner class is a class defined inside the scope of another class.

• Classes that were covered so far were top-level classes.

• The class inside which the inner class is defined is called outer class.

• Inner class can access even the private members of the outer class. Similarly
outer class can also access the private members of inner class.

• Similar to inner class, inner interface can also be created.


Inner class example

• Inside the Outer class scope, Inner class name is same as what is declared in
Outer class.

• Outside the Outer class scope, Inner class name is combination of Outer class
name and Inner class name and a dot (.) separating them. Inner class name can
be same as outer class name.

• The name of the inner class’s .class file name:

• OuterClass$InnerClass.class
Types of Inner Class

• Member class
• Static Inner Class/ Top-Level nested classes
• Non Static Inner Class
• Local Inner Class
• Anonymous Class
• Non Static Inner Class, Local Inner Class, Anonymous Class are generally
called inner class.
• Static inner class are considered to be top-level class.
Types of Inner Class

• Non static inner class object cannot be created without a outer class instance.
• The private fields and methods of the member classes are available to the
enclosing class and other member classes
• All the private fields and methods of the outer classes are also available to inner
class.
• Non-static inner class cannot have static members.
• Other modifier applicable here are abstract, final, public, protected, private
Name conflict

If the name of the members in Outer class and inner class are same, then how to refer
to the name of the outer class member in the inner class?

This can be done using Outer class name dot (.) this dot (.) member name.

class C {
int i=10;
class B
{
int i=C.this.i+10;
}
}
Static Inner Class

A static inner class is a class that’s a static member of the outer class

It can access only all static members of the outer class.

But like main method, instances of outer class can be created inside static inner
class and using this private members can be accessed.

It is created without an instance of the outer class unlike the regular inner classes.
static classes are also called top-level nested classes.

Other modifier applicable to member classes abstract, final, public, protected,


private
Static inner class : Syntax

//Class definition: //Instance creation inside the Outer class


public class Outer Inner innerObj=new Inner();
{
public static class Inner //Instance creation outside the Outer class
{ Outer outerObj=new Outer();
} Outer.Inner innerObj=Outer.new Inner();

}
How to access Static and Non-static members of Outer class from Static nested class
1. Inside the static nested class, we can declare both static and non-static members, including
the main method.

2. Both static and non-static members of the static nested class can directly access all the
static data members (variables) and static functions (methods) of its outer class.

3. A static nested class cannot access non-static members of its outer class because it does not
have an implicit reference to an outer object.

4. Non-static members of a normal inner class can access the static members of any static
nested class within the same outer class.
Use of Static Nested Class in Java
There are the following advantages of using a static nested class in Java. They are as
follows:

1. A static nested class can directly access the static members of its outer class including
private static members.

2. It acts as a direct member of its top-level class, not a member of the package in which it
is declared.

3. Without an existing outer class object, there may be a chance of an existing static nested
class. To access static members of the static nested class, there is no need for an outer class
object.
Difference between Normal inner class and Static nested class in Java
1. In the case of a normal or regular inner class, without an existing outer class object, there is
no chance of existing inner class object
whereas, in the case of a static nested class, without an existing outer class object, there may
be a chance of existing nested class object.

2. An inner class object is strongly associated with an outer class object whereas, a static
nested class is not strongly associated with an outer class object.
3. In normal inner class, we cannot declare any static members but in the static nested class,
we can declare a static member including the main method.

4. Since we cannot declare the main method in the normal inner class, therefore, we cannot
run inner class directly from the command prompt.
But we can declare the main method and can also run the static nested class directly from the
command prompt.

5. A normal inner class can access both static and non-static members of the outer class
directly but from the static nested class, we can access only static members.
Local Inner class

An inner class that is defined inside a method is called local inner class (or method
local inner class).

A local inner class can be instantiated only by the method which defined it.

Therefore no access specifier is applicable for the local inner class declaration.
Only abstract and final modifiers are allowed.

Also like other inner classes, local inner class can access all the members of the
outer class including private members.
Local Inner class
• Apart from the above, the local inner class can also access local variables which
are final.
class C {
int i=10;
void process( )
{
class B
{
B( )
{
System.out.println("I am local inner class");
}
}
B b=new B();
}
}
Anonymous Inner Classes

• Inner class without a class name is an anonymous inner class.

• Allows creation of one time use object !

• Anonymous inner class can be created either inside a method or outside a


method. It is implicitly final.

• No modifier is allowed anywhere in the class declaration

• Also declaration cannot have an implements or extends clause.

• No constructors can be defined.


Anonymous Inner Classes

• An anonymous inner class is either inherited from an interface or from a class


and so polymorphism is applicable. It cannot inherit from more than one class
directly.

Test t = new Test() // Test can be interface,abstract/concrete class


{
public void test_method() // data members and methods
{
........
}
}
Anonymous Inner class - Example

package vit.demo; System.out.println("i am anony");


interface B{ }
public void display(); };
} }

class C { public class Main {


int i=10; public static void main(String s[]) {
B b=new B(){ new C().b.display();
public void display(){ }
}
Nested Interface
An interface, i.e., declared within another interface or class, is known as a nested interface.
The nested interfaces are used to group related interfaces so that they can be easy to
maintain.

The nested interface must be referred to by the outer interface or class. It can't be accessed
directly.

Points to remember for nested interfaces


 There are given some points that should be remembered by the java programmer.
 The nested interface must be public if it is declared inside the interface, but it can have
any access modifier if declared within the class.
 Nested interfaces are declared static
Syntax of nested interface which is declared within the interface

interface interface_name
{
...
interface nested_interface_name
{
...
}
}
Syntax of nested interface which is declared within the class
class class_name{
...
interface nested_interface_name
{
...
}
}
interface Showable
{
void show();
interface Message
{
void msg();
}
}
class TestNestedInterface1 implements Showable.Message
{
public void msg(){System.out.println("Hello nested interface");}

public static void main(String args[])


{
Showable.Message message=new TestNestedInterface1();//upcasting here
message.msg();
}
}

You might also like