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. package com.kb.casting;
  3. class Parent{
  4.     int x=10;
  5.     void show(){
  6.         System.out.println("parent-show");
  7.     }
  9.     void OnlyParentShow(){
  10.         System.out.println("OnlyParentShow");
  11.     }
  12. }
  14. class Child extends Parent{
  15.     int x=20;
  16.     void show(){
  17.         System.out.println("child-show");
  18.     }
  19.     void OnlyChildShow(){
  20.         System.out.println("OnlyChildShow");
  21.     }
  22. }
  24. public class ParentChild {
  26.     public static void main(String[] args) {
  27.         Parent p = new Child();
  28.         p.show();
  29.         p.OnlyParentShow();
  30.         System.out.println(p.x);
  31.     }
  32. }
package com.kb.casting;

class Parent{
	int x=10;
	void show(){
	void OnlyParentShow(){

class Child extends Parent{
	int x=20;
	void show(){
	void OnlyChildShow(){

public class ParentChild {

	public static void main(String[] args) {
		Parent p = new Child();

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.

  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. Parent p = new Child();
  2. 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