SlideShare a Scribd company logo
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
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
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
Implementation of the Method clone:
Simple Case
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-5
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
Implementation of the Method clone: Harder
Case
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)
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-9
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
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
Class with an Inner Class
Class with an Inner Class
Class with an Inner Class
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

PPTX
Javasession8
Rajeev Kumar
 
PPT
Inner classes or nested classes in Java Program
ssuser5d6130
 
PPTX
Nested classes in java
ChiradipBhattacharya
 
PPTX
Java Nested class Concept
jagriti srivastava
 
PPT
L5 classes, objects, nested and inner class
teach4uin
 
PPT
encapsulation and abstraction
ALIZAPARVIN
 
PPTX
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
PPTX
Module 4_Ch2 - Introduction to Object Oriented Programming.pptx
jhesorleylaid2
 
Javasession8
Rajeev Kumar
 
Inner classes or nested classes in Java Program
ssuser5d6130
 
Nested classes in java
ChiradipBhattacharya
 
Java Nested class Concept
jagriti srivastava
 
L5 classes, objects, nested and inner class
teach4uin
 
encapsulation and abstraction
ALIZAPARVIN
 
OCP Java (OCPJP) 8 Exam Quick Reference Card
Hari kiran G
 
Module 4_Ch2 - Introduction to Object Oriented Programming.pptx
jhesorleylaid2
 

Similar to Lecture09.ppt (20)

PPTX
object oriented programming unit two ppt
isiagnel2
 
PPTX
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
PPTX
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
PPT
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
PDF
Classes and Object Concept Object Oriented Programming in Java
gedeios
 
PPTX
Java chapter 5
Abdii Rashid
 
PPTX
Introduction to OOP with java
Sujit Kumar
 
PDF
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
PPTX
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
PPTX
class and object in c++.pptx
Adarsh College, Hingoli
 
PPT
Unit 7 inheritance
atcnerd
 
PPTX
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
PPT
Chap02
Jotham Gadot
 
PDF
ITFT-Classes and object in java
Atul Sehdev
 
PDF
Inner Classes in Java
Dallington Asingwire
 
PPTX
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
PDF
Classes in Java great learning.pdf
SHASHIKANT346021
 
PPT
Object and Classes in Java
backdoor
 
PPTX
Module 4_Ch4 - Polymorphism and Inheritance.pptx
jhesorleylaid2
 
PPTX
OOPS – General Understanding in .NET
Sabith Byari
 
object oriented programming unit two ppt
isiagnel2
 
Object oriented programming CLASSES-AND-OBJECTS.pptx
DaveEstonilo
 
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
A1771937735_21789_14_2018__16_ Nested Classes.ppt
RithwikRanjan
 
Classes and Object Concept Object Oriented Programming in Java
gedeios
 
Java chapter 5
Abdii Rashid
 
Introduction to OOP with java
Sujit Kumar
 
04_-_Inheritance_Polymorphism_and_Interfaces.pdf
markbrianBautista
 
Unit II Inheritance ,Interface and Packages.pptx
pranalisonawane8600
 
class and object in c++.pptx
Adarsh College, Hingoli
 
Unit 7 inheritance
atcnerd
 
Object Oriented Programming Tutorial.pptx
ethiouniverse
 
Chap02
Jotham Gadot
 
ITFT-Classes and object in java
Atul Sehdev
 
Inner Classes in Java
Dallington Asingwire
 
Quick Interview Preparation for C# All Concepts
Karmanjay Verma
 
Classes in Java great learning.pdf
SHASHIKANT346021
 
Object and Classes in Java
backdoor
 
Module 4_Ch4 - Polymorphism and Inheritance.pptx
jhesorleylaid2
 
OOPS – General Understanding in .NET
Sabith Byari
 

Recently uploaded (20)

PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Tips Management in Odoo 18 POS - Odoo Slides
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Antianginal agents, Definition, Classification, MOA.pdf
Prerana Jadhav
 
CDH. pptx
AneetaSharma15
 
Python-Application-in-Drug-Design by R D Jawarkar.pptx
Rahul Jawarkar
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Basics and rules of probability with real-life uses
ravatkaran694
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 

Lecture09.ppt

  • 1. 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
  • 2. 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
  • 3. 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
  • 4. Implementation of the Method clone: Simple Case
  • 5. 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-5
  • 6. 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
  • 7. Implementation of the Method clone: Harder Case
  • 8. 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)
  • 9. 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-9
  • 10. 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
  • 11. 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
  • 12. Class with an Inner Class
  • 13. Class with an Inner Class
  • 14. Class with an Inner Class
  • 15. 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