Exam 1z0-819 All QuestionsBrowse all questions from this exam
Question 188

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.)

    Correct Answer: B, D

    To make this code compile correctly, the return statement must adhere to the generic restrictions specified. The method returns a House with a type parameter that is a superclass of Dog. Therefore, valid return types are ones where the type parameter is either Dog itself or any supertype of Dog, which includes Animal. Hence, 'return new House<Dog>();' and 'return new House<>();' would be appropriate.

Discussion
d7bb0b2Option: A

House<Dog>, House<Animal> , House<Object> , House(implicit object) because they are super class of Dog

ASPushkin

House<Object> is not allowed class Animal{} class Dog extends Animal{} class Predog extends Dog{} class House<A extends Animal> { public House<? super Dog> build(A a) { return .. } } generic method return parameter : House <? super Dog> has super type realtionship with House<Dog>, House<Animal>, House<Object>, House<> type class parameter : <A extends Animal> following types are within bounds of type-varaible A. And it allows : House<Dog>, House<Animal>, House<Predog>, House<> so intersection is House<Dog>, House<Animal>, House<>

ASPushkin

House<Object> is not allowed. Checked

d7bb0b2Options: CE

only can return : House<Dog>, House<Animal> , House<Object> , House(implicit object) because they are super class of Dog

d7bb0b2

The answered is same for all options