Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə33/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   ...   29   30   31   32   33   34   35   36   ...   47
Java Interview Guide

Questions
What is the command pattern? When is it useful?
What is the observer pattern? When is it useful?
What is the strategy pattern? When is it useful?
What is the visitor pattern? When is it useful?
What is the null object pattern? When is it useful?


Reflection
Reflection allows a program to manipulate an object at runtime without knowing
about the object at compile time. For example, if you wanted to debug an object
that didn’t override the toString() method, you could use reflection
to inspect every field. Reflection is possible due to a collection of classes that
represent programming constructs such as Class, Field, and Method.


Class
A Class is a special object constructed by the JVM that represents classes and
interfaces in an application. If you consider a class file as the blueprint for
an object, you can consider the Class class as a blueprint for blueprints.
A Class instance can be retrieved from an object by calling the getClass()
method, or it can be looked up by its fully qualified name with the
Class#forName()
method.
A Class provides type introspection for an object. Type introspection allows
a program to examine the properties of an object at runtime. For example,
a Class object contains introspective methods such as getName(),
getPackage()
, getDeclaredFields(),
and getDeclaredMethods(). A Class can also create an object with the
newInstance()
method. This is widely used in frameworks that need
to instantiate objects defined in XML files.


Field
A Field provides access to a single field in a class or an interface. A Field
contains introspective methods that allow you to get a value from a field, as well
as reflective methods that allow you to a set a value into a field. However, a flag
must be set on the Field object via the setAccessible() method to allow
access to fields with limited visibility.


Method
A Method object provides access to a single method in a class or interface.
A Method contains introspective methods that provide information about the
method declaration, as well as reflective methods that allow you to invoke the
method. However, a flag must be set on the Method object via the
setAccessible()
method to allow access to methods with limited visibility.


Pros and Cons
Reflection is a powerful tool that plays a crucial role in countless extensions
to the Java library, such as test frameworks, IoC containers, ORM mappers,
profilers, and debuggers. On the other hand, Java is a strongly-typed language
that provides robust compile-time checking, and much of that protection is lost
with reflection. Reflection also breaks encapsulation by allowing access to fields
and methods outside of their intended scope. Finally, reflection has slower
performance than non-reflective code due to the additional overhead
on the JVM.


Questions
What is reflection?
What is type introspection?
What is a Class object?
What is a Field object?
What is a Method object?
What are some of the pros and cons of reflection?



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   ...   29   30   31   32   33   34   35   36   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin