Wednesday, 23 May 2018





                    Upcasting And DownCasting in java


There are two types of primitive type casting
1. Implicit type casting
2. Explicit type casting

Implicit type casting:-
·         Compiler is the responsible to perform this type casting
·         This type casting is required whenever we are assigning smaller data type value to the bigger data type variable
·         It is also known as "widening or upcasting"
·         No loss of information in this type casting.




public class Test{
   public static void main(String[] args)  {
      int i = 100;
      long l = i; //no explicit type casting required 
      float f = l; //no explicit type casting required 
      System.out.println("Int value "+i);
      System.out.println("Long value "+l);
      System.out.println("Float value "+f);
   }
}

Output :
Int value 100
Long value 100
Float value 100.0







Explicit  type casting:-
·          Programmer is responsible to perform this type casting

·         It is required when ever we are assigning bigger data type value to the smaller data type value

·         It is also known as "Narrowing or down casting"

·         There may be a chance of loss of information in this type casting






public class Test{
   public static void main(String[] args)  {
       double d = 100.04; 
       long l = (long)d;  //explicit type casting required 
       int i = (int)l; //explicit type casting required 
       System.out.println("Double value "+d);
       System.out.println("Long value "+l);
       System.out.println("Int value "+i); 
   } 
}

Output :
Double value 100.04
Long value 100
Int value 100
 Example:-

Upcasting is like putting 10 kg rice in a 20 kg container  there's enough space to put..so compiler does it automatically

Downcasting is vice versa...but in this case the compiler needs to know how much space it has available so it can put only what is possible..explicit cast does that

This example applies to primitive types.. for user defined types inheritance comes into picture...


UpCast -> From lower level class type to upper level class type
Downcast -> From upper level class type to lover level class type


For example, all cars are vehicles but not all vehicles are cars
Car is a vehicle.. is a relationship exists

Vehicle v = new Car( );//Upcasting done automatically

Car c = (Car) v ( );// Downcasting needs to be explicit..





package javaresearch;

public class Vehicle {
           
            public void engineStart()
            {
                        System.out.println("engine start");
            }

            public void engineStop()
            {
                        System.out.println("engine stop");
            }
           
           
           
           
}






package javaresearch;

public class Car extends Vehicle{
           
           
            public void engineStart()//overrided method
            {
                        System.out.println("car engine start");
            }

            public void engineStop()//overided method
            {
                        System.out.println("car engine stop");
            }
           
            public void carName()//car class specific  method
            {
                        System.out.println("Car Model="+"BMW");
            }
            public void carColour()//car class specific method
            {
                        System.out.println("Car Colour="+"blue");
            }
           

            public static void main(String[] args) {
                        Vehicle v=new Car();//upcasting
               //  i want to call car specific method carName() and carColur()---downcasting is required
                       Car c=(Car)v;//downcasting
                        c.carName();//bmw
                        c.carColour();//blue
                        v.engineStart();//overrided method call(car engine start)
                        v.engineStop();//overrided method call(car engine stop)
           
            }

}

o/p:-------------------- Car Model=BMWCar Colour=bluecar engine startcar engine stop


package javaresearch;

public class A {
           
            int x=100;
            static int y=200;
            public void m1()
            {
                        System.out.println(" super class non static m1 method");
            }
           
            public static  void m2()
            {
                        System.out.println(" super class static m2 method");
            }

}


package javaresearch;

public class B extends A
{
                int x=300;
                static int y=400;
                public void m1()
                {
                                System.out.println(" sub class non static m1 method");
                }
               
                public static  void m2()
                {
                                System.out.println(" sub class  static m2 method ");
                }
                public void m3()
                {
                                System.out.println(" sub class non static m3 method");
                }
               
                public static void m4()
                {
                                System.out.println(" sub class static m4 method");
                }
               
               




                            public static void main(String[] args) {
                                A a=new A();
                                System.out.println(a.x);
                                System.out.println(a.y);
                                a.m1();
                                a.m2();
                                System.out.println("-------------------------------");
                                B b=new B();
                                System.out.println(b.x);
                                System.out.println(b.y);
                                b.m1();
                                b.m2();
                                b.m3();
                                b.m4();
                                System.out.println("-------------------------------");
                                A a1=new B();//upcasting
                                System.out.println(a1.x);
                                System.out.println(a1.y);
                               a1.m1();
                               a1.m2();
                              System.out.println("---------------------------------");
                              B bb = (B)a1;//downcasting
                              System.out.println(bb.x);
                              System.out.println(bb.y);
                              bb.m1();
                             bb.m2();
                             bb.m3();
                             bb.m4();
                               
                               
                }


}


o/p:------------

100
200
 super class non static m1 method
 super class static m2 method
-------------------------------
300
400
 sub class non static m1 method
 sub class  static m2 method
 sub class non static m3 method
 sub class static m4 method
-------------------------------
100
200
 sub class non static m1 method
 super class static m2 method
---------------------------------
300
400
 sub class non static m1 method
 sub class  static m2 method
 sub class non static m3 method
 sub class static m4 method



No comments:

Post a Comment