Menu
Topics Index
...
`

Type Casting Program Output270


If you need explanation Read this topic

If you need Answer Take test on this topic

Type Casting Program Output270
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 = 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. Compilation 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 D...here we have the Concept of the Inheritance where child class will inherit the properties of the Parent class..and Parent class reference can refer the Child class Object..in order to achieve Dynamic method dispatch..
Execution:

Here we have class A as Parent class and Class b as Child class..class B Extending the Properties of the Class A...so now class B will have the i and j..in the main() we are creating the instance for class A so memory is allocated for the variable i in the class A...and assigned with the 21 next we are creating the instance for the class B and assigning the values 43 and 25...next we create the reference for the class A and Copied class B Object into the Parent class reference..in java Parent class Reference can refer Child class Object...next we create reference for class B and try to copying Parent class object a2 so here compiler will raise an error saying incompatible types

Posted by Uday Kumar    2015-01-26 03:53:47


d.

Posted by Shubham Bansal    2015-01-26 04:46:42


ans is D.
In java parent class can refer to the child class objects but child can't refer the parent objects.
here class i s parent class with int varible i.
And class B is child extened the parent class A so now B class have two variables i and j.
in main() object for class A is created and assigned a value to i i.e, 21.
object for class B is created and asigned a value for i and j i.e, i=43 & j=25.

now new object or instance is created and assigned instance of class B, here parent class refer to the child class.
In line x instace for class B is created and assigned a parent class object child class will not handle parent class.
So error rises here.

Posted by Ananthoju Arun Chary    2015-01-26 05:31:43


This dose is now closed and the winners are Uday Kumar,  for 'First Correct Comment', Uday Kumar, Ananthoju Arun Chary,  for 'Best Comment' and Uday Kumar for the 'Popular Comment'. The 'lucky liker' is Urvashi Arya. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-01-27 07:26:21


© meritcampus 2019

All Rights Reserved.

Open In App