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 :

  1. class A{
  3.   }
  4. class B extend A{
  6.   }
class A{
class B extend A{

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

  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();

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 :

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

  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’s 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.

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

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