Reflection Interview Questions :-


  What is reflection ?

It is the process of examining / modifying the runtime behaviour of an object at runtime.Using java reflection we can inspect a class, interface, enum, get their structure, methods and fields information at runtime even though class is not accessible at compile time. We can also use reflection to instantiate an object, invoke it's methods, change field values.


  Why is Reflection slower ?

Because it has to inspect the metadata in the bytecode instead of just using pre compiled addresses and constants.


  Is It Good to use Reflection in an application ? Why ?

no, It's like challenging the design of application.


  What is Java Reflection API? Why it's so important to have?

Java Reflection API provides ability to inspect and modify the runtime behavior of java application. We can inspect a java class, interface, enum and get their methods and field details. Reflection API is an advanced topic and we should avoid it in normal programming. Reflection API usage can break the design pattern such as Singleton pattern by invoking the private constructor i.e violating the rules of access modifiers.

Even though we don't use Reflection API in normal programming, it's very important to have. We can't have any frameworks such as Spring, Hibernate or servers such as Tomcat, JBoss without Reflection API. They invoke the appropriate methods and instantiate classes through reflection API and use it a lot for other processing.


  Can a private variable or method of a class can be accessed?

Yes its possible using reflection.


  What are advantages of Using Reflection?

1. Extensibility Features :- An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.

2. Debugging and testing tools :- Debuggers use the property of reflection to examine private members on classes.


  What are the Drawbacks of Java Reflection?

1.Performance Overhead :- Reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

2. Exposure of Internals :- Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.


  How to Retrieve fields with data type Reflection Java

getDeclaredFields() returns all fields and getType() returns field's data type.


  How can you construct an object of a given class using reflection?

You can use the newInstance() method defined on the Class object. This will only work if the class has a public constructor without parameters. Otherwise you get an exception.


  How can you get the class object from a class name?

Use the Class.forName(className) method. Fully qualified classname may be required if the class is not in the same package as the caller.


  Using reflection, how can you tell if a member is public or private?

All the classes that implement the Member interface have a method called getModifiers(). This gives an integer value. There is a class called Modifier that exposes a number of static methods to interpret this integer.

Here is an example :-

Member mem;

//.

int mod = mem.getModifiers();

Modifier.isPrivate(mod);


  What are the main Java reflection classes?

1. Class (java.lang.class) :-

Instances of the class Class represent classes and interfaces in a running Java application, every object is represented by a Class object

2. Package java.lang.reflect :-

   (a). Member (java.lang.reflect.Member) :- An Interface that reflects identifying information about a single member (a field or a method) or a constructor.

   (b). Method (java.lang.reflect.Method) :-    . Implements Member Interface.

   . Provides information about, and access to, a single method on a class or interface.

   . Represents instance methods and class methods (static).

3. Field (java.lang.reflect.Field) :-

   (a). Implements Member Interface

   (b). provides information about, and dynamic access to, a single field (also for static fields).

   (c). Provides access and modification ( set, get ) methods.

4. Constructor (java.lang.reflect.Constructor) :-

   (a). Provides information about, and access to, a single constructor for a class.

5. Package (java.lang.Package) :-

   (a). Package objects contain version information about the implementation and specification of a Java package


 


PHP

PHP is a server-side scripting language designed primarily for web development but is also used as a general-purpose programming language..

Read more