Login with  Log in with facebook
Hiring Manager? SIGN UP HERE
0

class A{ }
class B extends A{ }
class C extends A{ }
class Test{
public static void main(String[] args){
A a = new A();
B b = new B();
C c = new C();
// assignment here
}
}

Given the above sample code, which one of the following assignments is legal?

Mukesh Kumar
08/20/2011 at 04:47
2 Answers
1
0

Hi Mukesh,

your question does not seem to be complete. 

Any assignment which calls a class by it's own type or supertype would be valid.

Rishi Yadav
08/20/2011 at 13:21

2
0

any of the following would be valid at compile time:

a = b;
a = c;
c = (C) a;
b = (B) a;

but I must say that at run-time, if an object of type B has been assigned to "a" and then "a" is assigned to "c", you might get an Exception when using c.

Gerardo Robles
08/29/2011 at 11:12

If you want to post any answer to this forum then you need to log in.
Schedule a Demo

Schedule a Demo with us

Name *
Email *
Phone *
Company *
Details