SlideShare a Scribd company logo
Slides prepared by Rose Williams, Binghamton University
Chapter 13
Interfaces and
Inner Classes
© 2006 Pearson Addison-Wesley. All rights reserved 13-2
The Serializable Interface
• An extreme but commonly used
example of an interface is the
Serializable interface
– It has no method headings and no
defined constants: It is completely empty
– It is used merely as a type tag that
indicates to the system that it may
implement file I/O in a particular way
© 2006 Pearson Addison-Wesley. All rights reserved 13-3
The Cloneable Interface
• The Cloneable interface is another
unusual example of a Java interface
– It does not contain method headings or
defined constants
– It is used to indicate how the method
clone (inherited from the Object class)
should be used and redefined
© 2006 Pearson Addison-Wesley. All rights reserved 13-4
The Cloneable Interface
• The method Object.clone() does a bit-
by-bit copy of the object's data in storage
• If the data is all primitive type data or data of
immutable class types (such as String),
then this is adequate
– This is the simple case
• The following is an example of a simple
class that has no instance variables of a
mutable class type, and no specified base
class
– So the base class is Object
© 2006 Pearson Addison-Wesley. All rights reserved 13-5
Implementation of the Method clone:
Simple Case
© 2006 Pearson Addison-Wesley. All rights reserved 13-6
The Cloneable Interface
• If the data in the object to be cloned includes
instance variables whose type is a mutable class,
then the simple implementation of clone would
cause a privacy leak
• When implementing the Cloneable interface for a
class like this:
– First invoke the clone method of the base class Object
(or whatever the base class is)
– Then reset the values of any new instance variables
whose types are mutable class types
– This is done by making copies of the instance variables
by invoking their clone methods
© 2006 Pearson Addison-Wesley. All rights reserved 13-7
The Cloneable Interface
• Note that this will work properly only if
the Cloneable interface is
implemented properly for the classes
to which the instance variables belong
– And for the classes to which any of the
instance variables of the above classes
belong, and so on and so forth
• The following shows an example
© 2006 Pearson Addison-Wesley. All rights reserved 13-8
Implementation of the Method clone:
Harder Case
© 2006 Pearson Addison-Wesley. All rights reserved 13-9
Simple Uses of Inner Classes
• Inner (or nested) classes are classes defined within
other classes
– The class that includes the inner class is called the outer
class
• There are four categories of inner classes in Java:
1.Inner classes (non-static)
2.Static inner classes
3.Local classes (defined inside a block of Java
code)
4.Anonymous classes (defined inside a block of
Java code)
© 2006 Pearson Addison-Wesley. All rights reserved 13-10
Simple Uses of Inner Classes
• An inner class definition is a member of the
outer class in the same way that the
instance variables and methods of the outer
class are members
– An inner class is local to the outer class
definition
– The name of an inner class may be reused for
something else outside the outer class definition
– If the inner class is private, then the inner class
cannot be accessed by name outside the
definition of the outer class
© 2006 Pearson Addison-Wesley. All rights reserved 13-11
Simple Uses of Inner Classes
• There are two main advantages to inner
classes
– They can make the outer class more self-
contained since they are defined inside a class
– Both of their methods have access to each
other's private methods and instance variables
• Using an inner class as a helping class is
one of the most useful applications of inner
classes
– If used as a helping class, an inner class should
be marked private
© 2006 Pearson Addison-Wesley. All rights reserved 13-12
Tip: Inner and Outer Classes Have
Access to Each Other's Private Members
• Within the definition of a method of an inner class:
– It is legal to reference a private instance variable of the
outer class
– It is legal to invoke a private method of the outer class
• Within the definition of a method of the outer class
– It is legal to reference a private instance variable of the
inner class on an object of the inner class
– It is legal to invoke a (nonstatic) method of the inner class
as long as an object of the inner class is used as a calling
object
• Within the definition of the inner or outer classes,
the modifiers public and private are equivalent
© 2006 Pearson Addison-Wesley. All rights reserved 13-13
Class with an Inner Class
© 2006 Pearson Addison-Wesley. All rights reserved 13-14
Class with an Inner Class
© 2006 Pearson Addison-Wesley. All rights reserved 13-15
Class with an Inner Class
© 2006 Pearson Addison-Wesley. All rights reserved 13-16
The .class File for an Inner Class
• Compiling any class in Java produces
a .class file named ClassName.class
• Compiling a class with one (or more) inner
classes causes both (or more) classes to be
compiled, and produces two (or more) .class
files
– Such as ClassName.class and
ClassName$InnerClassName.class

More Related Content

PPT
Lecture09.ppt
hemanth248901
 
PPTX
Inner Classes & Multi Threading in JAVA
Tech_MX
 
PPTX
Javasession8
Rajeev Kumar
 
PDF
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
bca23189c
 
PPTX
UNIT_-II_2021R.pptx
RDeepa9
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PPT
Java oops PPT
kishu0005
 
PPT
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 
Lecture09.ppt
hemanth248901
 
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Javasession8
Rajeev Kumar
 
ch4 foohggggvvbbhhhhhhhhhbbbbbbbbbbbbp.pdf
bca23189c
 
UNIT_-II_2021R.pptx
RDeepa9
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
Java oops PPT
kishu0005
 
025466482929 -OOP with Java Development Kit.ppt
DakshinaPahan
 

Similar to Inner classes or nested classes in Java Program (20)

PPT
02-OOP with Java.ppt
EmanAsem4
 
PPT
Inner classes9 cm604.28
myrajendra
 
PDF
Object oriented programming abstraction and interface
ebtehally95
 
PDF
Java Inner Classes
Jussi Pohjolainen
 
PPTX
Core java oop
Parth Shah
 
PPTX
javaimplementation
FaRaz Ahmad
 
PPTX
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
belgiumsckgr
 
PDF
Inner Classes in Java
Dallington Asingwire
 
PPTX
Nested class
Daman Toor
 
PDF
Classes in Java great learning.pdf
SHASHIKANT346021
 
PPT
Chapter08 - Inheritance.ppt
Patrick Okot
 
PPT
Java inheritance
Arati Gadgil
 
PPT
Object and class
mohit tripathi
 
PPTX
Interface
vvpadhu
 
PPT
Inner Classes
parag
 
PPT
Object and Classes in Java
backdoor
 
PPT
Chap11
Terry Yoast
 
PDF
Classes And Methods
adil raja
 
PPT
9781439035665 ppt ch10
Terry Yoast
 
PPT
L5 classes, objects, nested and inner class
teach4uin
 
02-OOP with Java.ppt
EmanAsem4
 
Inner classes9 cm604.28
myrajendra
 
Object oriented programming abstraction and interface
ebtehally95
 
Java Inner Classes
Jussi Pohjolainen
 
Core java oop
Parth Shah
 
javaimplementation
FaRaz Ahmad
 
WINSEMFRE2024-25_CSE2005_ETH_AP2024255000715_2025-03-18_Reference-Material-I....
belgiumsckgr
 
Inner Classes in Java
Dallington Asingwire
 
Nested class
Daman Toor
 
Classes in Java great learning.pdf
SHASHIKANT346021
 
Chapter08 - Inheritance.ppt
Patrick Okot
 
Java inheritance
Arati Gadgil
 
Object and class
mohit tripathi
 
Interface
vvpadhu
 
Inner Classes
parag
 
Object and Classes in Java
backdoor
 
Chap11
Terry Yoast
 
Classes And Methods
adil raja
 
9781439035665 ppt ch10
Terry Yoast
 
L5 classes, objects, nested and inner class
teach4uin
 
Ad

More from ssuser5d6130 (7)

PPT
packages in java programming language ppt
ssuser5d6130
 
PPT
Arrays in java programming language slides
ssuser5d6130
 
PPT
Arrays in Java Programming Language slides
ssuser5d6130
 
PPT
Arrays in Java Programming Language slides
ssuser5d6130
 
PPTX
Lecture_5_Method_overloading_Final.pptx in Java
ssuser5d6130
 
PPT
Control statements in Java Programming Language
ssuser5d6130
 
PPTX
API-led.pptx
ssuser5d6130
 
packages in java programming language ppt
ssuser5d6130
 
Arrays in java programming language slides
ssuser5d6130
 
Arrays in Java Programming Language slides
ssuser5d6130
 
Arrays in Java Programming Language slides
ssuser5d6130
 
Lecture_5_Method_overloading_Final.pptx in Java
ssuser5d6130
 
Control statements in Java Programming Language
ssuser5d6130
 
API-led.pptx
ssuser5d6130
 
Ad

Recently uploaded (20)

PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PPTX
Understanding operators in c language.pptx
auteharshil95
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PDF
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
PDF
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTs-The Rise of Empiresghhhhhhhh (1).pptx
academysrusti114
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Understanding operators in c language.pptx
auteharshil95
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Review of Related Literature & Studies.pdf
Thelma Villaflores
 
2.Reshaping-Indias-Political-Map.ppt/pdf/8th class social science Exploring S...
Sandeep Swamy
 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 

Inner classes or nested classes in Java Program

  • 1. Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes
  • 2. © 2006 Pearson Addison-Wesley. All rights reserved 13-2 The Serializable Interface • An extreme but commonly used example of an interface is the Serializable interface – It has no method headings and no defined constants: It is completely empty – It is used merely as a type tag that indicates to the system that it may implement file I/O in a particular way
  • 3. © 2006 Pearson Addison-Wesley. All rights reserved 13-3 The Cloneable Interface • The Cloneable interface is another unusual example of a Java interface – It does not contain method headings or defined constants – It is used to indicate how the method clone (inherited from the Object class) should be used and redefined
  • 4. © 2006 Pearson Addison-Wesley. All rights reserved 13-4 The Cloneable Interface • The method Object.clone() does a bit- by-bit copy of the object's data in storage • If the data is all primitive type data or data of immutable class types (such as String), then this is adequate – This is the simple case • The following is an example of a simple class that has no instance variables of a mutable class type, and no specified base class – So the base class is Object
  • 5. © 2006 Pearson Addison-Wesley. All rights reserved 13-5 Implementation of the Method clone: Simple Case
  • 6. © 2006 Pearson Addison-Wesley. All rights reserved 13-6 The Cloneable Interface • If the data in the object to be cloned includes instance variables whose type is a mutable class, then the simple implementation of clone would cause a privacy leak • When implementing the Cloneable interface for a class like this: – First invoke the clone method of the base class Object (or whatever the base class is) – Then reset the values of any new instance variables whose types are mutable class types – This is done by making copies of the instance variables by invoking their clone methods
  • 7. © 2006 Pearson Addison-Wesley. All rights reserved 13-7 The Cloneable Interface • Note that this will work properly only if the Cloneable interface is implemented properly for the classes to which the instance variables belong – And for the classes to which any of the instance variables of the above classes belong, and so on and so forth • The following shows an example
  • 8. © 2006 Pearson Addison-Wesley. All rights reserved 13-8 Implementation of the Method clone: Harder Case
  • 9. © 2006 Pearson Addison-Wesley. All rights reserved 13-9 Simple Uses of Inner Classes • Inner (or nested) classes are classes defined within other classes – The class that includes the inner class is called the outer class • There are four categories of inner classes in Java: 1.Inner classes (non-static) 2.Static inner classes 3.Local classes (defined inside a block of Java code) 4.Anonymous classes (defined inside a block of Java code)
  • 10. © 2006 Pearson Addison-Wesley. All rights reserved 13-10 Simple Uses of Inner Classes • An inner class definition is a member of the outer class in the same way that the instance variables and methods of the outer class are members – An inner class is local to the outer class definition – The name of an inner class may be reused for something else outside the outer class definition – If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class
  • 11. © 2006 Pearson Addison-Wesley. All rights reserved 13-11 Simple Uses of Inner Classes • There are two main advantages to inner classes – They can make the outer class more self- contained since they are defined inside a class – Both of their methods have access to each other's private methods and instance variables • Using an inner class as a helping class is one of the most useful applications of inner classes – If used as a helping class, an inner class should be marked private
  • 12. © 2006 Pearson Addison-Wesley. All rights reserved 13-12 Tip: Inner and Outer Classes Have Access to Each Other's Private Members • Within the definition of a method of an inner class: – It is legal to reference a private instance variable of the outer class – It is legal to invoke a private method of the outer class • Within the definition of a method of the outer class – It is legal to reference a private instance variable of the inner class on an object of the inner class – It is legal to invoke a (nonstatic) method of the inner class as long as an object of the inner class is used as a calling object • Within the definition of the inner or outer classes, the modifiers public and private are equivalent
  • 13. © 2006 Pearson Addison-Wesley. All rights reserved 13-13 Class with an Inner Class
  • 14. © 2006 Pearson Addison-Wesley. All rights reserved 13-14 Class with an Inner Class
  • 15. © 2006 Pearson Addison-Wesley. All rights reserved 13-15 Class with an Inner Class
  • 16. © 2006 Pearson Addison-Wesley. All rights reserved 13-16 The .class File for an Inner Class • Compiling any class in Java produces a .class file named ClassName.class • Compiling a class with one (or more) inner classes causes both (or more) classes to be compiled, and produces two (or more) .class files – Such as ClassName.class and ClassName$InnerClassName.class