0% found this document useful (0 votes)
17 views17 pages

Lecture2pptx 2024 08 28 22 03 17

Uploaded by

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

Lecture2pptx 2024 08 28 22 03 17

Uploaded by

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

Inner Classes

Inner Classes
Inner classes, also called Nested
Classes, are nothing but classes that are
defined within other classes.
Here is a simple example of an inner class
class Outer
{
class inner
{
}
}
// Demonstrate an inner class.
class Outer
{
int outer_x = 100;
void test()
{
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner
{
void display() {
System.out.println("display: outer_x = " + outer_x);
}}}
class InnerClassDemo
{
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}}
public class OuterClass
{
private int x=10;
private static int y=20;
class Inner
{
public void m1()
{
System.out.println("instance variable..."+x+",static variable..."+y);
}
}

public static void main(String[] args)


{
OuterClass o=new OuterClass();
OuterClass.Inner in=o.new Inner();
in.m1();

}
}
Types of Inner Classes
Inner classes nest within other classes.
Inner classes are of the following types:
1. Static Inner classes
2. Local Inner classes
3. Anonymous Inner classes
A Static Inner Class is a static
member of a class. Like any other static
method, a static member class has
access to all static methods of the
parent, or top-level, class.

Local Inner classes are declared


within a block of code and are visible
only within that block, just as any other
method variable.

An anonymous class is a local class


that has no name.
Properties of Static Inner
Classes
Static members of the outer class are
visible to the static inner class.
An inner class may not
have static members unless the inner class
is itself marked as static.
Syntax for Static Class
Syntax for static inner class is as follows:

<access-specifier> class OuterClassName


{
public static class
<StaticInnerClassName>
{
...
}
...
}
Example of Static inner class
class Cover
{
static class InnerCover
{
void go()
{
System.out.println("I am the first Static Inner Class");
}}}
public class Broom
{
static class InnerBroom
{
void go1()
{
System.out.println("I am second Static Inner Class");
}}
public static void main(String[] args)
{
Cover.InnerCover demo = new Cover.InnerCover();
demo.go();
InnerBroom innerBroom = new InnerBroom ();
innerBroom .go1();
}}
Properties of Local Inner
Classes
Local classes are never declared with an
access specifier. Their scope is always
restricted to the block in which they are
declared.
Local classes have a great advantage: they
are completely hidden from the outside
world.
Syntax for Local Inner
Class
Syntax of the local inner class is as follows:
<access-specifier> class <OuterClassName>
{
code...
<access-specifier> <return-type>
<MethodName>(<arguments>)
{
class <LocalInnerClassName>
{
code...
}
code...
}
code...
}
public class happier
{
interface Smiler {
public void smile();
}
public static void main(String[] args)
{
c lass Happy implements Smiler {
private String more = "";

public void smile() {


System.out.println(":-)" + more);
}

public void happier() {


more += ")";
}}

Happy h1 = new Happy();


h1.smile();
Happy h2 = new Happy();
h2.happier();
h2.smile();
}}
Properties of Anonymous
classes
Here, SuperType can be an interface; then,
the inner class implements that interface.
Or SuperType can be a class; then, the
inner class extends that class.
An anonymous inner class cannot have
constructors because the name of a
constructor must be the same as the name
of a class, and the class has no name.
Instead, the construction parameters are
given to the superclass constructor.
Syntax for Anonymous Inner
Classes
This syntax for anonymous classes is very
cryptic.

new SuperType(construction
parameters)
{
inner class methods and data
}
Class Files generation for
Inner Classes
Each class can have more than one inner
classes. Once the main class is compiled
which has several inner classes, the
compiler generates separate class files for
each of the inner class.
public class Main
{
// Inner class Test1
class Test1
{
}
// Inner class Test2
class Test2
{
}
public static void main(String [] args)
{
// Anonymous inner class 1
new Object()
{};
// Anonymous inner class 2
new Object() {};
System.out.println("Hello World");
}
}

You might also like