Friday, February 20, 2009

Marker Interface and Reflection Mechanism

Marker Interface
The marker interface is just an empty interface. Its purpose is to "mark". That is, a class is suppose to contain certain capability so it "marks" itself with a marker interface -- for example a class capable of cloning itself marks itself with 'Cloneable' interface. Its just a way of Object Oriented Programming. A certain framework (for example RMI) will only deal with classes which are "mark" with their required interfaces (for example Remote).
Reflection Mechanism
Reflection enables Java code to discover information about the fields, methods and constructors of loaded classes, and to use reflected fields, methods, and constructors to operate on their underlying counterparts on objects, within security restrictions. The API accommodates applications that need access to either the public members of a target object (based on its runtime class) or the members declared by a given class.
Ex.
import java.lang.reflect.*;
class B
{
private int val=10;
}
class A
{
public static void main(String[] args) throws Exception
{
B bob=new B();
Field fld=bob.getClass().getDeclaredField("val");fld.setAccessible(true);
System.out.println("value:"+fld.getInt(bob));}}

No comments:

Post a Comment