SlideShare a Scribd company logo
REFLECTION

NIKHIL BHARDWAJ
ROLL NO. 205110018
GOALS
1. What Is Reflection
2. Reflection Primitives in Java
3. Overview of Java’s reflection APIs
4. Advantages of Reflection
5. Problems with Reflection
6. Summary
WHAT IS REFLECTION?
When you look in a mirror:
- You can see your reflection
-   You can act on what you see, for example, straighten your tie


In computer programming:
- Reflection is infrastructure enabling a program can see and
manipulate itself
- It consists of metadata plus operations to manipulate the metadata


Meta means self-referential
- So metadata is data (information) about oneself
REFLECTION PRIMITIVES IN JAVA

Java stores metadata in classes
- Metadata for a class: java.lang.Class
- Metadata for a constructor: java.lang.reflect.Constructor
- Metadata for a field: java.lang.reflect.Field
- Metadata for a method: java.lang.reflect.Method


Two ways to access a Class object for a class:
Class c1 = Class.forName(“java.util.Properties”);
Object obj = ...;
Class c2 = obj.getClass();
Reflection in Java
OVERVIEW OF JAVA’S
REFLECTION APIS
ADVANTAGES OF REFLECTION
1. Signature-Based Polymorphism
Reflection provides an alternative where we can invoke methods
having same signature, from different classes not have a common
interface (which is required in interface based polymorphism).
2. Inspecting and Manipulating Classes
Writing direct code is based on depending on specific class
details (like interface, method or field names) known at compile-
time. If we need to write code that depends on class details
known only at runtime, then reflection comes handy.
3. Creating Adaptable and Flexible Solutions
Because of its dynamic nature, reflection is useful for creating
adaptable and flexible software that is often not possible
otherwise.
4. Writing Tools that Require Implementation Details
Many of the developer tools need access to internal or
implementation details of the code. For example, an intelligent
text editor can query a class using reflection and get details about
its implementation, which can be very helpful. When user types in
an object name and a „.‟ to access a member, the text editor can
pop with a list box with list of accessible members (obtained using
reflection) that the programmer can select from.
PROBLEMS WITH REFLECTION
1. Exposes implementation details.
One of the major objectives of object oriented programming is
information hiding, where only the relevant higher level details of
the class that the programmer needs to know are exposed to the
users, and low-level implementation details are hidden. Reflection
exposes such low-level implementation details of the class to the
program.
2. Performance
The code that is reflective is slow that the direct code that
performs the same functionality. Reflection can be order of times
(10 to 30 times) slower than the direct code.
3. Security
there are some security concerns in using reflection, particularly in
component and internet programming where the code might be
from un-trusted sources.
Reflection does provide some limited ways to circumvent many of
the checks that are usually statically checked. For example, in
Java, private members are accessible only to the class members.
If we attempt to access a private member from code outside the
class, it will result in an exception. But by setting the
suppressAccessChecks method (provided in
java.lang.reflect.ReflectPermission class), access checks can be
switched off for that class. So, you can access all members from
outside the class using reflection.
SUMMARY
Reflection is a powerful language feature.
With reflection, it is possible to do two basic types of operations
on code: inspection and manipulation. Reflection enables using
„signature based polymorphism‟ as an alternative to „interface
based polymorphism‟ in Java.
Reflection also facilitates creation of flexible and adaptable
frameworks and patterns, which is not usually possible with direct
code. Though there are many clear advantages in using
reflection, like any other language feature, reflection can be
misused.
REFERENCES

More Related Content

PPT
Reflection in java
PDF
Java Reflection Explained Simply
PPTX
Java reflection
PPT
Reflection
PPTX
Java Reflection @KonaTechAdda
PDF
Java reflection
PDF
Basics of reflection in java
PDF
Advanced Reflection in Java
Reflection in java
Java Reflection Explained Simply
Java reflection
Reflection
Java Reflection @KonaTechAdda
Java reflection
Basics of reflection in java
Advanced Reflection in Java

What's hot (20)

PPT
Java Reflection
PDF
Understanding And Using Reflection
PPT
Most Asked Java Interview Question and Answer
PPTX
Java interview questions 2
PPTX
Java interview questions 1
DOCX
Basic java important interview questions and answers to secure a job
PPTX
Object+oriented+programming+in+java
PDF
JAVA Object Oriented Programming (OOP)
PPTX
Java training in delhi
DOCX
Java questions for viva
PPTX
Basics of Java
PPT
Object Oriented Programming with Java
PDF
Java Interview Questions by NageswaraRao
PDF
20 most important java programming interview questions
PDF
201 core java interview questions oo ps interview questions - javatpoint
DOC
Java interview questions
PDF
Java scjp-part1
PPT
9781111530532 ppt ch10
PDF
Basic Java Programming
PPT
Java interface
Java Reflection
Understanding And Using Reflection
Most Asked Java Interview Question and Answer
Java interview questions 2
Java interview questions 1
Basic java important interview questions and answers to secure a job
Object+oriented+programming+in+java
JAVA Object Oriented Programming (OOP)
Java training in delhi
Java questions for viva
Basics of Java
Object Oriented Programming with Java
Java Interview Questions by NageswaraRao
20 most important java programming interview questions
201 core java interview questions oo ps interview questions - javatpoint
Java interview questions
Java scjp-part1
9781111530532 ppt ch10
Basic Java Programming
Java interface
Ad

Viewers also liked (20)

PPTX
Reflection on practice
PPTX
Java Exploit Analysis .
PDF
Factory method pattern (Virtual Constructor)
PPT
Easy rest service using PHP reflection api
PPTX
Java notes(OOP) jkuat IT esection
KEY
Using Chef for Automated Infrastructure in the Cloud
PDF
Docker Introduction
PPTX
What is this ‘reflection thing’ anyway
PDF
Ansible - Introduction
PDF
Chef Fundamentals Training Series Module 1: Overview of Chef
PDF
Overview of Chef - Fundamentals Webinar Series Part 1
PDF
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
PPT
LIGHT DISPERSION SPECTRUM-REFRACTION & REFLECTION
PPTX
Jenkins and Chef: Infrastructure CI and Automated Deployment
PPT
Ansible presentation
KEY
Infrastructure Automation with Chef
PDF
DevOps and Chef
PPTX
Automated Deployments with Ansible
PPTX
Ansible: How to Get More Sleep and Require Less Coffee
PPTX
Disciplines and Ideas in Social Sciences
Reflection on practice
Java Exploit Analysis .
Factory method pattern (Virtual Constructor)
Easy rest service using PHP reflection api
Java notes(OOP) jkuat IT esection
Using Chef for Automated Infrastructure in the Cloud
Docker Introduction
What is this ‘reflection thing’ anyway
Ansible - Introduction
Chef Fundamentals Training Series Module 1: Overview of Chef
Overview of Chef - Fundamentals Webinar Series Part 1
Introduction to Chef: Automate Your Infrastructure by Modeling It In Code
LIGHT DISPERSION SPECTRUM-REFRACTION & REFLECTION
Jenkins and Chef: Infrastructure CI and Automated Deployment
Ansible presentation
Infrastructure Automation with Chef
DevOps and Chef
Automated Deployments with Ansible
Ansible: How to Get More Sleep and Require Less Coffee
Disciplines and Ideas in Social Sciences
Ad

Similar to Reflection in Java (20)

PPTX
Untitled presentation about object oriented.pptx
PDF
Java Interview Questions
PPSX
Elements of Java Language
DOCX
Library Managemnet System
PPTX
Session 02 - Elements of Java Language
PDF
Object-Oriented Programming in Java.pdf
PPTX
OOP interview questions & answers.
PPT
Share Unit 1- Basic concept of object-oriented-programming.ppt
PPTX
Presentation5
PPTX
OOP in Java Presentation.pptx
PPT
Unit 1- Basic concept of object-oriented-programming.ppt
PDF
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
PDF
Java interview questions
PDF
Reflections the most important feature in java
DOCX
CAR SHOWROOM SYSTEM
PPTX
Features of Object Oriented Programming.pptx
PPTX
Php oop (1)
PDF
Android interview questions
PDF
Android interview questions
PDF
Java chapter 3 - OOPs concepts
Untitled presentation about object oriented.pptx
Java Interview Questions
Elements of Java Language
Library Managemnet System
Session 02 - Elements of Java Language
Object-Oriented Programming in Java.pdf
OOP interview questions & answers.
Share Unit 1- Basic concept of object-oriented-programming.ppt
Presentation5
OOP in Java Presentation.pptx
Unit 1- Basic concept of object-oriented-programming.ppt
JAVA VIVA QUESTIONS_CODERS LODGE.pdf
Java interview questions
Reflections the most important feature in java
CAR SHOWROOM SYSTEM
Features of Object Oriented Programming.pptx
Php oop (1)
Android interview questions
Android interview questions
Java chapter 3 - OOPs concepts

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Types and Its function , kingdom of life
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
RMMM.pdf make it easy to upload and study
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Microbial disease of the cardiovascular and lymphatic systems
Cell Types and Its function , kingdom of life
Renaissance Architecture: A Journey from Faith to Humanism
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
RMMM.pdf make it easy to upload and study
Final Presentation General Medicine 03-08-2024.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 4: Burden of Disease Tutorial Slides S2 2025
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Cell Structure & Organelles in detailed.
O7-L3 Supply Chain Operations - ICLT Program
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPH.pptx obstetrics and gynecology in nursing
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program

Reflection in Java

  • 2. GOALS 1. What Is Reflection 2. Reflection Primitives in Java 3. Overview of Java’s reflection APIs 4. Advantages of Reflection 5. Problems with Reflection 6. Summary
  • 3. WHAT IS REFLECTION? When you look in a mirror: - You can see your reflection - You can act on what you see, for example, straighten your tie In computer programming: - Reflection is infrastructure enabling a program can see and manipulate itself - It consists of metadata plus operations to manipulate the metadata Meta means self-referential - So metadata is data (information) about oneself
  • 4. REFLECTION PRIMITIVES IN JAVA Java stores metadata in classes - Metadata for a class: java.lang.Class - Metadata for a constructor: java.lang.reflect.Constructor - Metadata for a field: java.lang.reflect.Field - Metadata for a method: java.lang.reflect.Method Two ways to access a Class object for a class: Class c1 = Class.forName(“java.util.Properties”); Object obj = ...; Class c2 = obj.getClass();
  • 7. ADVANTAGES OF REFLECTION 1. Signature-Based Polymorphism Reflection provides an alternative where we can invoke methods having same signature, from different classes not have a common interface (which is required in interface based polymorphism). 2. Inspecting and Manipulating Classes Writing direct code is based on depending on specific class details (like interface, method or field names) known at compile- time. If we need to write code that depends on class details known only at runtime, then reflection comes handy. 3. Creating Adaptable and Flexible Solutions Because of its dynamic nature, reflection is useful for creating adaptable and flexible software that is often not possible otherwise.
  • 8. 4. Writing Tools that Require Implementation Details Many of the developer tools need access to internal or implementation details of the code. For example, an intelligent text editor can query a class using reflection and get details about its implementation, which can be very helpful. When user types in an object name and a „.‟ to access a member, the text editor can pop with a list box with list of accessible members (obtained using reflection) that the programmer can select from.
  • 9. PROBLEMS WITH REFLECTION 1. Exposes implementation details. One of the major objectives of object oriented programming is information hiding, where only the relevant higher level details of the class that the programmer needs to know are exposed to the users, and low-level implementation details are hidden. Reflection exposes such low-level implementation details of the class to the program. 2. Performance The code that is reflective is slow that the direct code that performs the same functionality. Reflection can be order of times (10 to 30 times) slower than the direct code.
  • 10. 3. Security there are some security concerns in using reflection, particularly in component and internet programming where the code might be from un-trusted sources. Reflection does provide some limited ways to circumvent many of the checks that are usually statically checked. For example, in Java, private members are accessible only to the class members. If we attempt to access a private member from code outside the class, it will result in an exception. But by setting the suppressAccessChecks method (provided in java.lang.reflect.ReflectPermission class), access checks can be switched off for that class. So, you can access all members from outside the class using reflection.
  • 11. SUMMARY Reflection is a powerful language feature. With reflection, it is possible to do two basic types of operations on code: inspection and manipulation. Reflection enables using „signature based polymorphism‟ as an alternative to „interface based polymorphism‟ in Java. Reflection also facilitates creation of flexible and adaptable frameworks and patterns, which is not usually possible with direct code. Though there are many clear advantages in using reflection, like any other language feature, reflection can be misused.