Casting

There are 2 types of casting..

        1.Up casting

        2.Down casting



Up Casting

        Primitive data types..

1. byte

2. char

3. short  

4. int

5. long

6. float

7. double

8. boolen

Integer (Hierarchy).                          Floating (Hierarchy)

1. long                                                  1. Double

2. int                                                     2.  Float

3. short

4. char

5. byte                                                 

Ex : 1 (Integer)

int a = 5 ;

long b = 7 ;

a + b = 👍

Ex : 2 (Floating)

Float c = 7.8 ;

Double d = 5 ;

c + d 👍 

★ Only primitive types of the same type can deal with each other. If the types are different, do not deal directly. The younger person cannot assign to the older person. The older person can assign the younger person.  It is up casting.

           A a = new B () ; 👍

           B b = new A () ; 👎

Down Casting.

★ Converting the sub class object created from super class reference (variable) [A a = new B ();] back to sub class reference (variable) is called Down casting.

            (Sub class reference)

                  ↓

                  B  A     =        a ;

                       ↑                ↓  

(Sub class variable)       (Super class variable)

B b = A ; 👎

B b = (B) a ; 👍

Ex : 

Class A {

} Class B extends A {

}







Comments