In C++, the keyword that implements parameterized types is “template.” Java currently has no parameterized types since it is possible for it to get by-however awkwardly-using the singly rooted hierarchy. Parameterized types are an important part of C++, partly because C++ has no singly rooted hierarchy. For example, with a parameterized container, the compiler could customize that container so that it would accept only Shapes and fetch only Shapes. A parameterized type is a class that the compiler can automatically customize to work with particular types. Wouldn’t it make sense to somehow create the container so that it knows the types that it holds, eliminating the need for the downcast and a possible mistake? The solution is called a parameterized type mechanism. When you fetch object references from a container, though, you must have some way to remember exactly what they are so you can perform a proper downcast.ĭowncasting and the run-time checks require extra time for the running program and extra effort from the programmer. It’s not completely dangerous, however, because if you downcast to the wrong thing you’ll get a run-time error called an exception, which will be described shortly. With upcasting, you know, for example, that a Circle is a type of Shape so it’s safe to upcast, but you don’t know that an Object is necessarily a Circle or a Shape so it’s hardly safe to downcast unless you know exactly what you’re dealing with. This manner of casting is called downcasting. Instead, you cast down the hierarchy to a more specific type. Here, the cast is used again, but this time you’re not casting up the inheritance hierarchy to a more general type. So how do you turn it back into something that has the useful interface of the object that you put into the container? When you fetch it back, you get an Object reference, and not a reference to the type that you put in. But, since the container holds only Objects, when you add your object reference into the container it is upcast to Object, thus losing its identity. To use such a container, you simply add object references to it and later ask for them back. The singly rooted hierarchy means that everything is an Object, so a container that holds Objects can hold anything. The direction is from ground to top, hence it is named as upcast.To make these containers reusable, they hold the one universal type in Java: Object. It is assigned to the top floor reference variable. Lr_top_floor = lr_ground_floor (less view) Therefore, narrowing cast. Therefore there is no question mark during the assignment of the ground floor instance to the top floor instance. People will not question you, as the visibility will be less. You will not be able to see the other properties. When you stand on the ground floor, the view is less. It is assigned to the ground floor(Downcast). So lr_ground_floor ?= lr_top_floor (more view) widening cast. In this case remember the direction of movement. In the above case, it is assigned to the ground floor (Downcasting) In other words it is moving down from top floor to ground floor. Please remember the word Assignment to distinguish the upcast and downcast. Therefore during the assignment, place a question mark before the ‘=’ sign. When you are able to view the other houses, people will always question your increased visibility. Let us assign the instance of the top floor to the ground floor. Therefore it is called as Narrowing cast. The properties are not visible in the ground floor. So, we can call it as widening cast When you are in the ground floor, the view is narrowing. When you stand in the top floor, the view of the properties such as roads, other houses are visible easily. Lr_ground_floor = New zcl_ground_floor (). Data lr_top_floor TYPE REF TO zcl_top_floorĭata lr_ground_floor TYPE REF TO zcl_ground_floor
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |