Non-Primitive-Typecasting.java

Rohan055 · updated April 16, 2025
// Non- Primitive Typecasting

/*The process of Converting One type of Non-Primitive data into another type of Non-Primitive data is known as Non primitive typecasting

It has two Types
(1) Up Casting
    -> The process of converting Child type of reference into parent type of reference is known as upcasting
    -> In Upcasting it act as a generalized container

    
(2) Down Casting
    -> The process of Converting Parent type to child type 
    -> it is done by the compiler emplicitely
    -> It is used to achieve generalization
    -> it means in a parent type of reference we can store any of its child reference
    -> if we try to convert one reference type into another which doesn't have any is a relationship 

 Advantage of upcasting
  -> It is used to achieve generalization
  Example
   package nonPrimitiveTypecasting;

   class Cab {
   
   }

   class Minicab extends Cab {
   
   }

   class Primecab extends Cab {
   
   }

   class Suv extends Cab {
   
   }

   public class main {
    public static void main (String [] args) {
    System.out.println("Enter your Choice")
     int choice=2;
     Cab b=null;

     switch(choice) {
      case1: b = new  MiniCab();
           break;

       case1: b = new  Suv();
           break;
     }
    }
   }
    -> It used to achieve loose coupling 

    Drawback of upcasting
    1) In upcasting scenerio with the help of parent type reference
    2) We cannot access the member of child type

    Example
    class fruit {
     void getHealth() {
      System.out.println(getting Healthy.....);
     }
    }

    Downcasting
    1) The process of converting parent type of reference into child type is known as Downcasting
    2) it is not done by the complier implicetly as programmer we have to do it explicetly with the help of typecast operator
    3) With the help of downcasting we can overcome the drawback of downcasting
    Note:
    In Downcasted scenario(downcasting) during downcating sometime we can runtime problem (exception ClassCastException)

    ClassCastException
    -> During the downcasting if parent type of reference does'nt have any instance of child type and we try to do downcasting then we get classcastexception
    (If without upcasting , we try to do downcasting we get ClassCastException)

    class Tv {
       void play() {
          System.out.println("start playing");
       }
    }

    class SonyTv extends Tv {
      void getColour () {
       System.out.println("getting mix colour");
      }
    }

    class LgTv extends Tv {
      void useInternet () {
       System.out.println("using internet");
      }
    }

    public class main {
      public static void main (String[] args) {
       SonyTv t1 = new SonyTv();
       Tv t = s; // upcasting

       LgTv t = (Lgtv) s; // ClassCastException

       Sonytv t = (SonyTv) t; 
        
       
       
      }
      
    }

    
*/
     
    
class Fruit {
    
}

class Apple extends Fruit {
    
}

public class Main {
    public static void main(String[] args) {
        Apple a = new Apple ();
        Fruit f = a; // Upcasting
    }
}
Output

Comments

Please sign up or log in to contribute to the discussion.