Which return statements compile for a generic House method?
Given: class Animal { } class Dog extends Animal { } class Petdog extends Dog { } and Which two statements inserted independently on line 3 will make this code compile? (Choose two.) - 
Community Votes
100% of anonymous learners picked answer A. Votes are pick records left by other test-takers — they are not the verified answer.
Community Insight
It tests generic invariance on a method return type: an instantiation that names a supertype (Animal/Object) or subtype (Petdog) of Dog is not assignable to House<Dog> without a wildcard, and the raw/diamond House form is the trap many candidates forget is still legal.
This 1Z0-819 generics question supplies the hierarchy Animal → Dog → Petdog and asks which two return statements make the method compile when its declared return type is the generic House. Only the explicit House<Dog> instantiation and the raw/diamond House form satisfy the declared type; House<Animal>, House<Object> and House<Petdog> all fail because Java generics are invariant.
Assuming covariance — picking new House<Animal>() or new House<Object>() because Animal is Dog's supertype, or new House<Petdog>() because Petdog is Dog's subtype. Generics are invariant, so only the exact House<Dog> parameterization (plus the raw/diamond House constructor call) is assignable to the House<Dog> return type.
Community Discussion (3 comments)
Comments & Corrections
No comments yet — spotted an error or have a note? Share it below.
Expert Analysis
Why the Answer Is Correct
The fragment combines the generic class House<T> with the chain Animal → Dog → Petdog and a method whose return type fixes one concrete parameterization of House (the Dog case). Under Java's invariance rule, the only directly assignable instantiation is new House<Dog>, because Dog is exactly the type argument the compiler expects — there is no wildcard in the declared type to widen or narrow it. The second statement that compiles is the unparameterized constructor new House (raw, or its diamond equivalent inferred from the target type), which the compiler converts to the required House<Dog> with at most an unchecked-conversion warning. So the two working statements are the exact House<Dog> instantiation and the raw/generic House form, which is what the answer letters (A and E) represent.Why the Other Options Are Wrong
Returning new House<Animal> or new House<Object> relies on the false assumption that House is covariant: Animal and Object are supertypes of Dog, but House<Animal> and House<Object> are not subtypes of House<Dog>, so the return statement is rejected with “incompatible types”. Returning new House<Petdog> fails in the opposite direction — Petdog is a subtype of Dog, and invariance blocks that conversion too. None of these compile even though every type in the list is related to Dog by inheritance, which is precisely the invariance error this question is designed to expose. If the declared return type had been the lower-bounded wildcard House<? super Dog>, those supertype instantiations would have been legal — but then far more than two options would compile, which is not what the scenario asks.Community Comment Notes
One learner, d7bb0b2, summarised the legal returns as "House<Dog>, House<Animal>, House<Object>, House(implicit object)" and justified it with "because they are super class of Dog" — reasoning that is correct for a wildcard target such as House<? super Dog>, but not for the concrete House<Dog> return type the method declares. The same commenter also wrote "The answered is same for all options", which explains a lot on this page: the angle-bracket type arguments of the five options were stripped when the item was transcribed, so all five lines render as an identical "return new House;". Read that remark as a warning to always recover the real generic signatures from the original screenshot before applying the supertype rule.Exam Strategy
For generic return/assignment questions, write down the exact declared type first, then test each option against it — an unrelated parameterization never compiles regardless of how the class hierarchy looks. Remember that the raw and diamond constructor calls are accepted by the compiler (an unchecked warning at most), which is usually the hidden second answer in a “choose two” generics item.Official Reference
Exam Strategy
Decode the generic signature before looking at the options: if the target type is exactly House<Dog>, only Dog (or the raw/diamond form) fits; a ? super Dog or ? extends Dog wildcard is the only thing that would let Animal, Object or Petdog in. Then double-check whether the option uses an explicit type argument, the diamond, or a raw type, since the raw/diamond variant is what usually supplies the second correct letter in these two-answer questions.
Frequently Asked Questions
Why can't the method return new House<Animal>() when Animal is Dog's superclass?
Generic types are invariant: House<Animal> is not a subtype of House<Dog>, so the return fails to compile. Supertype substitution only works when the declared type is the wildcard House<? super Dog>.
Does the raw new House() really compile for a House<Dog> return type?
Yes. A raw constructor call is assignable to House<Dog> with just an unchecked-conversion warning, which is why the raw/diamond form is counted as one of the two correct answers.