PUB ID

Wednesday, 25 November 2015

what is Upcasting and Downcasting in Java

UpCasting
     The Process of Storing subclass object reference into superclass reference variable is called upcasting.

For Example
        Parent p=new Child(); // upcasting

DownCasting 
             downcasting is explicitly type conversion
  The process  of retrieving the subclass object reference from superclass reference variable ,and storing it into same subclass referenced variable is called downcasting.

For Example
        Parent p=new Child(); // upcasting
        Child c=(Child)p; // downcasting
For performing downcasting The Source type must have IS-A relationship with Destination type

Example Programe
interface A
{
  void display();
}
class B implements A
{
    public void display() {
        System.out.println("Am in class B");
    }
 
}
class C extends B
{
    @Override
    public void display() {
        System.out.println("Am in class C");
    }
}
- See more at: http://www.codemiles.com/java/difference-between-upcasting-and-downcasting-t7627.html#sthash.wIkUQTlE.dpuf

 interface A
{
  void display();
}
class B implements A
{
    public void display() {
        System.out.println("Am in class B");
    }

}
class C extends B
{
    @Override
    public void display() {
        System.out.println("Am in class C");
    }
}


interface A
{
  void display();
}
class B implements A
{
    public void display() {
        System.out.println("Am in class B");
    }
 
}
class C extends B
{
    @Override
    public void display() {
        System.out.println("Am in class C");
    }
}
- See more at: http://www.codemiles.com/java/difference-between-upcasting-and-downcasting-t7627.html#sthash.wIkUQTlE.dpuf
 Applying upcasting

public static void main(String[] args) {

        // Upcasting from subclass to super class.
        A aRef=new C();

        aRef.display();//Am in class C
      }


Applying Downcasting

public static void main(String[] args) {

        // Upcasting from subclass to super class.
        A aRef=new C();

        aRef.display();//Am in class C
        //Downcasting of reference to subclass reference.
        B bRef=(B) aRef;
        bRef.display();//Am in class C

    } 








interface A
{
  void display();
}
class B implements A
{
    public void display() {
        System.out.println("Am in class B");
    }
 
}
class C extends B
{
    @Override
    public void display() {
        System.out.println("Am in class C");
    }
}
- See more at: http://www.codemiles.com/java/difference-between-upcasting-and-downcasting-t7627.html#sthash.wIkUQTlE.dpuf
interface A
{
  void display();
}
class B implements A
{
    public void display() {
        System.out.println("Am in class B");
    }
 
}
class C extends B
{
    @Override
    public void display() {
        System.out.println("Am in class C");
    }
}
- See more at: http://www.codemiles.com/java/difference-between-upcasting-and-downcasting-t7627.html#sthash.wIkUQTlE.dpuf

No comments:

Post a Comment