It is the process of examining / modifying the runtime behaviour of an object at runtime. Reflection is not good to use in an application because It’s like challenging the design of application.
Here are some commonly asked Java Reflection API interview questions and answers to prepare you for your interview.
Here is the list of questions, we will discuss some of the most asked interview questions, like as : Java Reflection API Interview Questions and Answers? or what is java reflection? or what is unsafe reflection in java or reflection in java example or What is the use of reflection?
1). What is Java Reflection api? (Most Imp Question)
Reflection in Java is a way to access details about a class while the program is running. This information can be used to examine and alter how a class behaves.
Note: Suppose a class fields are private then you can’t access outside the class but by using the Reflection you can change the
value of that private variable.
2. What are the main uses or purpose of Java Reflection API?
The Java Reflection API is mainly used for two purposes:
Java Reflection lets Java programs check and change properties of any Java object while the program is running.
It also helps Java programs to grow and add new features dynamically.”
Some of the things which we can do with the help of Reflection are mentioned below:
·
We can fetch all the information of the Class. such as the name, package, type ,super class etc
· To get the list of all members and methods of theClass
· We can even access private members and methods ofthe Class.
· To get the list of constructors.
· To create an instance of the Class without using new keyword
· To call the methods of the Class.
· Getter and Setter Field Values
·
3). What is a Class Object? (Most Imp Question)
To start with Reflection, we need a Class object. All types in Java including the primitive types (int, long, float etc.) including arrays have an associated Class object.
Different Ways to get Class Object:
To create Reflection so that you need create object of that java.lang.Class or className class . This class contains different methods for Reflection.
i). Using name of the class: If you know the name of the class at compile time you can obtain a Class object like this:
Class<ReflectionClassExample> clazz = ReflectionClassExample.class;
II). Using object of the class: If you have the object of the class, you can obtain a Class object like this:
ReflectionClassExample reflectionClassExample = new ReflectionClassExample();
Class<ReflectionClassExample> clazz1 = (Class<ReflectionClassExample>) reflectionClassExample.getClass();
III). Using Class.forName(): When using the Class.forName() method you must supply the fully qualified class name. The Class.forName() method may throw a ClassNotFoundException if the class cannot be found on the classpath at runtime.
Class<ReflectionClassExample> clazz2 = (Class<ReflectionClassExample>) Class.forName(“com.java.
4).Can you give me some examples of real-world applications that use Java Reflection API? (Imp Question)
Java Reflection API is used in a number of different ways in the real world.
o IDE (Integrated Development Environment) Like as: Eclipse, MyEclipse, NetBeans,Spring tool suite extra.
o Debugger
o Test Tools etc.
5). What is the security risk of object reflection? or How to Ensure Java Application Security? or What is a reflection attack?
(Most Imp Question)
Keep Secrets Hidden: Don’t let just anyone access your important parts (like classes and methods). Use words like “private” and “protected” to limit access.
Use a Security Guard (Security Manager): Have a guardian in place to decide who gets permission to do what. Set rules to control what your application is allowed to do.
Lock the Doors (Seal Classes): Make sure nobody can mess with your classes. Use the “final” keyword to stop them from being changed or extended.
Mark What’s Off-Limits (Security Annotations): Put special marks on things that shouldn’t be touched. Check for these marks and stop anyone trying to mess with them.
Be Careful with Special Tools (Class Loaders): If you have special tools to load classes, be careful. Only trust tools from reliable sources.
Regular Security Checks (Security Audits):Keep an eye out for any potential problems. Regularly look through your code to make sure there are no weak spots.
Check What Comes In (Input Validation): Before using information from outside sources, make sure it’s safe. Check and clean up any data that comes from users or other places.
Hide Your Secrets (Code Obfuscation): Make it hard for people to understand your code. Use tools that mix things up so that it’s not easy to figure out.
Watch What’s Happening (Runtime Monitoring): Keep an eye on your application while it’s running. If something strange happens, be ready to stop it.
Keep Everything Updated (Update Libraries): Make sure your tools and helpers are up-to-date. This way, you get the latest fixes and improvements that keep your application safe.
So, it’s like protecting your house. Lock the doors, have a guard, and be careful who you let in. Always stay on top of things to keep your application safe!