![]() I Post the rest of the code so you can play around with it. The classes Ball, To圜ar and Dice all extend the Product class and implement the getId() method of it, so the three children can all be treated as just Products. Product popProduct = factory.popProduct() ![]() Private ArrayList products = new ArrayList() In this design pattern the implementation details of multiple child classes can be abstracted by just using their parent class.Ĭonsider the following example: import In which situation is downcasting actually used? Thanks for the help everyone.Ī Situation in which Downcasting could occur is in the context of the factory design pattern. Is there any flaw/error in my understanding?Ģ. Because subObj2 is NOT an instance of SubObject, but rather Object, it is incompatible.ġ. My understanding of ClassCastException error is that it's inherited RuntimeException to catch it during compile time, to show that the code has attempted to cast an object to a subclass of which it is not an instance. ![]() SubObject subObj2 = (SubObject) obj2 //this throws the ClassCastException error. So I have successfully downcasted the reference of the base class (Object) to its derived class (subObject). And I am aware that all classes that do not have a direct explicit superclass, are assumed to be subclasses of Object class. Let's say I created a class called SubObject. Please correct me on anything that is incorrect as I've been self learning java for about 2 months now at a really slow pace. If you are multiplying a Double with a Float, they will be unboxed to double and float. Hence, to multiply a double and a float, you promote the variable of the smaller type (float) to the larger type (double). ![]() I'm trying to clarify this so I fully understand type casting. double is an 8 bytes types and float is a 4 bytes type. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |