This example explains the follwoings
  • instance vs. extension (super-class and sub-class)
  • scope of modifiers.
  • reflection
From sun-micro----
Class cls = java.lang.String.class;
    Method method = cls.getMethods()[0];
    Field field = cls.getFields()[0];
    Constructor constructor = cls.getConstructors()[0];
    String name;
   
    // Fully-qualified names
    name = cls.getName();     // java.lang.String
    name = cls.getName()+"."+field.getName();     // java.lang.String.CASE_INSENSITIVE_ORDER
    name = constructor.getName();      // java.lang.String
    name = cls.getName()+"."+method.getName();    // java.lang.String.hashCode
   
    // Unqualified names
    name = cls.getName().substring(cls.getPackage().getName().length()+1);  // String
    name = field.getName();            // CASE_INSENSITIVE_ORDER
    name = constructor.getName().substring(cls.getPackage().getName().length()+1); // String
    name = method.getName();           // hashCode
From sun-micro----

import java.lang.reflect.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class Test_this {
static public void main( String[] args )throws IOException
{
System.out.println("----");
process_this pt = new process_this("Manas");
printName(pt);
}
static void printName(Object o)
{
Class cls = o.getClass();
Field[] fld =cls.getDeclaredFields();
fld = cls.getFields();
for (int i=0; i<fld.length; i++) {
System.out.println(" ---Field : "+ (i+1));
Class type = fld[i].getType();
System.out.println(fld[i]);
System.out.println(" get type: getName : "+ type.getName());
System.out.println(" get type: hascode() : "+ type.hashCode());
System.out.println(" get type: toString() : "+ type.toString());

}
}
}
class process_this
{
public String str= "manas";
private int n1 = 23;
public int n2 = 34;
public process_this(){ System.out.println("this is a blank constructor");}
public process_this(String str){ System.out.println("this is a string constructor");}
public process_this(int n1){ System.out.println("this is a integer constructor");}
public process_this(String str, int n1){ System.out.println("this is a string and integer constructor");}
public void method_this(String str2)throws IOException
{
int n1 = 12;
double dd= 23.45;
System.out.println("will do something later"+ n1 + dd);
}
}

 

This example shows the propoerties of an extended class, with reflection you can access the private members during the run time.

import java.lang.reflect.*;
import java.util.*;
import java.net.*;
import java.io.*;

public class Test_this {
static public void main( String[] args )throws IOException
{
System.out.println("----");
process_this pt = new process_this("Manas");
printName(pt);
}
static void printName(Object o)
{
String str_fld ;
Class cls = o.getClass();
Field[] fld =cls.getDeclaredFields();
fld = cls.getFields();
for (int i=0; i<fld.length; i++) {
System.out.println(" ---Field : "+ (i+1));
Class type = fld[i].getType();
System.out.println(fld[i]);
System.out.println(" get type: getName : "+ type.getName());
System.out.println(" get type: hascode() : "+ type.hashCode());
System.out.println(" get type: toString() : "+ type.toString());
str_fld = fld[i].getName();
System.out.println("Filed name : " + str_fld);
}
}
}
class process_this
{
public String str= "manas";
private int n1 = 23;
public int n2 = 34;
public process_this(){ System.out.println("this is a blank constructor");}
public process_this(String str){ System.out.println("this is a string constructor");}
public process_this(int n1){ System.out.println("this is a integer constructor");}
public process_this(String str, int n1){ System.out.println("this is a string and integer constructor");}
public void method_this(String str2)throws IOException
{
int n1 = 12;
double dd= 23.45;
System.out.println("will do something later"+ n1 + dd);
}
}

 

 
 

import java.lang.reflect.*;
//import java.util.*;
//import java.net.*;
import java.io.*;

public class Test_this extends process_this{
private int n = 12;
static public void main( String[] args )throws IOException
{
try {
Class cls = Class.forName("process_this");
Field fieldlist[] = cls.getDeclaredFields();
for (int i = 0; i < fieldlist.length; i++) {
Field fld = fieldlist[i];
System.out.println("name = " + fld.getName());
System.out.println("decl class = " + fld.getDeclaringClass());
System.out.println("type = " + fld.getType());
int mod = fld.getModifiers(); System.out.println("modifiers = " +Modifier.toString(mod));
System.out.println("-----");
}
}
catch (Throwable e) {
System.err.println(e);
}

}

}

class process_this
{
public String str= "manas";
private int n1 = 23;
public int n2 = 34;
public process_this(){ System.out.println("this is a blank constructor");}
public process_this(String str){ System.out.println("this is a string constructor");}
public void method_this(String str2)throws IOException
{
double dd= 23.45;
String st = "hello";
System.out.println("will do something later"+ this.n1 + dd);
//return st;
}
}
 

 
Using the example below we can overview the differences between extension of a class or creating an instance of a class; and use of reflection with the extended class. The object created from an instance of a class using "new" key word can't access the private member. Where as with the reflection, the subclass **(extended class), we can access the private member of

**class subclass-name extends superclass-name { // body of subclass where public members can reused as one of the rules of inheritance }