What is up-casting

What is up-casting

Answer :  Up-casting in java is inheritance relationship between parent and child class. Basically it happens on is a relationship between instance of the class. Below example you will see two class ParentClass.java and ChildClass.java where ChildClass extends ParentClass which means ChildClass is ParentClass but ParentClass is not ChildClass.

Below is possible:

ParentClass parentClass = new ChildClass();
ChildClass childClass=(ChildClass) parentClass


And below is not possible:

ParentClass parentClass2 = new ParentClass ();
ChildClass childClass=(ChildClass) parentClass2 // class cast exception occurs


Please note that in java it is a concept that without upcasting u cannot perform downcasting. Please have class example below:

package com.javahonk.downcasting;

public class ChildClass extends ParentClass {

    public static void main(String[] args) {

	//upcasting - below variable holding a value of type Child
	ParentClass parentClass = new ChildClass();

	if (parentClass instanceof ChildClass) {
	    //Down-casting OK since ParentClass variable is 
	    //currently holding ChildClass instance
	    ChildClass childClass=(ChildClass) parentClass;
	    System.out.println("Object downcasting");



class ParentClass {




Object downcasting


Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *