Upcasting and downcasting in Java
Casting : Taking an object of one type and assigning it to reference variable of another type.
Upcasting : Object of child class is assigned to reference varibale of parent type.
Example :
- class A{
- }
- class B extend A{
- }
class A{ } class B extend A{ }
- A a1 = new B();
A a1 = new B();
The above operation is a upcasting it happens automatically and no need to do anything explicitly.
Here we can call the methods defined/declared in class ‘A’ but during runtime it will call class B’s class overridden methods.
If the method is not overridden in child’s class then only parent’s method which will be inherited to child will be called.
But same is not applicable to variables because variables decision happens at a compile time, so always class A’s variables (not child’s inherited variables) will be accessed.
Let us see an example
- package com.kb.casting;
- class Parent{
- int x=10;
- void show(){
- System.out.println("parent-show");
- }
- void OnlyParentShow(){
- System.out.println("OnlyParentShow");
- }
- }
- class Child extends Parent{
- int x=20;
- void show(){
- System.out.println("child-show");
- }
- void OnlyChildShow(){
- System.out.println("OnlyChildShow");
- }
- }
- public class ParentChild {
- public static void main(String[] args) {
- Parent p = new Child();
- p.show();
- p.OnlyParentShow();
- System.out.println(p.x);
- }
- }
package com.kb.casting; class Parent{ int x=10; void show(){ System.out.println("parent-show"); } void OnlyParentShow(){ System.out.println("OnlyParentShow"); } } class Child extends Parent{ int x=20; void show(){ System.out.println("child-show"); } void OnlyChildShow(){ System.out.println("OnlyChildShow"); } } public class ParentChild { public static void main(String[] args) { Parent p = new Child(); p.show(); p.OnlyParentShow(); System.out.println(p.x); } }
show() is a method and overridden in child so child’s method is called.
OnlyParentShow() is a method not overridden but inherited to child and called as it is.
But look at the variable output , x is 10 in parent and 20 in child, even though child has overridden ‘x’ but variable of parent is choosen.
Downcasting : Is not directly possible in Java.
Example :
- Child c = new Parent();
Child c = new Parent();
The above program will not work. it is compile time error.
This is not possible because all the members of child class are not available in parent,
So biggest mystery can happen if we call c.OnlyChildShow();
It compiles successfully but at runtime there is no such method with parent’s object.
So, no direct downcasting is supported in Java but we can do upcasting and then we can use that variable for downcasting as below
- Parent p = new Child();
- Child c =(Child) p;
Parent p = new Child(); Child c =(Child) p;
Downcasting will not happen automatically like upcasting,we have to cast it explicitly like we did above (Child)p.
Here it looks like a Downcasting but not exactly what we expect(giving parent’s object to child’s reference variable)
It’s putting parent’s reference variable holding child’s object to child’s reference variable.
It is almost equivalent to
- Child c = new Child();
Child c = new Child();
But indirectly using parent’s reference variable as temp.
Whenever upcasting happens always remember
1) Parent's variables will be accessed 2) Child's methods(overridden methods if overriding happened else inherited methods as it is from parent) will be called.
Superb explanation bro!!! thanks a lot !! 🙂
very nice explation bro
Dude you Rock!
love it…great site
hello sir, i want to ask whether this syntax is of upcasting only.
A a1=(A) new(B)
if yes then we we have taken (A) here
Hi i am little confusion about , why variable x value 10 is printed in child class x variable is 20 why not it printed…..please explain this point…….
and also use of upcasting…
Variable binding happens at compile time unlike method binding which happens at run time.
Since we are using p.x,where ‘p’ is parent class variable at compile time(though at run time it points to child class), parent class ‘x’ value will be considered.
Upcasting is required in many cases like writing some common method for multiple child objects and define method in such cases with parameter type as Parent class variable.
thanks for ur nice explaination
I have been reading java from this website to clear my doubts and it’s very helpful compared to other free stuff available on the web….thanx for such simple explanation
Thank you Pratik !!
Thank you for explaining clearly. It was really helpful 🙂
Thank you!!