
So just make sure that if you are doing any downcasting, that you're well aware of the type of object you'll be casting. So, unless you're doing something with error handling, then your program will likely exit or you'll get an ugly error message on your webpage. This is what's known as a run-time exception, as it's really only detectable when your program is running. If you were to try to cast something like an Integer to a String, then you'll get a ClassCastException. There is a certain amount of risk that goes along with downcasting your variables. This will result in the run time exception ClassCastException.
JAVA DOWNCAST CODE

When you try to cast an object of Parent class to its Child class type, this exception will be thrown.When will be ClassCastException is thrown: It is an unchecked exception and thus, it does not need to be declared in a method's or a constructor's throws clause. The ClassCastException extends the RuntimeException class and thus, belongs to those exceptions that can be thrown during the operation of the JVM (Java Virtual Machine).
JAVA DOWNCAST FULL
The full exception hierarchy of this error is: Exception hierarchyĪll Java errors implement the interface, or are extended from another inherited class therein. Casting only works when the casted object follows an is a relationship to the type you are trying to cast to. So, this exaception occurs when you try to cast an instance of an Object to a type that it is not. In the above example, when tries to cast an Integer to a String, String is not a subclass of Integer, so a ClassCastException will be thrown. exampleĮxception in thread "main" : canno

All you need to do, is, place a method inside of the Cat class, that converts the fields and returns a new Dog based on that.The ClassCastException thrown to indicate that your code has attempted to cast an object to a subclass of which it is not an instance.

We just need to write it once, and every Animal gets it through inheritance.Ĭonsider the following example: class Animal Īnd you want to make a Dog out of the Cat. What this means for a programmer, is that we don’t need to write for every possible Animal, that it has health. Cat is also an Animal and a Mammal, which logically means – if Mammals possess mammary glands and Animals are living beings, then Cat also has mammary glands and is living being.

Object is Cat’s grandgrandparent, which means Cat is also an Object. Now, if you ask – is Cat an Object – It doesn’t extend Object, it extends Mammal?īy inheritance Cat gets all the properties its ancestors have. By silently, i mean, that Java automatically extends every class from Object class, which isn’t extended from something else, so everything is an Object (except primitives). You can see, that Cat and Dog are both Mammals, which extends from Animal, which silently extends from Object. What we have here, is a simplified version of an Animal Hierarchy. Throughout this tutorial i’m going to use Animal hierarchy to explain how class hierarchy works. Upcasting and downcasting are NOT like casting primitives from one to other, and i believe that’s what causes a lot of confusion, when programmer starts to learn casting objects. Upcasting is done automatically, while downcasting must be manually done by the programmer, and i’m going to give my best to explain why is that so. Java permits an object of a subclass type to be treated as an object of any superclass type. Upcasting and downcasting are important part of Java, which allow us to build complicated programs using simple syntax, and gives us great advantages, like Polymorphism or grouping different objects.
