Topics Index

Type Casting Program Output271

If you need explanation Read this topic

If you need Answer Take test on this topic

Type Casting Program Output271
What will be the output of the following program?
class TypeCasting
    public static void main(String s[])
        A a = new A();
        a.i = 21;
        B b = new B();
        b.i = 43;
        b.j = 25;

        A a2 = b;

        B b2 = (B) a2; // LINE X

        System.out.println("i = " + b2.i); // LINE Y


class A
    int i;

class B extends A
    int j;

A. i = 21
B. Compilation Error at LINE Y
C. i = 43
D. Runtime Error at LINE X
Topic: Assigning Super Class Reference To A Sub Class Reference In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is we have the inheritance concept where child class will inherit the properties of the Parent class using the extend keyword
here class A has as attribute i and class B extending the class A so the class B now contains the i,j

in the main() we are creating the instance for the class A so that memory is allocated for the variable i and we are assigning i=21
next we are creating the instance for the class B so that memory will allocate for the i n j values are assigned 43 and 25
next we create the reference for the Parent class n class B is object is copied into parent class reference(in JAVA PARENT CLASS REFERENCE CAN REFER CHILD CLASS OBJECT)
next we create thr reference for the class B and we are type casting Parent class Object into child class previuously which is child class Object..
now we are printing the value of i in the b2 so here b2 is pointing the b so i value is 43

Posted by Uday Kumar    2015-01-22 08:40:29

Ans is Option C... Execution starts from main() method after execution of first stmt it will create Object of Class A after that we are initializing the member variable of Class A with value 21  that means i=21 after that we are creating Object of Class B and here Class B extending the Class A if you extend one class to another the sub class will contains all properties of super class so in Class B we will have 2 member variables i and j and now we are initializing those variables as i=43 and j=25.... after that we are creating another reference variable of Class A with variable name as "a2" and we are initializing this variable with object of Class B...                                          here we are assigning sub class object to super class reference variable.........                                            after that we are creating object of Class B by assigning super class object to sub class reference variable but                                                               Normally super class reference variable cannot be given to sub class reference variable so we are type casting this super class variable with sub class  with the stmt B b2=(B)a2;   so in b2 object values of member variables are same as values of Object of Class B bcz we are assigning object  "b" to object object "b2" indirectly so after that it will print value of i as 43 b2.i=43....

Posted by Bhagi Bhagyasri    2015-01-22 09:25:04

This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Bhagi Bhagyasri,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2015-01-23 04:56:13

© meritcampus 2019

All Rights Reserved.

Open In App