-1

I'm trying to understand how object casting works in Java Internally.

Here is my small code example

public interface Processor {

}


public class Mainclass {

    Object getObject(){

        return new Object();
    }
    public static void main(String[] args){


        Mainclass ob = new Mainclass();
        Processor prcsr = (Processor) ob.getObject();
   }
}

When ob.getObject() gets called, It returns an Object reference type which is holding a location of Object class instance. Correct ?

So when we cast Object reference type to Processor, What is happening actually ? Does it converts Object reference type to processor type which will be holding to Object class Instance location?

If yes, then Processor is a Interface here, then who is Implementing this Interface here ?

I'm confused here, Can anyone help me how it works internally ?

Thanks

  • 5
    That code should raise a `ClassCastException`, since an instance of `Object` is not an instance of `Processor`. – Daniel Pryden Apr 17 '19 at 16:42
  • 3
    Nothing happens, other than Java checks if the thing being cast is an instance of the thing you are casting too. Casting is just a way of telling the compiler you know more than it does (in this case, you're wrong though). – Andy Turner Apr 17 '19 at 16:45
  • 8
    Casting directly is the programmer telling the compiler, "No, seriously! It's a Processor" and the complier says, "Okay, you're the boss"... and then the runtime treats your object like a Processor and says: "Complier, you're drunk, go home and take this exception with you!" And then the programmer gets that exception and rethinks his life. –  Apr 17 '19 at 16:45

1 Answers1

0

Does it converts Object reference type to Processor type which will be holding to Object class Instance location?

No, because Object does not implement the interface Processor. Also because of this, the cast will fail, throwing a ClassCastException.

Casting a type (Object) to another unrelated type (Processor) will generally fail.

What is happening actually?

Casting is you telling the compiler what type a variable should actually be, because you know better than than the compiler.

Here

Processor prcsr = (Processor) ob.getObject();

You are telling the compiler

Hey compiler, you only know that obj.getObject returns something of type Object, but I know better than you. I know that obj.getObject is going to return a type that implements Processor.

The compiler hears this and goes "okay", and lets you assign it to the variable prcsr, which is of type Processor.

However, at runtime, it turns out that obj.getObject returned an instance of Object, which does not implement Processor, so what you were telling the compiler was wrong, and an exception gets thrown.

Now let's say obj.getObject actually returns an instance of FooProcessor, which does implement Processor (declaration not shown here):

Object getObject(){

    return new FooProcessor();
}

Now at runtime, the cast succeeds because what you were telling the compiler was correct.

So basically, casting is mostly a compile time thing. Very little actually happens at runtime. At runtime it just checks whether what you were saying was right or not. If is wrong, throw an exception.

Sweeper
  • 213,210
  • 22
  • 193
  • 313