Then we define a Student class implementing the person interface. In the above programming example, an interface Person is defined with a lone method ‘display ()’. ("Superclass of Student Class: " + superClass.getName()) declare class Student that implements Person The below example demonstrates the getSuperClass() method. Similarly, it provides a method getModifier() that returns the access modifier of the class. provides a method “getSuperClass()” that is used to get the superclass of any class. Then using this object we called the “getClass ()” method to get an object obj_test of. In the first line, we created an object of Test class. GetClass() method uses object of a class to get the object.įor example, consider the following piece of code: Test obj = new Test () Class obj_test = obj.getClass () Then we can use the obj_test to perform reflection as this object will have all the information about the class Test.įorName () method takes the name of the class as an argument and returns the Class object.įor example, the object of the Test class can be created as follows: class obj_test = Class.forName (“Test”) class extension.įor example, if Test is a class, then we can create a Class object as follows: Class obj_test = Test.class The first option to create an object of Class is by using the. We can create objects of using one of the following options. Methods to inspect and modify the behavior of a class at run time.Methods to retrieve class metadata at run time. This is the main class used for reflection. The class holds all the information and data about classes and objects at runtime. Next, we will discuss each of these classes and use programming examples to demonstrate the reflection on these classes. Modifier: Modifier class gives us information about a specific access modifier.Īll the above classes are a part of package.Constructor: Constructor class gives information about class constructor that includes constructor access modifier, constructor name, and parameter types.Method: The Method class can help us to extract information like access modifier of the method, method return type, method name, method parameter types, and exception types raised by the method.Field: The Field class has information that we use to declare a variable or a field like a datatype (int, double, String, etc.), access modifier (private, public, protected, etc.), name (identifier) and value.Using Reflection API, we can implement the reflection on the following entities: It is used, when your application has third-party libraries and when you want to know about the classes and methods available.IDE (Integrated Development Environment) E.g.Reflection is mainly used in debugging tools, JUnit, and frameworks to inspect and change the behavior at runtime.A distinguishing feature of Reflection API is that we can also manipulate the private data members or methods of the class. We use Reflection API provided by the above packages to modify the class and its members including fields, methods, constructors, etc. The special class “” provides the methods and properties to extract metadata using which we can inspect and modify the class behavior. In Java, the “java.lang” and “” are the two packages that provide classes for reflection. We inspect these objects and then change their behavior at runtime using reflection API. The objects can be anything like methods, interfaces, classes, etc. Thus we can use Reflection API in our programs for the purpose of modifying the object’s behavior. As a result, we can modify the behavior of this object at runtime. Then we use the Reflection API on this object. In the above representation, we can see that we have an unknown object. The “Reflection” process is depicted below. Reflection is an “Application Programming Interface” (API) provided by Java. An object can be a class, a field, or a method.” Thus a Reflection can be defined as a “technique of inspecting and modifying the runtime behavior of an unknown object at run time. Java programming language provides a feature called “Reflection” that allows us to modify the runtime behavior of a class or field or method at runtime. In other words, it is very difficult to change the behavior of various programming components at runtime especially for unknown objects. Whether the properties and methods are anonymous or have names, they can be changed at our will during compile time.īut we cannot change these classes or methods or fields at runtime on the fly. We are aware that in a given class we can modify its properties and methods at compile time and it is very easy to do so.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |