OOP IN JAVA ( UPCASTING & DOWNCASTING)

  UPCASTING AND DOWNCASTING 





Upcasting

Basically ,Upcasting gives us the flexibility to access the super class members but it is not possible to access all the sub class members using this feature. And also we use upcasting to combine something in the super Class(Objects, Variable and methods) with (objects, Variable and methods) of the sub class.

    class A{ 
}
class B extends  A{
}
class Test{
A a = new A();
B b = new B();
}

b= a ( Totally Wrong)

a=b ( correct)


How to create Upcasting Method Process ;


                                             A a = new B () 


  A - Super class Object Reference 

  a - Reference variable

 B- Sub class Object Reference   


Note : in upcasting, you can't access from lower power to up power. you can access from up power to lower power power

int x = 5;
long y=5;

x=y ; (wrong )
y=x ( correct)

double a = 7.474; 

float c = 25.36f;

a=c (Correct)

c=a (Wrong)


    DOWNCASTING 

When subclass reference refers to super class object, it is called  downcasting in java. when sub class type is converted into super class type, it is called downcasting.




Converting the subclass object placed in a superclass variable back to the subclass variable

A a= new B () ( upcasting )

Passing the super class variable to the sub class variable can be done if the sub class class object is used as a parameter.

B b = (B) a;   ( downcasting)


                                                               (B) =  A 


  Thank you 


 

 

 

Comments

Popular posts from this blog

DEMYSTIFYING THE POWER OF x86 ARCHITECTURE

A GUID TO BEACOMING A SOFTWARE ENGINEER

JAVA LAMBDA EXPRESSION