SlideShare una empresa de Scribd logo
1 de 15
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

Más contenido relacionado

La actualidad más candente

Class&objects
Class&objectsClass&objects
Class&objects
harivng
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
Vinay Kumar
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
Alexander Podkhalyuzin
 

La actualidad más candente (18)

Nested class in java
Nested class in javaNested class in java
Nested class in java
 
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUSINHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
 
Crystal: tipos, peculiaridades y desafios
Crystal: tipos, peculiaridades y desafiosCrystal: tipos, peculiaridades y desafios
Crystal: tipos, peculiaridades y desafios
 
DIY: Analyse statique en Java
DIY: Analyse statique en JavaDIY: Analyse statique en Java
DIY: Analyse statique en Java
 
Class&objects
Class&objectsClass&objects
Class&objects
 
Unit 5 java-awt (1)
Unit 5 java-awt (1)Unit 5 java-awt (1)
Unit 5 java-awt (1)
 
study of java
study of java study of java
study of java
 
Class graph neo4j and software metrics
Class graph neo4j and software metricsClass graph neo4j and software metrics
Class graph neo4j and software metrics
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
introduction to c #
introduction to c #introduction to c #
introduction to c #
 
Class and object
Class and objectClass and object
Class and object
 
class c++
class c++class c++
class c++
 
Access controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modesAccess controlaspecifier and visibilty modes
Access controlaspecifier and visibilty modes
 
Control statements
Control statementsControl statements
Control statements
 
Friend function in c++
Friend function in c++ Friend function in c++
Friend function in c++
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
 
Inheritance
InheritanceInheritance
Inheritance
 

Similar a Inner classes

Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
Svetlin Nakov
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Java Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptxJava Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptx
AkashJha84
 

Similar a Inner classes (20)

Java
JavaJava
Java
 
Java session4
Java session4Java session4
Java session4
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
 
Presentation to java
Presentation  to  javaPresentation  to  java
Presentation to java
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdfch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
Session 21 - Inner Classes
Session 21 - Inner ClassesSession 21 - Inner Classes
Session 21 - Inner Classes
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
20.2 Java inheritance
20.2 Java inheritance20.2 Java inheritance
20.2 Java inheritance
 
Java OO Revisited
Java OO RevisitedJava OO Revisited
Java OO Revisited
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
 
Java class
Java classJava class
Java class
 
Java Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptxJava Programming inner and Nested classes.pptx
Java Programming inner and Nested classes.pptx
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
 

Último

Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Peter Udo Diehl
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 

Último (20)

Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi IbrahimzadeFree and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
Free and Effective: Making Flows Publicly Accessible, Yumi Ibrahimzade
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo DiehlFuture Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
Future Visions: Predictions to Guide and Time Tech Innovation, Peter Udo Diehl
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024What's New in Teams Calling, Meetings and Devices April 2024
What's New in Teams Calling, Meetings and Devices April 2024
 
A Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System StrategyA Business-Centric Approach to Design System Strategy
A Business-Centric Approach to Design System Strategy
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
Optimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through ObservabilityOptimizing NoSQL Performance Through Observability
Optimizing NoSQL Performance Through Observability
 
UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
Secure Zero Touch enabled Edge compute with Dell NativeEdge via FDO _ Brad at...
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
The Metaverse: Are We There Yet?
The  Metaverse:    Are   We  There  Yet?The  Metaverse:    Are   We  There  Yet?
The Metaverse: Are We There Yet?
 
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová10 Differences between Sales Cloud and CPQ, Blanka Doktorová
10 Differences between Sales Cloud and CPQ, Blanka Doktorová
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 

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