![]() ![]() Here, superclass reference o refers to subclass object. Let’s take an example program to see the effect of upcasting where superclass reference refers to subclass object. Java compiler will do the implicit casting. If we do not use the cast operator in the above case, even we will not get any error message. This kind of conversion is called upcasting or widening in Java. Here, the subclass object type has converted into super class type. We can do it like this: One o = (One) new Two() // Converting class Two's type into class One. We will convert object’s data type into class O using cast operator. In this statement, on the left side, the data type of reference o is One, but on the right side, we got object whose data type is Two. But when a reference of class refers to different class’s object, we need casting.įor example: One o = new Two() // class One's reference o is pointing to class Two's object. Therefore, casting is not required in this case. Data type of the class object is also One. So, data type of o is One.Īt right side of the assignment operator, we have a class One’s object. This is because on the left side and on the right side of assignment operator (=), we have the same data type.įor example, in the first statement, o is a reference of class One. In the above two statements, we do not need casting. ![]() In this statement, class Two’s reference t is pointing to Two’s object. Similarly, subclass reference is used to point to subclass object. ![]() In the above statement, class One’s reference o is pointing or referring to One’s object.Ģ. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |