Java & JEE Training
Session 21 – Inner Classes
Page 1Classification: Restricted
Agenda
• Inner Classes
• Method-local Inner Class
• Anonymous Inner Class
• Static Nested Inner Class
Page 2Classification: Restricted
Inner Classes
Page 3Classification: Restricted
Inner Classes (Non-static Nested Classes)
class Outer_Demo {
int num;
// inner class
private class Inner_Demo {
public void print() {
System.out.println("This is an inner class");
}
}
// Accessing he inner class from the method within
void display_Inner() {
Inner_Demo inner = new Inner_Demo();
inner.print();
}
}
public class My_class {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Accessing the display_Inner() method.
outer.display_Inner();
}
}
Page 4Classification: Restricted
Example: Accessing private members using Inner Class
class Outer_Demo {
// private variable of the outer class
private int num = 175;
// inner class
public class Inner_Demo {
public int getNum() {
System.out.println("This is the getnum method of the inner class");
return num;
}
}
}
public class My_class2 {
public static void main(String args[]) {
// Instantiating the outer class
Outer_Demo outer = new Outer_Demo();
// Instantiating the inner class
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();
System.out.println(inner.getNum());
}
}
Page 5Classification: Restricted
Method-local Inner Class
public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 23;
// method-local inner class
class MethodInner_Demo {
public void print() {
System.out.println("This is method inner class "+num);
}
} // end of inner class
// Accessing the inner class
MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}
public static void main(String args[]) {
Outerclass outer = new Outerclass();
outer.my_Method();
}
}
Page 6Classification: Restricted
Anonymous Inner Class
abstract class AnonymousInner {
public abstract void mymethod();
}
public class Outer_class {
public static void main(String args[]) {
AnonymousInner inner = new AnonymousInner() {
public void mymethod() {
System.out.println("This is an example of anonymous inner class");
}
};
inner.mymethod();
}
}
Page 7Classification: Restricted
Example: Anonymous Inner Class as Argument
// interface
interface Message {
String greet();
}
public class My_class {
// method which accepts the object of interface Message
public void displayMessage(Message m) {
System.out.println(m.greet() +
", This is an example of anonymous inner class as an argument");
}
public static void main(String args[]) {
// Instantiating the class
My_class obj = new My_class();
// Passing an anonymous inner class as an argument
obj.displayMessage(new Message() {
public String greet() {
return "Hello";
}
});
}
}
Page 8Classification: Restricted
Static Nested Class
public class Outer {
static class Nested_Demo {
public void my_method() {
System.out.println("This is my nested class");
}
}
public static void main(String args[]) {
Outer.Nested_Demo nested = new Outer.Nested_Demo();
nested.my_method();
}
}
Page 9Classification: Restricted
Why use Nested classes?
• It is a way of logically grouping classes that are only used in one place.
• It increases encapsulation.
• Nested classes “can” lead to more readable and maintainable code.
• Child to parent class connection is simpler as it visually illustrates the
variables and methods of each class.
Page 10Classification: Restricted
Topics to be covered in next session
• Java IO
• Files
• Streams
• Byte-based
• Character-based
• Object Serialization
Page 11Classification: Restricted
Thank you!

More Related Content

PPTX
Java Inner Class
PDF
Java Inner Classes
PPT
Inner classes ,annoumous and outer classes in java
DOCX
Nested classes in java
PPTX
Inner class
DOCX
Nested class in java
PPTX
C# Inheritance
PPTX
Inner classes
Java Inner Class
Java Inner Classes
Inner classes ,annoumous and outer classes in java
Nested classes in java
Inner class
Nested class in java
C# Inheritance
Inner classes

What's hot (19)

PPT
L5 classes, objects, nested and inner class
PPTX
Inner Classes & Multi Threading in JAVA
PPTX
Access specifier
PPTX
[OOP - Lec 07] Access Specifiers
PPTX
C# Access modifiers
PPTX
Inner classes
PPTX
class c++
PPT
object oriented programming language by c++
PPT
Content Providers in Android
PPTX
Session 13 - Exception handling - continued
PPT
Oepnfiledialog
PDF
Class and object in C++ By Pawan Thakur
PDF
Java Programming - 04 object oriented in java
PPTX
Presentation on class and object in Object Oriented programming.
PPTX
PDF
How to write you first class in c++ object oriented programming
PPTX
Pj01 x-classes and objects
PPT
Classes and objects
PPT
Class and object in c++
L5 classes, objects, nested and inner class
Inner Classes & Multi Threading in JAVA
Access specifier
[OOP - Lec 07] Access Specifiers
C# Access modifiers
Inner classes
class c++
object oriented programming language by c++
Content Providers in Android
Session 13 - Exception handling - continued
Oepnfiledialog
Class and object in C++ By Pawan Thakur
Java Programming - 04 object oriented in java
Presentation on class and object in Object Oriented programming.
How to write you first class in c++ object oriented programming
Pj01 x-classes and objects
Classes and objects
Class and object in c++
Ad

Similar to Session 21 - Inner Classes (20)

PPSX
Inner Classes
PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
PPTX
Java Programming inner and Nested classes.pptx
PDF
Java Inner Classes
PDF
Developing Applications for Android - Lecture#2
PPTX
Java- Nested Classes
PPT
Inner Classes
PPTX
PPTX
Inner classes in java
PPTX
Constructor&method
PPTX
Dynamic method dispatch
PPTX
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
PPTX
Access controlaspecifier and visibilty modes
PPT
02-OOP with Java.ppt
PPT
025466482929 -OOP with Java Development Kit.ppt
PPTX
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
PPT
Java oops PPT
PDF
Java OO Revisited
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Module 3 and 4-javahjhhdhdhddhhdhdhdhd.pptx
Inner Classes
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
Java Programming inner and Nested classes.pptx
Java Inner Classes
Developing Applications for Android - Lecture#2
Java- Nested Classes
Inner Classes
Inner classes in java
Constructor&method
Dynamic method dispatch
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
Access controlaspecifier and visibilty modes
02-OOP with Java.ppt
025466482929 -OOP with Java Development Kit.ppt
OBJECT ORIENTED PROGRAMMING STRUCU2.pptx
Java oops PPT
Java OO Revisited
Modules 333333333³3444444444444444444.pptx
Module 3 and 4-javahjhhdhdhddhhdhdhdhd.pptx
Ad

More from PawanMM (20)

PPTX
Session 48 - JS, JSON and AJAX
PPTX
Session 46 - Spring - Part 4 - Spring MVC
PPTX
Session 45 - Spring - Part 3 - AOP
PPTX
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PPTX
Session 42 - Struts 2 Hibernate Integration
PPTX
Session 41 - Struts 2 Introduction
PPTX
Session 40 - Hibernate - Part 2
PPTX
Session 39 - Hibernate - Part 1
PPTX
Session 38 - Core Java (New Features) - Part 1
PPTX
Session 37 - JSP - Part 2 (final)
PPTX
Session 36 - JSP - Part 1
PPTX
Session 35 - Design Patterns
PPTX
Session 34 - JDBC Best Practices, Introduction to Design Patterns
PPTX
Session 33 - Session Management using other Techniques
PPTX
Session 32 - Session Management using Cookies
PPTX
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PPTX
Session 30 - Servlets - Part 6
PPTX
Session 29 - Servlets - Part 5
PPTX
Session 28 - Servlets - Part 4
Session 48 - JS, JSON and AJAX
Session 46 - Spring - Part 4 - Spring MVC
Session 45 - Spring - Part 3 - AOP
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 43 - Spring - Part 1 - IoC DI Beans
Session 42 - Struts 2 Hibernate Integration
Session 41 - Struts 2 Introduction
Session 40 - Hibernate - Part 2
Session 39 - Hibernate - Part 1
Session 38 - Core Java (New Features) - Part 1
Session 37 - JSP - Part 2 (final)
Session 36 - JSP - Part 1
Session 35 - Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 33 - Session Management using other Techniques
Session 32 - Session Management using Cookies
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 30 - Servlets - Part 6
Session 29 - Servlets - Part 5
Session 28 - Servlets - Part 4

Recently uploaded (20)

PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Unlock new opportunities with location data.pdf
PDF
Getting Started with Data Integration: FME Form 101
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PPT
Geologic Time for studying geology for geologist
DOCX
search engine optimization ppt fir known well about this
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
DP Operators-handbook-extract for the Mautical Institute
PDF
STKI Israel Market Study 2025 version august
PPTX
Chapter 5: Probability Theory and Statistics
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
Architecture types and enterprise applications.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
O2C Customer Invoices to Receipt V15A.pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
Unlock new opportunities with location data.pdf
Getting Started with Data Integration: FME Form 101
Assigned Numbers - 2025 - Bluetooth® Document
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
Enhancing emotion recognition model for a student engagement use case through...
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Geologic Time for studying geology for geologist
search engine optimization ppt fir known well about this
A novel scalable deep ensemble learning framework for big data classification...
WOOl fibre morphology and structure.pdf for textiles
DP Operators-handbook-extract for the Mautical Institute
STKI Israel Market Study 2025 version august
Chapter 5: Probability Theory and Statistics
A review of recent deep learning applications in wood surface defect identifi...
Architecture types and enterprise applications.pdf

Session 21 - Inner Classes

  • 1. Java & JEE Training Session 21 – Inner Classes
  • 2. Page 1Classification: Restricted Agenda • Inner Classes • Method-local Inner Class • Anonymous Inner Class • Static Nested Inner Class
  • 4. Page 3Classification: Restricted Inner Classes (Non-static Nested Classes) class Outer_Demo { int num; // inner class private class Inner_Demo { public void print() { System.out.println("This is an inner class"); } } // Accessing he inner class from the method within void display_Inner() { Inner_Demo inner = new Inner_Demo(); inner.print(); } } public class My_class { public static void main(String args[]) { // Instantiating the outer class Outer_Demo outer = new Outer_Demo(); // Accessing the display_Inner() method. outer.display_Inner(); } }
  • 5. Page 4Classification: Restricted Example: Accessing private members using Inner Class class Outer_Demo { // private variable of the outer class private int num = 175; // inner class public class Inner_Demo { public int getNum() { System.out.println("This is the getnum method of the inner class"); return num; } } } public class My_class2 { public static void main(String args[]) { // Instantiating the outer class Outer_Demo outer = new Outer_Demo(); // Instantiating the inner class Outer_Demo.Inner_Demo inner = outer.new Inner_Demo(); System.out.println(inner.getNum()); } }
  • 6. Page 5Classification: Restricted Method-local Inner Class public class Outerclass { // instance method of the outer class void my_Method() { int num = 23; // method-local inner class class MethodInner_Demo { public void print() { System.out.println("This is method inner class "+num); } } // end of inner class // Accessing the inner class MethodInner_Demo inner = new MethodInner_Demo(); inner.print(); } public static void main(String args[]) { Outerclass outer = new Outerclass(); outer.my_Method(); } }
  • 7. Page 6Classification: Restricted Anonymous Inner Class abstract class AnonymousInner { public abstract void mymethod(); } public class Outer_class { public static void main(String args[]) { AnonymousInner inner = new AnonymousInner() { public void mymethod() { System.out.println("This is an example of anonymous inner class"); } }; inner.mymethod(); } }
  • 8. Page 7Classification: Restricted Example: Anonymous Inner Class as Argument // interface interface Message { String greet(); } public class My_class { // method which accepts the object of interface Message public void displayMessage(Message m) { System.out.println(m.greet() + ", This is an example of anonymous inner class as an argument"); } public static void main(String args[]) { // Instantiating the class My_class obj = new My_class(); // Passing an anonymous inner class as an argument obj.displayMessage(new Message() { public String greet() { return "Hello"; } }); } }
  • 9. Page 8Classification: Restricted Static Nested Class public class Outer { static class Nested_Demo { public void my_method() { System.out.println("This is my nested class"); } } public static void main(String args[]) { Outer.Nested_Demo nested = new Outer.Nested_Demo(); nested.my_method(); } }
  • 10. Page 9Classification: Restricted Why use Nested classes? • It is a way of logically grouping classes that are only used in one place. • It increases encapsulation. • Nested classes “can” lead to more readable and maintainable code. • Child to parent class connection is simpler as it visually illustrates the variables and methods of each class.
  • 11. Page 10Classification: Restricted Topics to be covered in next session • Java IO • Files • Streams • Byte-based • Character-based • Object Serialization