• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Casting classes

 
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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{

}


Thanks!

Regards,
-M

 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Mauro Trevigno
Ranch Hand
Posts: 99
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys, I got it
 
I got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic