File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Casting classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Casting classes" Watch "Casting classes" New topic

Casting classes

Mauro Trevigno
Ranch Hand

Joined: Aug 27, 2012
Posts: 96

Hi guys,

I look in the java API and I dont find anything related, just a lot of type mismatch generics issues, so still I cant understand why asci = sc, its a type mismatch.

public class Testing {

public static void main (String args[]) {

ASCI asci = new ASCI();
Testing sc = new Testing();

//asci = sc; ///// Type mismatch: cannot convert from Testing to ASCI
sc= asci This one Works!
class ASCI extends Testing{




Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Instead of using confusing names like "Testing" and "ASCI", let's use some names that help us think about the problem. How about "Animal" and "Dog". A Dog is a kind of animal, right? Also, a Cat is another kind of Animal. So this is legal:

But this is not legal

In the second example, we're actually smarter than the compiler. We know that "a" is pointing to a Dog, but the compiler does actually know that; it just knows that it might not be a Cat, and so the second assignment is not legal. You can force the compiler to listen to you using a cast:

The compiler will compile the program this time, but when you run it, you'll get a ClassCastException -- an error that says, basically, you lied to the compiler, and told it "a" contained a Cat, when it did not.

[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15092

Ernest gave a good explanation.

One thing to keep in mind about casts is: Casting does not do any kind of automatic conversion*. The only thing that is means is that you are telling the compiler: "Look, I have this object of type X here, but I know that it's really an Y, so I want you to treat it as if it is an Y - don't complain about it". Note that the type of the object will still be checked - but at runtime instead of at compile time.

Since it's always better to catch errors as early as possible, you'll want to avoid casting as much as possible in your programs - because it's delaying a possible error from compile time to runtime.

* Actually, when casting between primitive types, for example from float to int, a conversion is done. But for non-primitive types, there are no automatic conversions.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Mauro Trevigno
Ranch Hand

Joined: Aug 27, 2012
Posts: 96

Thanks Guys, I got it
I agree. Here's the link:
subject: Casting classes
jQuery in Action, 3rd edition