[ACCEPTED]-Java rules for casting-object

Accepted answer
Score: 33

In Java there are two types of reference 14 variable casting:

  • Downcasting: If you have a reference variable 13 that refers to a subtype object, you can 12 assign it to a reference variable of the 11 subtype. You must make an explicit cast 10 to do this, and the result is that you can access 9 the subtype's members with this new reference 8 variable.

  • Upcasting: You can assign a reference variable 7 to a supertype reference variable explicitly 6 or implicitly. This is an inherently safe 5 operation because the assignment restricts 4 the access capabilities of the new variable.

Yes, you 3 need to implement the interface directly or indirectly to enable 2 assigning your class object reference to 1 the interface type.

Score: 8

Suppose we want to cast d object to A,

A 12 a = (C)d;

So internally 3 rules have been 11 checked by Compiler and JVM. The compiler is checking first 2 rules at Compile time and JVM will check last one rule at Runtime.

Rule 1 (Compile time checking):

Type of 'd' and 10 C must have some relation (child to parent 9 or parent to child or same time).If there 8 is no relationship then we will get a compile 7 error(inconvertible types).

Rule 2 (Compile time checking):

'C' must be 6 either same type or derived type(subclass) of 5 'A' otherwise we will get a compile error(incompatible 4 types).

Rule 3 (Runtime Exception):

Runtime object type of 'd' must 3 be same or derived a type of 'C' otherwise 2 we will get a runtime exception (ClassCastException Exception).

Find 1 following examples to get more idea,

String s = new String("hello"); StringBuffer sb = (StringBuffer)s;  // Compile error : Invertible types because there is no relationship between.

Object o = new String("hello"); StringBuffer sb = (String)o;       // Compile error : Incompatible types because String is not child class of StringBuffer.

Object o = new String("hello"); StringBuffer sb = (StringBuffer)o; // Runtime Exception : ClassCastException because 'o' is string type and trying to cast into StingBuffer and there is no relationship between String and StringBuffer.
Score: 4

This will work:

class Foo implements Runnable {
    public void run() {}

Foo foo = new Foo();
System.out.println((Runnable) foo);

But this will not:

class Bar {
    public void run() {}

Bar bar = new Bar();
System.out.println((Runnable) bar);

Because 6 although Bar has a run() method that could implement 5 Runnable.run(), Bar is not declared to implement Runnable so it cannot 4 be cast to Runnable.

Java requires that you declare 3 implemented interfaces by name. It does not 2 have duck typing, unlike some other languages such 1 as Python and Go

Score: 3

There's an intuitive way of thinking about 18 this - you're not changing an object with 17 a cast, you're only doing something that 16 would already be permitted if the type was 15 known - inotherwords, you can only cast 14 to a type that your object already is. So 13 just look "up" the object chain 12 to see what kinds apply to your object.

So 11 you can cast to an interface only if it's defined 10 somewhere higher up in the chain (e.g. if 9 your classes parent implements it, etc. etc). It 8 has to be explicit - from your question 7 it sounds like you may be thinking that 6 if you implement method "void foo()" then 5 you should be able to cast to an interface 4 that defines the method "void foo()" - this 3 is sometimes described as "duck typing" (if it quacks 2 like a duck, it's a duck) but is not how 1 java works.

Score: 2

You can cast if the runtime type of an object 4 is a subtype of what you're trying to cast 3 it into.


Yes, the object that you're 2 trying to cast will need to implement the 1 interface in order for you to cast it successfully.

Score: 2


interface MyInterface{}

class MyClass implements MyInterface{}


MyClass m = new MyClass();
MyInterface i = (MyInterface)m;

is possible.


More Related questions