NESTED CLASSES
A class declared within another class is called a
Nested Class
Outer class
Inner class
l Kinds Of Classes
l
--- Top level classes
l - declared inside package
l - visible throughout package.
l - public classes must be defined in their own file.
l
--- Nested and inner classes
l - declared inside class (or method).
l - can be visible only to outer class, or have wider
visibility.
Types Of Nested Classes
l 1. Non-Static Nested Classes
l i. Member inner class
l ii. Local inner class
l Iii. Anonymous inner class
l 2. Static Nested Classes
l
l
l
Kinds of inner/nested class
--- Inner class
l - defined inside another class.
l - but each instance of an inner class is transparently associated
with an instance of the outer class.
--- Anonymous inner classes
l - unnamed inner classes.
--- Nested class
l - defined inside another class.
l - has access to private members of enclosing class.
l
l Member Inner Class example:
l class Outer{
l private int data=30;
l class Inner{
l void msg(){
l System.out.println("data is "+data);}
l } //close inner class
l public static void main(String args[]){
l Outer obj=new Outer();
l Outer.Inner in=obj.new Inner();
l in.msg();
l }
l }
l
l Local Inner Class example:
l public class localInner{
l private int data=30;//instance variable
l void display(){
l class Local{
l void msg(){System.out.println(data);}
l }
l Local l=new Local();
l l.msg();
l } //close method
l public static void main(String args[]){
l localInner obj=new localInner();
l obj.display();
l }
l }
l Anonymous Inner Class:(using Interface)
l
l interface Eatable{
l void eat();
l }
l class TestAnnonymousInner1{
l public static void main(String args[]){
l Eatable e=new Eatable(){
l public void eat(){System.out.println("nice fruits");}
l };
l e.eat();
l }
l }
l Anonymous Inner Class(using Class):
l
l abstract class Person{
l abstract void eat();
l }
l class TestAnonymousInner{
l public static void main(String args[]){
l Person p=new Person(){
l void eat(){System.out.println("nice fruits");}
l };
l p.eat();
l }
l }
l
l Static Nested Class example:(for instance
method)
l class TestOuter{
l static int data=30;
 void m1() {
 System.out.println(“instance method”);}
l static class Inner{
l void msg(){System.out.println("data is "+data);}
l }
l public static void main(String args[]){
l TestOuter t = new TestOuter();
l t.m1();
l TestOuter.Inner obj=new TestOuter.Inner();
l obj.msg();
l }
l }
l Static Nested Class(for static method):
l
l class TestOuter{
l static int data=30;
l static class Inner{
l static void msg(){System.out.println("data is "+data);}
l }
l public static void main(String args[]){
l TestOuter.Inner.msg();//no need to create the instance of
static nested class
l }
l }
l Nested Interface example:
l interface Showable{
l void show();
l interface Message{
l void msg(); }
l }
l class TestNestedInterface implements Showable.Message{
l public void msg(){System.out.println("Hello nested
interface");}
l public static void main(String args[]){
l Showable.Message message=new
TestNestedInterface(); //upcasting here
l message.msg();
l }
l }
l Nested Interface:
l interface Room{
l void show();
l interface Almarah{
l void msg(); }
l }
l class TestNestedInterface implements Room,Room.Almarah{
l public void msg(){System.out.println("Hello nested
interface");}
l public void show(){System.out.println(“show method”);}
l public static void main(String args[]){
l Room room = new TestNestedInterface ();
l Room.Almarah roomAlmarah = new TestNestedInterface1();
l room.show();
l roomAlmariah.msg();
l }
l }
l Interface within a class:
l class Chat{
l void classMethod() {System.out.println(“Class Method”);}
l interface Message{
l void msg(); }
l }
l class TestNestedInterface extends Chat implements
Chat.Message{
l public void msg(){System.out.println("Hello nested interface");}
l public static void main(String args[]){
l Chat chat = new TestNestedInterface();
l Chat.Message message=new TestNestedInterface();
l chat.classMethod();
l message.msg();
l }
l }
l Class within interface:
l interface Message{
l void msg(); }
l class Chat{
l void classMethod() {System.out.println(“Class Method”);}
l }
l class TestNestedInterface extends Message.Chat
implements Message{
l public void msg(){System.out.println("Hello nested
interface");}
l public static void main(String args[]){
l Message message = new TestNestedInterface();
l Message.Chat chat=new TestNestedInterface();
l chat.classMethod();
l message.msg();
l }
l }
l
l Uses of Nested Classes:
l
l 1. it can access all the members (data members and methods) of
outer class including private.
l
l 2. to develop more readable and maintainable code .
l
l 3. Code Optimization.
l

More Related Content

PDF
Java Inner Classes
PPTX
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
DOCX
Lecture22.23.07.2014
PPTX
PDF
Design of OO language
PDF
Java Inner Classes
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPTX
Presentation on class and object in Object Oriented programming.
Java Inner Classes
Дмитрий Контрерас «Back to the future: the evolution of the Java Type System»
Lecture22.23.07.2014
Design of OO language
Java Inner Classes
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Presentation on class and object in Object Oriented programming.

What's hot (18)

DOCX
Nested class in java
PPTX
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PDF
Crystal: tipos, peculiaridades y desafios
PDF
DIY: Analyse statique en Java
ODP
Class&objects
PPTX
Unit 5 java-awt (1)
PPTX
study of java
PDF
Class graph neo4j and software metrics
PDF
Java Programming - 04 object oriented in java
PPTX
introduction to c #
PPTX
Class and object
PPTX
class c++
PPTX
Access controlaspecifier and visibilty modes
PPT
Control statements
PDF
Friend function in c++
PPTX
Multiple Inheritance
PDF
Under the hood of scala implicits (kl10tch 10.03.2015)
PPTX
Inheritance
Nested class in java
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Crystal: tipos, peculiaridades y desafios
DIY: Analyse statique en Java
Class&objects
Unit 5 java-awt (1)
study of java
Class graph neo4j and software metrics
Java Programming - 04 object oriented in java
introduction to c #
Class and object
class c++
Access controlaspecifier and visibilty modes
Control statements
Friend function in c++
Multiple Inheritance
Under the hood of scala implicits (kl10tch 10.03.2015)
Inheritance
Ad

Similar to Inner classes (20)

PPTX
Java Programming inner and Nested classes.pptx
PPTX
Inner Classes & Multi Threading in JAVA
PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
PPTX
Nested classes in java
PPTX
Inner class
PPTX
Nested class
PPTX
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
PPTX
Inner classes in java
PPTX
types of classes in java
PPTX
Java- Nested Classes
PPTX
Javasession8
PPTX
Java Inner Class
PPT
Inner classes9 cm604.28
DOCX
Nested classes in java
PPTX
Java Nested class Concept
PPTX
Session 21 - Inner Classes
PPT
Inner classes ,annoumous and outer classes in java
PDF
Inner Classes in Java
PPTX
[Java] #8 String and Inner Class
Java Programming inner and Nested classes.pptx
Inner Classes & Multi Threading in JAVA
A1771937735_21789_14_2018__16_ Nested Classes.ppt
Nested classes in java
Inner class
Nested class
Java Nested classes, static class and methods, nested blocks_Inner_Classes.pptx
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
Inner classes in java
types of classes in java
Java- Nested Classes
Javasession8
Java Inner Class
Inner classes9 cm604.28
Nested classes in java
Java Nested class Concept
Session 21 - Inner Classes
Inner classes ,annoumous and outer classes in java
Inner Classes in Java
[Java] #8 String and Inner Class
Ad

Recently uploaded (20)

PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Architecture types and enterprise applications.pdf
PDF
August Patch Tuesday
PDF
STKI Israel Market Study 2025 version august
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PPTX
Benefits of Physical activity for teenagers.pptx
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Unlock new opportunities with location data.pdf
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
CloudStack 4.21: First Look Webinar slides
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Getting Started with Data Integration: FME Form 101
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Getting started with AI Agents and Multi-Agent Systems
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
The various Industrial Revolutions .pptx
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPTX
Modernising the Digital Integration Hub
Group 1 Presentation -Planning and Decision Making .pptx
Architecture types and enterprise applications.pdf
August Patch Tuesday
STKI Israel Market Study 2025 version august
NewMind AI Weekly Chronicles – August ’25 Week III
Benefits of Physical activity for teenagers.pptx
Univ-Connecticut-ChatGPT-Presentaion.pdf
Unlock new opportunities with location data.pdf
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
CloudStack 4.21: First Look Webinar slides
sustainability-14-14877-v2.pddhzftheheeeee
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Getting Started with Data Integration: FME Form 101
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Getting started with AI Agents and Multi-Agent Systems
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
The various Industrial Revolutions .pptx
Taming the Chaos: How to Turn Unstructured Data into Decisions
Modernising the Digital Integration Hub

Inner classes

  • 1. NESTED CLASSES A class declared within another class is called a Nested Class Outer class Inner class
  • 2. l Kinds Of Classes l --- Top level classes l - declared inside package l - visible throughout package. l - public classes must be defined in their own file. l --- Nested and inner classes l - declared inside class (or method). l - can be visible only to outer class, or have wider visibility.
  • 3. Types Of Nested Classes l 1. Non-Static Nested Classes l i. Member inner class l ii. Local inner class l Iii. Anonymous inner class l 2. Static Nested Classes l l l
  • 4. Kinds of inner/nested class --- Inner class l - defined inside another class. l - but each instance of an inner class is transparently associated with an instance of the outer class. --- Anonymous inner classes l - unnamed inner classes. --- Nested class l - defined inside another class. l - has access to private members of enclosing class. l
  • 5. l Member Inner Class example: l class Outer{ l private int data=30; l class Inner{ l void msg(){ l System.out.println("data is "+data);} l } //close inner class l public static void main(String args[]){ l Outer obj=new Outer(); l Outer.Inner in=obj.new Inner(); l in.msg(); l } l } l
  • 6. l Local Inner Class example: l public class localInner{ l private int data=30;//instance variable l void display(){ l class Local{ l void msg(){System.out.println(data);} l } l Local l=new Local(); l l.msg(); l } //close method l public static void main(String args[]){ l localInner obj=new localInner(); l obj.display(); l } l }
  • 7. l Anonymous Inner Class:(using Interface) l l interface Eatable{ l void eat(); l } l class TestAnnonymousInner1{ l public static void main(String args[]){ l Eatable e=new Eatable(){ l public void eat(){System.out.println("nice fruits");} l }; l e.eat(); l } l }
  • 8. l Anonymous Inner Class(using Class): l l abstract class Person{ l abstract void eat(); l } l class TestAnonymousInner{ l public static void main(String args[]){ l Person p=new Person(){ l void eat(){System.out.println("nice fruits");} l }; l p.eat(); l } l } l
  • 9. l Static Nested Class example:(for instance method) l class TestOuter{ l static int data=30;  void m1() {  System.out.println(“instance method”);} l static class Inner{ l void msg(){System.out.println("data is "+data);} l } l public static void main(String args[]){ l TestOuter t = new TestOuter(); l t.m1(); l TestOuter.Inner obj=new TestOuter.Inner(); l obj.msg(); l } l }
  • 10. l Static Nested Class(for static method): l l class TestOuter{ l static int data=30; l static class Inner{ l static void msg(){System.out.println("data is "+data);} l } l public static void main(String args[]){ l TestOuter.Inner.msg();//no need to create the instance of static nested class l } l }
  • 11. l Nested Interface example: l interface Showable{ l void show(); l interface Message{ l void msg(); } l } l class TestNestedInterface implements Showable.Message{ l public void msg(){System.out.println("Hello nested interface");} l public static void main(String args[]){ l Showable.Message message=new TestNestedInterface(); //upcasting here l message.msg(); l } l }
  • 12. l Nested Interface: l interface Room{ l void show(); l interface Almarah{ l void msg(); } l } l class TestNestedInterface implements Room,Room.Almarah{ l public void msg(){System.out.println("Hello nested interface");} l public void show(){System.out.println(“show method”);} l public static void main(String args[]){ l Room room = new TestNestedInterface (); l Room.Almarah roomAlmarah = new TestNestedInterface1(); l room.show(); l roomAlmariah.msg(); l } l }
  • 13. l Interface within a class: l class Chat{ l void classMethod() {System.out.println(“Class Method”);} l interface Message{ l void msg(); } l } l class TestNestedInterface extends Chat implements Chat.Message{ l public void msg(){System.out.println("Hello nested interface");} l public static void main(String args[]){ l Chat chat = new TestNestedInterface(); l Chat.Message message=new TestNestedInterface(); l chat.classMethod(); l message.msg(); l } l }
  • 14. l Class within interface: l interface Message{ l void msg(); } l class Chat{ l void classMethod() {System.out.println(“Class Method”);} l } l class TestNestedInterface extends Message.Chat implements Message{ l public void msg(){System.out.println("Hello nested interface");} l public static void main(String args[]){ l Message message = new TestNestedInterface(); l Message.Chat chat=new TestNestedInterface(); l chat.classMethod(); l message.msg(); l } l }
  • 15. l l Uses of Nested Classes: l l 1. it can access all the members (data members and methods) of outer class including private. l l 2. to develop more readable and maintainable code . l l 3. Code Optimization. l