1

What is the difference between.

public class Test {

    public static void main(String args[]) {
        String toBeCast = "cast this string";
        A a = toBeCast; // error - Type mismatch: cannot convert from String to A
        Object object = toBeCast;
    }
}


public class A {

}

When we say every object extends Object class, why does A a = toBeCast; not allowed, but this Object object = toBeCast; works fine.

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • other way around: why do you think `String` objects should be castable to `A`? Where in your code do you make sure A is a superclass of String, so that the compiler knows that everything that A should be able to do is covered by String? – Mike 'Pomax' Kamermans Nov 21 '14 at 01:59
  • @Mike'Pomax'Kamermans actually i am just thinking of `Object class` here. `A is also Object` and so. – Ankur Singhal Nov 21 '14 at 02:00
  • 2
    no, A is a *subclass* of Object, just like how String is a subclass of Object. – Mike 'Pomax' Kamermans Nov 21 '14 at 02:00
  • @Mike'Pomax'Kamermans and there is no relation between `A and String`, am i correct, so cannot do casting..?? – Ankur Singhal Nov 21 '14 at 02:01
  • 1
    indeed. That's how hierarchies work. If two classes are in the same hierarchy chain, then an instance of the subclass can be safely cast to an instance of the superclass. In this case, `A` and `String` are very much not in the same chain. – Mike 'Pomax' Kamermans Nov 21 '14 at 02:03
  • 1
    Casting an object reference does *not* change the type of the object. In fact, it does not change the object in any way. This is in contrast to casting a scalar which *does* modify its format and value. – Hot Licks Nov 21 '14 at 02:19
  • 1
    There is an old saying in country of origin that goes something like "just because a dog have legs, doesn't make it a table" – hfontanez Nov 21 '14 at 02:23

5 Answers5

4

Remember that old saying from geometry class - "Every square is a rectangle, but not every rectangle is a square". Generalize that to: "Every square/parallelogram/rhombus is a polygon, but not every polygon is a square/parallelogram/rhombus".

Here's what you're doing :

String toBeCast = "cast this string" //this rhombus is a rhombus: cool!
A a = toBeCast; //this parallelogram is that rhombus : WTF? that doesn't make sense!  
Object object = toBeCast; //this polygon is that rhombus: cool! 
drew moore
  • 31,565
  • 17
  • 75
  • 112
1
  OBJECT
  /    \
  A    String

this is how your class hierarchy looks like that is why casting to Object A gives an error.

Ker p pag
  • 1,568
  • 12
  • 25
1

The variable toBeCast is an instance of the string struct. The variable a is an instance of the class A.

Its possible to compile the following:

String toBeCast = "cast this string";
Object obj = toBeCast;

This is because, as you say every instance of an object (including strings) inherit from System.Object, however the following will not compile:

A a = toBeCast;

Although a (Type A) inherits from System.Object and toBeCast (Type String) inherits from Object, Type A does not inherit from Type String.

And so the compiler returns: "Type mismatch: cannot convert from String to A".

Fehr
  • 476
  • 4
  • 5
0

Because String is not a Sub Class of A

shazin
  • 21,379
  • 3
  • 54
  • 71
  • @drewmoore i don't think this question should get downvotes, this doubt might be in minds of many..??. I do not know who did it. – Ankur Singhal Nov 21 '14 at 02:12
0

Take a view at an object window and an object screw. Both of them are Objects, but they are more as an object. There have more specifications to describe both so you know what is a window or a screw. The same is in objects in coding language Java. A casting of classes from whole different environment makes non sense.

String is also Final and an immutable object. Please take a look at Why is String final in Java?

Community
  • 1
  • 1
Sail
  • 21
  • 1