filmov
tv
Reflection api in java to access private methods with Example | interview questions for experienced

Показать описание
@KingBomm Tech #java #ReflectionApi #KingBommTech
⚡⚡Reflection API:🔥🔥
Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.
⭐⭐Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
⭐⭐Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.
Example :
public class Test {
// creating a private field
private String s;
// creating a public constructor
public Test() { s = "KingBomm Tech"; }
// Creating a public method with no arguments
public void m1() {
}
// Creating a public method with int as argument
public void m2(int n) {
}
// creating a private method
private void m3() {
}
}
public class Demo {
public static void main(String[] args) {
// Creating object whose property is to be checked
Test obj = new Test();
// Creating class object from the object using getclass method
// Getting the constructor of the class through the object of the class
Constructor constructor;
try {
// Getting methods of the class through the object of the class by using getMethods
// Printing method names
for (Method method:methods)
// Creates object of desired method by providing the method name as argument to the getDeclaredMethod
// invokes the method at runtime
// creates object of desired method by providing the
// method name and parameter class as arguments to
// the getDeclaredMethod
// invokes the method at runtime
// creates object of the desired field by providing the name of field as argument to the getDeclaredField method
// allows the object to access the field irrespective of the access specifier used with the field
// takes object and the new value to be assigned to the field as arguments
// invokes the method at runtime
// Creates object of the desired method by providing the name of method as argument to the getDeclaredMethod method
// allows the object to access the method irrespective
// of the access specifier used with the method
// invokes the method at runtime
} catch (NoSuchMethodException |
SecurityException |
IllegalAccessException |
IllegalArgumentException |
InvocationTargetException |
NoSuchFieldException e) {
}
}
}
⚡⚡Advantages 🔥🔥
⭐⭐Extensibility Features
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
⭐⭐Debugging and testing tools
Debuggers use the property of reflection to examine private members on classes.
⚡⚡Drawbacks 🔥🔥
⭐⭐Performance Overhead
Reflective operations have slower performance than their non-reflective counterparts.
⭐⭐Exposure of Internals
Reflective code breaks abstractions.
🌟🌟Share, Support, Subscribe!!!🔥🔥
Another Application with source Code:
⭐⭐ Food Delivery Application Project 🔥 :
⭐⭐ Customer Service Disk Project 🔥:
⭐⭐ Online Quiz Application Project 🔥 :
⭐⭐ Ngo Font-End Web Designing Project 🔥 :
⭐⭐ Online Banking Application Project 🔥 :
⭐⭐ Online Pharmacy Application Project 🔥 :
👉Angular JS Complete Course total Free 🔥 :
Follow Me On Social Media
Share, Support, Subscribe!!!
⚡⚡Reflection API:🔥🔥
Reflection is an API which is used to examine or modify the behavior of methods, classes, interfaces at runtime.
⭐⭐Reflection gives us information about the class to which an object belongs and also the methods of that class which can be executed by using the object.
⭐⭐Through reflection we can invoke methods at runtime irrespective of the access specifier used with them.
Example :
public class Test {
// creating a private field
private String s;
// creating a public constructor
public Test() { s = "KingBomm Tech"; }
// Creating a public method with no arguments
public void m1() {
}
// Creating a public method with int as argument
public void m2(int n) {
}
// creating a private method
private void m3() {
}
}
public class Demo {
public static void main(String[] args) {
// Creating object whose property is to be checked
Test obj = new Test();
// Creating class object from the object using getclass method
// Getting the constructor of the class through the object of the class
Constructor constructor;
try {
// Getting methods of the class through the object of the class by using getMethods
// Printing method names
for (Method method:methods)
// Creates object of desired method by providing the method name as argument to the getDeclaredMethod
// invokes the method at runtime
// creates object of desired method by providing the
// method name and parameter class as arguments to
// the getDeclaredMethod
// invokes the method at runtime
// creates object of the desired field by providing the name of field as argument to the getDeclaredField method
// allows the object to access the field irrespective of the access specifier used with the field
// takes object and the new value to be assigned to the field as arguments
// invokes the method at runtime
// Creates object of the desired method by providing the name of method as argument to the getDeclaredMethod method
// allows the object to access the method irrespective
// of the access specifier used with the method
// invokes the method at runtime
} catch (NoSuchMethodException |
SecurityException |
IllegalAccessException |
IllegalArgumentException |
InvocationTargetException |
NoSuchFieldException e) {
}
}
}
⚡⚡Advantages 🔥🔥
⭐⭐Extensibility Features
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.
⭐⭐Debugging and testing tools
Debuggers use the property of reflection to examine private members on classes.
⚡⚡Drawbacks 🔥🔥
⭐⭐Performance Overhead
Reflective operations have slower performance than their non-reflective counterparts.
⭐⭐Exposure of Internals
Reflective code breaks abstractions.
🌟🌟Share, Support, Subscribe!!!🔥🔥
Another Application with source Code:
⭐⭐ Food Delivery Application Project 🔥 :
⭐⭐ Customer Service Disk Project 🔥:
⭐⭐ Online Quiz Application Project 🔥 :
⭐⭐ Ngo Font-End Web Designing Project 🔥 :
⭐⭐ Online Banking Application Project 🔥 :
⭐⭐ Online Pharmacy Application Project 🔥 :
👉Angular JS Complete Course total Free 🔥 :
Follow Me On Social Media
Share, Support, Subscribe!!!