What is Upcasting and Downcasting in Java

What is Upcasting and Downcasting in Java

What is Upcasting and Downcasting in Java

 

What is Up Casting and Down Casting in Java,in this article i will step through a sample program and explain when you need to use Up casting and when you need to use Down casting .

In this Sample Program i have created 3 Class

  1. robot  class -> Which is the super Class
  2. terminator_X_1 -> Which is the Child Class inherit the robot Class
  3. acessBot Class -> Which Access the robot and terminator_X_1 class

What is Up casting ?

Up Casting  is used , when we want to access Function or information in the Super Class. In this Case the Super Class is robot

What is Down Casting ?

Down  Casting  is used , when we want to access specific behavior in the child Class.

 

Robot Class

 

package updowncast;

public class robot {
	
	public void start(){
		
		System.out.println("Robot Start");
	}
	
    public void recalibrate(){
		
		System.out.println("Robot recalibrate");
	}
    public void move (){
		
		System.out.println("Robot Moving");
	}
	

}



 

Terminator_X_1 Class

 


package updowncast;

public class terminator_X_1 extends robot {
	
   public void start(){
		
		System.out.println("Robot Start");
	}
    
   public void shooting(){
	   
	    System.out.println("Terminator is Shooting");
	   
   }

}


 

accessBot Class

 

package updowncast;

public class acessBot {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//robot robot_1 = new robot();
		terminator_X_1 Arnold = new terminator_X_1();
		
		
		//Up casting
		
		robot robot_1 = Arnold;
		robot_1.recalibrate();
		
		// Down Casting
		
		robot robot_2 = new terminator_X_1();
		terminator_X_1 genesis = (terminator_X_1)robot_2;
		genesis.shooting();
		
		
	
	}

}

 

Program Explanation 

  1. First , I have created a super class called Robot .
  2. In the Robot Class i have created 3 Method start() recalibrate () move()
  3. Then i created the child class terminator_X_1 which inherit the robot super class
  4. In the terminator_X_1 class i have created 2 Method start() and shoot()
  5. Then I have created a class called accessBot to access the robot and terminator_X_1 class
  6. Up casting
  7. I created a new terminator_X_1 called Arnold
  8. I cast Arnold into the new robot Object robot_2
  9. Then I access the robot class recalibrate() to re calibrate Arnold
  10. Why is that so ?
  11. You donot want Arnold to access / and know how to recalibrate himself right!!
  12. Down Casting
  13. Then i created an object called robot_2
  14. robot_2 is a new terminator_X_1
  15. terminator_X_1 genesis = (terminator_X_1)robot_2; this statement is asking ? So now robot_2 is a terminator_X_1 type and your new terminator_X_1 object is named genesis
  16. /lastly i access the terminator_X_1 method ” genesis.shooting()
  17. Chcek out Numerical Casting in Java here

 

Leave a Reply

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

five × 4 =