Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces

Static, Nested and Inner Classes

with Example Java Programs

Inner classes are the nested classes. That means these are the classes that are defined inside the other classes.

Static, Nested and Inner Classes

Inner classes are the nested classes. That means these are the classes that are defined inside the other classes. The syntax of defining the inner class is

Access modifier class OuterClass

{

//code

Access_modifier class InnerClass

{

//code

}

}

Following are some properties of inner class -

The outer class can inherit as many number of inner class objects as it wants.

• If the outer class and the corresponding inner class both are public then any other class can create an instance of this inner class.

• The inner class objects do not get instantiated with an outer class object.

• The outer class can call the private methods of inner class.

• Inner class code has free access to all elements of the outer class object that contains it.

• If the inner class has a variable with same name then the outer class's variable can be accessed like this-

outerclassname.this.variable_name

There are four types of inner classes -

1. Static member classes

• This inner class is defined as the static member variable of another class.

• Static members of the outer class are visible to the static inner class.

• The non-static members of the outer class are not available to inner class.

Syntax

Access modifier class OuterClass

{

//code

public static class InnerClass

{

//code

}

2. Member classes

• This type of inner class is non-static member of outer class.

3. Local classes

• This class is defined within a Java code just like a local variable.

• Local classes are never declared with an access specifier.

• The scope inner classes is always restricted to the block in which they are declared.

• The local classes are completely hidden from the outside world.

Syntax

Access_modifier class OuterClass

{

//code

Access_modifier return_type methodname (arguments)

{

class InnerClass

{

//code

}

//code

}

}

4. Anonymous classes

• Anonymous class is a local class without any name.

•Anonymous class is a one-shot class- created exactly where needed

• The anonymous class is created in following situations-santa

 о When the class has very short body.

 o Only one instance of the class is needed.

o Class is used immediately after defining it.

• The anonymous inner class can extend the class, it can implement the interface or it can be declared in method argument.

Example

class MyInnerClass implements Runnable

{

public void run()

{

System.out.println("Hello");

}

class DemoClass

{

public static void main(String[] arg)

{

MyInnerClass my new MyInnerClass();

Thread th=new Thread(my);

my.start();

}

}

}

Review Questions

1.What is static inner class? Explain with example.

2. Discuss in detail about inner class with its usefulness.

3.Define Inner classes. How to access object state usng inner classes? Give an example.

4.Discuss the object and inner classes with examples.

Object Oriented Programming: Unit II: Inheritance, Packages and Interfaces : Tag: : with Example Java Programs - Static, Nested and Inner Classes