public class Reflection {
    public static void main(String [] args) {
        Class c1 = "hello".getClass();
        System.out.println(c1);
        Class c2 = "bye".getClass();
        System.out.println("The two classes are the same: " + c1.equals(c2));
        Class c3 = (new StringBuffer("hello")).getClass();
        System.out.println("The two classes are the same: " + c2.equals(c3));

        // classes of other data types:
        String [] arr = {"hello", "bye"};
        System.out.println(arr.getClass());
        int [] i_arr = {6, 7};
        System.out.println(i_arr.getClass());
        System.out.println(int.class); //getClass doesn't work for primitive types

        Circle circle = new Circle(6,7,5);
        System.out.println("the class of a circle: " + circle.getClass());
        System.out.println("the superclass of a circle: " +
                           circle.getClass().getSuperclass());
        ColorCircle cc = new ColorCircle(7, 8, 5, "blue");
        System.out.println("the class of a color circle: " + cc.getClass());
        System.out.println("the superclass of a color circle: " +
                           cc.getClass().getSuperclass());
        System.out.println("Class hierarchy of ColorCircle:");
        Class c = cc.getClass();
        while ( !c.equals((new Object()).getClass())) {
            System.out.println(c);
            c = c.getSuperclass();
        }

        try {
            String s = "hello".getClass().newInstance(); // constructor takes no arguments
            System.out.println(s);
        } catch (InstantiationException e) {
            System.out.println("Cannot create an instance");
        } catch (IllegalAccessException e) {
            System.out.println("Cannot access the object");
        }

        try {
            Class cl = Class.forName("Circle");
            Circle  circ = (Circle) cl.newInstance(); // constructor takes no arguments
            circ.draw();
        } catch (InstantiationException e) {
            System.out.println("Cannot create an instance");
        } catch (IllegalAccessException e) {
            System.out.println("Cannot access the object");
        } catch (ClassNotFoundException e) {
            System.out.println("Cannot find the class");
        try {
            Class cl = Class.forName("Circle");
            Circle  circ = (Circle) cl.newInstance(); // constructor takes no arguments
            circ.draw();
        } catch (InstantiationException e) {
            System.out.println("Cannot create an instance");
        } catch (IllegalAccessException e) {
            System.out.println("Cannot access the object");
        } catch (ClassNotFoundException e) {
            System.out.println("Cannot find the class");
        }

    }
}

----------------------------------------------------

public interface Shape {
    public void draw();
}

---------------------------------------------------

public class Circle implements Shape {
    private int x;
    private int y;
    private int radius;

    public Circle() {

    }

    public Circle (int x, int y, int radius) {
        this.x = x;
        this.y = y;
        this.radius = radius;
    }
    public void draw() {
        System.out.println("x = " + x + " y = " + y + " r = " + radius);
    }
}

------------------------------------------------------------

public class ColorCircle extends Circle{
    private String color;
    public ColorCircle (int x, int y, int radius, String color) {
        super(x,y,radius);
        this.color = color;
            }
    public void draw() {
        super.draw();
        System.out.println(" color = " + color);
    }
}


CSci 4651 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.