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.

Ex: class A{}
class B extend A{}

A a1 = new B();
this operation is a upcasting. and it happens automatically.no need to do anything explicitly.

here we can call the methods defined/declared in A but during runtime it will call class B’s 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.

lets see a program example and predict the o/p.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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);
	}
}

output is

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 o/p , 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.
ex:

1
Child c = new Parent();
Child c = new Parent();

for 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

1
2
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 is putting parent’s reference variable holding child’s object to child’s reference variable.

it is almost equivalent to

1
Child c = new Child();
Child c = new Child();

but indirectly using parent’s reference variable as temp.

so 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.

About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on