0% found this document useful (0 votes)
30 views10 pages

What Are Inner Classes

Inner classes in Java allow a class to be defined within another class. There are four types of inner classes: non-static inner classes, static inner classes, local inner classes, and anonymous inner classes. Non-static inner classes have access to the enclosing class's methods and fields, while static inner classes do not have access to any specific instance of the outer class. Local inner classes are defined within a method of an outer class, and anonymous inner classes are used to quickly implement interfaces or abstract classes without explicitly defining a new class.

Uploaded by

Akshu
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)
30 views10 pages

What Are Inner Classes

Inner classes in Java allow a class to be defined within another class. There are four types of inner classes: non-static inner classes, static inner classes, local inner classes, and anonymous inner classes. Non-static inner classes have access to the enclosing class's methods and fields, while static inner classes do not have access to any specific instance of the outer class. Local inner classes are defined within a method of an outer class, and anonymous inner classes are used to quickly implement interfaces or abstract classes without explicitly defining a new class.

Uploaded by

Akshu
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/ 10

What are Inner Classes?

• In Java, an inner class is a class that is defined within another class.

• It's like a class inside another class, just as you can have a box inside another box.

• Compelling reasons for using nested classes include the following:

• It is a way of logically grouping classes that are only used in one place:

• It increases encapsulation:

• It can lead to more readable and maintainable code:


TYPES OF INNER CLASSES

• NON STATIC INNER CLASS

• STATIC INNER CLASS

• LOCAL INNER CLASS

• ANONYMOUS INNER CLASS


NON-STATIC INNER CLASS
• Let's call it "Class within a Class." It's like having a little helper class
inside a bigger class.Pattern:
class OuterClass {
// Some code and variables in the OuterClass
class InnerClass {
// Some code and variables in the InnerClass
}
}
ANALOGY & DEMO
• CAR AND A ENGINE
• Imagine a car (OuterClass) and its engine (InnerClass). The car needs
an engine to run correctly. The engine can access all the car's parts,
and without the car, the engine would have no purpose. In this
analogy, the car is the OuterClass, and the engine is the Non-Static
(Instance) Inner Class.
STATIC INNER CLASS
• Let's call it "Class within a Class, the Static Way." It's like a class inside
another class, but this time, it has a static keyword.
class OuterClass {
// Some code and variables in the OuterClass static class
StaticInnerClass {
// Some code and variables in the StaticInnerClass
}
}
ANALOGY &DEMO
• A School and Its Classroom*Consider a school (OuterClass) that has
classrooms (Static InnerClass).
• Each classroom is a separate entity with its own students and
activities, but it doesn't need a specific school instance to exist.
• classrooms exists independently and is associated with the school as
a whole.
• In this analogy, the school is the OuterClass, and the classroom is the
Static Inner Class.
Local Inner Class:*

• Let's call it the "Class within a Method." It's like a box created inside a
specific method of the OuterClass.
• Pattern:
class OuterClass {
// Some code and variables in the OuterClass
void someMethod() { // Some code in the method
class LocalInnerClass {
// Some code and variables in the LocalInnerClass } /
// More code in the method
}
}
• Explanation:The OuterClass is the big box, and the someMethod() is like a
compartment inside it.The LocalInnerClass is a small box that's created and used only
within the someMethod() of the OuterClass.It's a way to keep related functionality
together and hidden within the method, not accessible from outside.
Real-Life Analogy & Demo
• Real-Life Analogy: *A Kitchen and a Cutting Board*Think of a kitchen
(OuterClass) where you're preparing a meal. You take out a cutting
board (Local Inner Class) to chop vegetables.
• The cutting board is only needed within the kitchen and doesn't
serve any purpose outside of it. It's a temporary helper inside the
kitchen. In this analogy, the kitchen is the OuterClass, and the cutting
board is the Local Inner Clas
Anonymous Inner Class:
• Let's call it the "Mystery Class." It's a class without a name, and it's created and used on the
fly.
• Pattern:
class OuterClass
{ // Some code and variables in the OuterClass

SomeInterface someMethod() { return new SomeInterface() {


// Implementation of the interface's methods }; }}
• Explanation:
• The OuterClass is the big box, and the someMethod() is like a section of the box.Inside the
someMethod(), we are creating an anonymous class that implements the SomeInterface.It's
used for quick implementations of interfaces or abstract classes without explicitly defining a
new class for it.Remember, inner classes can be a bit advanced topic, so don't worry if it
takes a little time to fully grasp it. Play around with these examples and try to create your
own inner classes. The more you practice, the better you'll understand it.
Analogy &demo
• Imagine you are throwing a party (OuterClass), and you need to send
out invitations. You have a set format for invitations (an interface in
this case). To create each invitation, you write a personalized message
(Anonymous Inner Class) based on the recipient. The anonymous
inner class represents the personalized message for each invitation,
and it's only used for this specific purpose. In this analogy, the party is
the OuterClass, and the personalized invitation messages are the
Anonymous Inner Class.

You might also like