Class Inheritance Program Output265
What will be the output of the following program?
class ClassInheritance
{ public static void main(String s[])
{
A a = new A();
a.i = 4;
B b = new B();
b.i = 10;
b.j = 20;
a = b;
System.out.println("i = " + a.i);
}
}
class A
{
int i;
}
class B extends A
{
int j;
}
A.
|
i = 4
|
B.
|
Compilation Error - since object b can not be assigned to object a
|
C.
|
i = a.i
|
D.
|
i = 10
|
Topic:
Java Class Inheritance
Is-A Relationship In Java
Assigning Sub Class Object To Super Class Reference In Java
If you need explanation Read this topic
If you need explanation Read this topic
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 d.
obj b assigned to a.
so a.i value 10.
Posted by Maheshwari Natarajan 2014-09-11 12:32:02
answer b.
cant assign 1class obj directly to anothr class obj.
compilation err
Posted by NagaRaj Nataraj 2014-09-11 12:34:32
Ans:D after assigning "a" with b . a contains b address hence b.i will be a.i
Posted by Indrasena Reddy 2014-09-11 12:35:06
Ans D
Posted by Pranavi Prakash 2014-09-11 12:36:56
ans a.
obj b assg to obj a.
bt while printing it print 4 .
class A local value
Posted by Anand Kumar 2014-09-11 12:39:12
D
Posted by Pankaj Arora 2014-09-11 12:55:17
d
Posted by Abhishek Kumar 2014-09-11 13:27:15
d. because a=b doesnt affect the value of a becase object cant be assigned directly...
Posted by Shubham Bansal 2014-09-11 15:11:51
Congratulations Maheshwari Natarajan. You are this dose winner. We will send you the link using which you can claim your recharge.
Posted by Merit Campus 2014-09-12 04:03:33