What is Upcasting and Down casting In Java?

In this tutorial, we will learn What is Upcasting and Downcasting In Java? Upcasting which is also known as a generalization or widening is casting in the parent type or from an individual type to a common type whereas down casting means specialization or we can say narrowing means casting to a child type.

Or we can say common type to an individual or specific type.

Upcasting

Upcasting

Most of the time we do not need upcasting, It comes into existence only when we need to use the code of only parent class or we can say when we need only parent class that is situation when we need it.

To get a clear idea of why and how we can use upcasting follow the example and get it clear,

class  Parent{  
   void PrintData() {  
      System.out.println("method of parent class");  
   }  
}  
  
class Child extends Parent {  
   void PrintData() {  
      System.out.println("method of child class");  
   }  
}  
class UpcastingExample{  
   public static void main(String args[]) {  
        
      Parent obj1 = (Parent) new Child();  
      Parent obj2 = (Parent) new Child();   
      obj1.PrintData();  
      obj2.PrintData();  
   }  
}

 

Here we access some specific functions and variables of the child class we could not access all the data members of the child class.

So here comes the requirement of just the opposite of it.

Down Casting

Here we provide a reference of the parent class to a child class object and it helps in accessing all the elements of the child class as it is an object of the child class and it can also access the elements of the parent class as it has the reference of it. And moreover, a child class object can access the elements of the parent class.

To learn more and get a clear idea follow the example given below.

class Parent {   
    String name;   
    
    // A method which prints the data of the parent class   
    void showMessage()   
    {   
        System.out.println("Parent method is called");   
    }   
}   
    
// Child class   
class Child extends Parent {   
    int age;   
    
    // Performing overriding  
    @Override  
    void showMessage()   
    {   
        System.out.println("Child method is called");   
    }   
}   
    
public class Downcasting{  
    
    public static void main(String[] args)   
    {   
        Parent p = new Child();  
        p.name = "Shubham";  
          
        // Performing Downcasting Implicitly   
        //Child c = new Parent(); // it gives compile-time error   
          
        // Performing Downcasting Explicitly   
        Child c = (Child)p;   
    
        c.age = 18;   
        System.out.println(c.name);   
        System.out.println(c.age);   
        c.showMessage();   
    }   
}

Learn more about Upcasting and downcast: Casting of data types. as upcasting down casting

And also visit Java Tutorials   To learn more about java solutions and other concepts of java, And to learn more about python visit Python’s list: What is the difference Between methods Append and Extend?.

Leave a Comment

%d bloggers like this: