![]() Keep in mind, if there is an Inheritance with IS – A relationship we can do Upcasting in Java in every scenario. There will not be any runtime error as, the cast is possible because at runtime animalis actually a Man even though the static type of animal is Animal. Hence we don’t get a compilation error here. So the Updated and Corrected Code would look like, Animal animal = new Man() īasically, we are telling the compiler that we know runtime type of the object. UpCasting in java is a Much Safer option always.Always Prefer doing instance of check in case of DownCasting.Lets Say if you have a cenario where you really want to go ahead with DownCasting in Java. We have basically done DownCasting here, thats the reason it looks messy and even not recommended in java to go for DownCasting. Downcasting a subclass to its superclass makes the functionality of the subclass unavailable. ![]() The reason you have to ask for it in Java is that Java is intended to be a type-safe language. Note: –We can use the instanceof operator to check the runtime type of the object before performing the cast, which allows you to prevent ClassCastException but whether you have to ask for it to be done. We can use the instanceof operator to check the runtime type of the object before performing the cast, which allows you to prevent ClassCastException. Now at runtime, it performs the cast and it seems that animal isn’t really a Man and so throws a ClassCastException . Downcasting is considered a code smell, and it is recommended to consider. Parent and Child objects are two types of objects. Downcasting is when you cast an object from super type to subtype. In Java, the object can also be typecasted like the datatypes. Here you Will get a ClassCastException The reason why is because animal‘s runtime type is, Animal . A process of converting one data type to another is known as Typecasting and Upcasting and Downcasting is the type of object typecasting. So the Code Looks Like:- Animal animal = new Man() Upcasting is allowed in Java, however downcasting gives a compile error. In your case, a cast from Man to an Animal is an upcast, because of a Man is-a Animal In general, you can upcast whenever there is an is-a relationship between two classes. Upcasting is always allowed, but downcasting involves a type check and can throw a ClassCastException. Upcasting is casting to a supertype, while downcasting is casting to a subtype. Let’s discuss why ? What is Upcasting & Downcasting Here:. What would be the Output ? If you know the answer then i think you are not the target audience here and you can skip this post.įor Point 2 there will be a compilation Error as well as runtime error. ![]() Let’s have a look at the problem or rather question. We will talk about Up casting and Down Casting in Java. It is one of the most popular interview questions in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |