Object and Class classes

Java-Object and Class classes

Object

The Object class is a superclass of all other classes. This means all other classes are subclass of the Object class. Object has eleven methods :

Object and Class classesA reference variable of a superclass can be assigned a reference to any subclass derived from that superclass. Thus, a reference variable of type Object can store a reference to an object of any other class type. For example :

Object and Class classes

However, you can’t do anything useful with the obj variable, because the compiler doesn’t know it’s an Integer. For example, if the Integer class has a method named floatValue(), the code : obj.floatValue()  doesn’t work. Because obj is an Object, not an Integer, the compiler doesn’t know about the floatValue() method. To do anything specific with the value, you need to have some knowledge about the original type and apply a cast :

Object and Class classes

The statement Integer ino = (Integer)obj now converts, or casts to type Integer before storing it in ino. This is called an explicit cast, and the conversion process is referred to as casting. This is necessary to explicitly employ casts to translate between Object and the type of data that is actually being operated upon.

Class

Class class encapsulates the run-time state of a class or interface. Objects of type Class are created automatically, when classes are loaded. You cannot explicitly declare a Class object. Class is a generic type(Class<T>).

Object and Class classes

Generally, you obtain a Class object by calling the getClass( ) method defined by Object. Suppose you have a variable daob of type Data1 that might contain a reference to an object of type Data1, Data2, Data3, or even Data4.

Object and Class classesTo figure out what sort of thing it really refers to, you could write the following statements:

Object and Class classes

The method getSimpleName() is a member of the Class class, and it returns the name of the actual class of the object for which it is called as a String object. Thus, the second statement outputs the name of the class for the daob object. If daob referred to a Data3 object, this would output : Data3. If you just wanted to output the class identity, you need not explicitly store the Class object. You can combine both statements into one: daob.getClass().getSimpleName(). This will produce the same output as before. Remember that the Class object returns the actual class of an object. Suppose you define a String object like this: String language = “Java”;. You could store a reference to this String object as type Object :

Object and Class classes

The third statement displays the type of obj. This statement outputs the type name as String. The fact that the reference is stored in a variable of type Object does not affect the underlying type of the object itself. When your program is executing, there are instances of the Class class in existence that represent each of the classes and interfaces in your program. There is also a Class object for each array type in your program as well as every primitive type. The Java Virtual Machine (JVM) generates these when your program is loaded. Because Class is primarily intended for use by the JVM, it has no public constructors, so you can’t create objects of type Class yourself.

Although you can use the getClass() method to get the Class object corresponding to a particular class or interface type, there is a more direct way. If you append .class to the name of any class, interface, or primitive type, you have a reference to the Class object for that class. For example, String.class references the Class object for the String class and Data1.class references the Class object for the Data1 class.

Object and Class classes

This may not seem particularly relevant at this point, but keep it in mind. Since there is only one Class object for each class or interface type, you can test for the class of an object programmatically. Given a variable daob of type Data1, you could check whether the object referenced was of type Data1 with the following statement :

Object and Class classes

This tests whether the object referenced by daob is of type Data1. Because each Class object is unique, this is a precise test. If daob contained a reference to an object that was a subclass of Data1, the result of the comparison in the if would be false.

Program

Object and Class classes

Program Source

public class Javaapp {

    public static void main(String[] args) {
        
        Integer in = new Integer(50);
        System.out.println("1 : "+in.getClass().getSimpleName());
        System.out.println("2 : "+in.getClass().getName());
        System.out.println("3 : "+in.getClass().getPackage());
        System.out.println("4 : "+in.getClass().getSuperclass());
        System.out.println("5 : "+in.getClass().isMemberClass());
        System.out.println("6 : "+in.getClass().isLocalClass());
        System.out.println("7 : "+in.getClass().isPrimitive());
        System.out.println("8 : "+in.getClass().isInterface());
        System.out.println("9 : "+in.getClass().isArray());
        String str = null;
        Integer in2 = null;
        Object obj = in;
        if(obj.getClass()==String.class){
            
            System.out.println("obj -> String");
            str=(String)obj;
            System.out.println("str -> String -> "+str);
        }else if(obj.getClass()==Integer.class)
        {
            System.out.println("obj -> Integer");
            in2=(Integer)obj;
            System.out.println("in  -> Integer -> "+in);
        }else{
            System.out.println("obj -> "+obj.getClass().getSimpleName());
        }
    }
}

Leave a Comment