What is reflection and how is it used

๐Ÿ’ก Concept: Java Reflection

Reflection in Java is a powerful API that allows a program to inspect and manipulate classes, methods, and fields at runtime.

๐Ÿ“˜ Quick Intro

Using reflection, you can examine class metadata, invoke methods dynamically, and access private members, facilitating dynamic behavior.

๐Ÿง  Analogy

Reflection is like having X-ray vision to see inside a compiled class and interact with its components dynamically.

๐Ÿ”ง Technical Explanation

  • Reflection API includes classes like Class, Method, Field, and Constructor.
  • Allows dynamic loading and inspection of classes.
  • Can invoke methods and access fields at runtime.
  • Used for frameworks, serialization, and debugging tools.
  • Can bypass access modifiers using setAccessible(true).

๐ŸŽฏ Use Cases

  • โœ… Developing frameworks and libraries that require dynamic behavior.
  • โœ… Inspecting classes during runtime for debugging or testing.
  • โœ… Serialization/deserialization and ORM frameworks.

๐Ÿ’ป Example: Using Reflection to Invoke a Method


import java.lang.reflect.Method;

public class ReflectionDemo {
    public void sayHello() {
        System.out.println("Hello from Reflection!");
    }

    public static void main(String[] args) throws Exception {
        ReflectionDemo obj = new ReflectionDemo();
        Method method = obj.getClass().getMethod("sayHello");
        method.invoke(obj);
    }
}

โ“ Interview Q&A

Q1: What is reflection in Java?
A: API for inspecting and manipulating classes at runtime.

Q2: Which classes are used in reflection?
A: Class, Method, Field, Constructor.

Q3: Can reflection access private fields?
A: Yes, with setAccessible(true).

Q4: What are common uses of reflection?
A: Frameworks, serialization, debugging.

Q5: Is reflection performance costly?
A: Yes, compared to direct calls.

Q6: Can reflection break encapsulation?
A: Potentially, if misused.

Q7: Is reflection type-safe?
A: No, it bypasses compile-time checks.

Q8: How to get class object for reflection?
A: Using Class.forName() or obj.getClass().

Q9: Can reflection create objects?
A: Yes, via Constructor class.

Q10: What are the risks of reflection?
A: Security and performance impacts.

๐Ÿ“ MCQs

Q1. What is reflection in Java?

  • Compile time checking
  • API for runtime inspection
  • Memory management
  • Thread management

Q2. Which classes are used in reflection?

  • Thread, Process
  • Class, Method, Field, Constructor
  • Scanner, File
  • Socket, Stream

Q3. Can reflection access private fields?

  • No
  • Yes
  • Sometimes
  • Never

Q4. What are common uses of reflection?

  • I/O
  • Frameworks and debugging
  • Networking
  • Database

Q5. Is reflection performance costly?

  • No
  • Yes
  • Depends
  • Unknown

Q6. Can reflection break encapsulation?

  • No
  • Yes
  • Maybe
  • Never

Q7. Is reflection type-safe?

  • Yes
  • No
  • Sometimes
  • Never

Q8. How to get class object for reflection?

  • new Class()
  • Class.forName()
  • Class.get()
  • Class.load()

Q9. Can reflection create objects?

  • No
  • Yes
  • Sometimes
  • Never

Q10. What are risks of reflection?

  • None
  • Security and performance impacts
  • Only security
  • Only performance

๐Ÿ’ก Bonus Insight

Reflection is essential for building dynamic and flexible Java frameworks but should be used carefully due to security and performance considerations.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About Java โ˜•

What is Java and its key features ๐Ÿ‘‰ Explained
Explain the Java Virtual Machine (JVM) ๐Ÿ‘‰ Explained
Difference between JDK, JRE, and JVM ๐Ÿ‘‰ Explained
What are Javaโ€™s main data types ๐Ÿ‘‰ Explained
Explain the concept of Object-Oriented Programming in Java ๐Ÿ‘‰ Explained
What is the difference between a class and an object ๐Ÿ‘‰ Explained
Explain encapsulation with an example ๐Ÿ‘‰ Explained
What is inheritance in Java and its types ๐Ÿ‘‰ Explained
Define polymorphism in Java with examples ๐Ÿ‘‰ Explained
What is abstraction in Java ๐Ÿ‘‰ Explained
Difference between abstract class and interface in Java ๐Ÿ‘‰ Explained
Explain method overloading and method overriding in Java ๐Ÿ‘‰ Explained
What are constructors in Java ๐Ÿ‘‰ Explained
What is the use of the static keyword in Java ๐Ÿ‘‰ Explained
Explain the difference between final, finally, and finalize in Java ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup