Citigroup Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

Neither the original code nor the proposed edit will work. The issue is that your object is of type A, and is not of type B. You therefore cannot assign it to a variable that holds an object of type B.

This would work in the other direction: A a = new B() works just fine and doesn't even need any casting. That's because, since B is a subclass of A, any object of type B is also of type A. Since the new instance of B is also of type A, we can assign it to a variable that holds an object of type A.

You might have an easier time understanding this if you replace A and B with Car and Chevy. If you make a new Car, it might not be a Chevy, so putting it in a place reserved for only Chevys is not always correct and won't be allowed. But if you make a new Chevy, it's always a Car, so putting it into a place for Cars is perfectly fine.

At no point has casting entered this discussion yet, so you might be wondering what that's used for. Well, it can't be used to transform any Car into a Chevy. You can't just do, as you did with A and B above, Chevy c = (Chevy) new Car();. Java isn't some kind of magical wizard that can somehow transform a Car into a Chevy if it isn't one already. A Chevy might have additional data associated with it -- where would that data come from when making the conversion? In this case, the operation would result in a ClassCastException, letting you know that the conversion failed because the object isn't of the type you're trying to cast it to.

A cast would only work in a situation where you know a priori that something is of a certain type, but yet the object is currently being stored in a variable whose type is some supertype. For example, let's say we wrote Car c = new Chevy(). We know c is a Chevy, even though we're holding it in a variable for Cars. We could now do Chevy ch = (Chevy) c. When we do this, Java will verify that the conversion we're attempting is valid (that c really is a Chevy and not any other kind of Car) before making the assignment, and a ClassCastException will be thrown if c is not a Chevy as expected.

- eugene.yarovoi April 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Second code will run. Creating object of type A and then downcasting to B, assigning it to type B is completely correct. First code for obvious reasons will not run (compile time problem).

- Shashank June 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Shashank: No. The second code is also incorrect, for the reasons explained in the answer. Try running it.

- Anonymous June 12, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

no ..no... no..

- sunil April 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

In the above code You are assigning a base class object (A's object) to sub class reference (to B) which can not be done.

So in must be downcasted to type B and is to be assigned to B's reference
like B b=(B)new A();

- navya April 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This will compile successful. issue will occur at run time because it is down casting issue
B b = (B)new A();

- Rishi July 09, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

B b=(B)new A() will throw below given exception while execution:

Exception in thread "main" java.lang.ClassCastException: A cannot be cast to B

- Aryan Gupta April 20, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In Java 8, it shows as Compilation error. Type Mismatch cannot Convert from A to B

- Suresh May 11, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More