I hope you will have understood to perform Java upcasting and downcasting nicely and enjoyed related programming.As? Types - means the down casting process is optional. Hope that this tutorial has covered almost all important points associated with upcasting and downcasting in java with example program. In this way, downcasting is possible in java. In the preceding code, you can observe that if we create an object of subclass, it is possible to access all the methods of super class and subclass. Now, we can call both methods m1() and m2() using reference variable of sub class. In the above program, superclass reference type has converted into sub class reference type. T.m2() // Calling m1 method using reference variable of sub class. T.m1() // Calling m1 method using reference variable of sub class. Two t = (Two)o // Converting super class reference type into sub class reference type. One o = new Two() // Super class reference refers to sub class object. So, look at the following source code and modify it. To overcome this problem, we will have to modify the previous code. So, let’s understand how downcasting is possible in java? How Downcasting is possible? So, in the case of downcasting, we cannot access any of the methods of super class or sub class. Therefore, JVM is thrown an exception named ClassCastException at runtime. In the above example program, we have performed downcasting directly. Here, subclass reference t refers to superclass object.Įxception in thread "main" :ĬlassCasting.One cannot be cast to classCasting.Two Let’s consider the above example program where we will do downcasting and see what happens? If we perform it directly, Java compiler will give compile-time error. Therefore, we need a compulsory cast operator. Here, we are converting superclass object type into subclass type. Look at the below figure where subclass reference is pointing to superclass object. In other words, when sub class type is converted into super class type, it is called downcasting. When subclass reference refers to super class object, it is called narrowing or downcasting in java. This shows that you can access only 50% of the functionality. ("m1 method in class Two") Īs you can observe in the program that it is possible to access sub class method but not super class method when super class method is overridden in sub class. Let’s take another program where we will override super class method in sub class and will observe that can we access super class method and sub class method. Hence, in upcasting or widening, we can call all methods of super class but not sub class methods. Now, observe that we are able to call m1() method of super class.īut when we are trying to call m2() method of sub class, we got error message: compile-time error. This means that sub class object type is converted into super class type. In this example program, superclass reference is pointing to subclass object. Here, super class reference o refers to sub class object. Let’s take an example program to see the effect of upcasting where super class reference refers to sub class 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, sub class object type has converted into super class type. It can be done 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 assignment operator, we have class One’s object. This is because on the left side and at 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, sub class reference is used to point to sub class object. In the above statement, class One’s reference o is pointing or referring to One’s object.Ģ. Super class reference is used to refer to superclass object.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |