Java - Reflection - Interview Questions

What do you understand by Reflection in Java programming language?

 

Java Reflection is an API provided in Java programming language that makes it possible to inspect classes, methods, fields etc. at runtime; without knowing their names at compile time. In addition to inspecting classes and its members, it is also possible to instantiate objects, call methods and set field values using reflection.

Where is Java Reflection commonly used?

 

Java Reflection API is commonly used in the development of developer tools.

Visual Development Environments:Visual development environments use Java reflection to make the development process easier and more efficient by prompting the correct types and values to the developer

Class Browsers:Class browsers inspect class and its members

Debuggers and Testing Tools:

What are the disadvantages of Reflection?

 

Performance overhead: Reflection works by dynamically resolving and inspecting classes and its members. with this flexibility comes its disadvantage - certain java virtual machine optimizations cannot be performed when types are resolved dynamically leading to slower performance as compared to normal class and method operations. When an operation can be performed non-reflective as well as reflective operation, always prefer the non-reflective operation. In performance sensitive applications, reflective operations must be avoided in loops and frequently called sections of code.

Security Restrictions: There are certain security impacts to using Reflection. Reflection needs a runtime permission which may not be available when running under a security manager, such as in an Applet.

Exposure of Internals: Java reflection enables us to perform certain operations which are illegal in non-reflective operations. For example - We can access the private members of a class which is illegal with non-reflective operations. This leads to dysfunctional and unportable code, and breaks the object oriented principle of abstraction and containment.

What is a Class object. How do you get a Class object via reflection?

 

Every type; including reference types, primitive types (int, char etc.) and arrays have an associated java.lang.Class object. To perform reflection operation on a class, we have to first get its associated class object. Following are the different ways to get a Class object, depending on what the code has access to - object, type, class or name of class.

Class.forName(): If the code has access to a fully-qualified class name you can use 'Class.forName()' to get the class object of the fully-qualified class name.

Object.getClass(): If the code has access to an instance object you can use 'Object.getClass()' syntax to get the class object for the object instance.

Type.class:If the code has access to the type of class, you can use 'Type.class' syntax to get the class object for the type.

How do you access the package of a class?

 

The package of a class can be accessed by calling the method getPackage() on the class object.

Class myClass = Class.forName('java.lang.String');
Package package = myClass.getPackage();
Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you access the interfaces implemented by a class?

 

The interfaces of a class can be accessed by calling the method getInterfaces() on the class object...

*** See complete answer and code snippet in the Java Interview Guide.

How do you access the parent class of a class?

 

The parent or super class of a class can be accessed by calling the method getSuperClass() on the class object...

*** See complete answer and code snippet in the Java Interview Guide.

How do you retrieve class access modifiers reflection?

 

Class access modifiers are the access modifiers such as public, private etc. that a class is declared with. Class modifiers can be accessed calling the method getModifiers() on the class object...

*** See complete answer and code snippet in the Java Interview Guide.

How do you access constructors defined in a class using reflection?

 

Constructors of a class can be accessed by calling the method getConstructors() on the class object...

*** See complete answer and code snippet in the Java Interview Guide.

How do you access fields defined in a class using reflection?

 

Fields of a class can be accessed by calling the method getFields() on the class object...

*** See complete answer and code snippet in the Java Interview Guide.

Java Interview Guide has over 250 REAL questions from REAL interviews. Get the guide for $15.00 only.
 
BUY EBOOK
 

How do you access annotations defined in a class using reflection?

 

Annotations of a class can be accessed by calling the method getAnnotations() on the class object...

*** See complete answer and code snippet in the Java Interview Guide.

 
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
Java Interview Guide

$15.00

BUY EBOOK
  SSL Secure Payment
Java Interview Quesiuons - Secure Payment
 

Java - Interview Questions

Java - Object Oriented ProgrammingJava - Objects & ClassesJava - Data TypesJava - VariablesJava - StringsJava - ArraysJava - CollectionsJava - ReflectionJava - Lambda ExpressionsJava - StreamsJava - GenericsJava - ExceptionsJava - IOJava - ThreadsJava - ConcurrencyJava - JDBCJava - NetworkingJava - SecurityJava - JVM InternalsJava - PerformanceJava - New in Java 8Java - New in Java 9Java - New in Java 10Java - New in Java 11
 
MASTER Java  

Top ranked courses to help you master Java skills.
Java Programming Masterclass

iconicon

Offered By - Tim Buchalka
Platform - Udemy
Rating - * * * * *
Students Enrolled - 575,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example