Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming language is JAVA: Class D extends class C. Reference d is of type D

ID: 3830775 • Letter: P

Question

Programming language is JAVA:

Class D extends class C. Reference d is of type D and reference c is of type C. Which of the following won't compile?
    a.    c = (C) d;
    b.    d = (D) c;
    c.    c = d;
    d.    d = c
    e.    all of the above will compile.

Assume that c is a reference of type C that refers (at runtime, of course) to an object of type D. Is it ever possible for D to be a superclass of C?
    a.    yes
    b.    no


Assume that c is a reference of type C that refers to an object of type D. What happens with the code below?

    D d = (D) c;   

    a.    runtime error
    b.    runs fine

Assume that c is a reference of type C that refers to an object of type C. If D is a subclass of C, What happens with the code below?    

    D d = (D) c;   

    a.    runtime error  
    b.    runs fine

Assume that c is a reference of type C that refers to an object of type E. If D is a superclass class of E, what happens with the code below?

    D d = (D) c;   

    a.    runtime error  
    b.    runs fine

Explanation / Answer

Answer in BOLD

Class D extends class C. Reference d is of type D and reference c is of type C. Which of the following won't compile?
a. d = c;

Assume that c is a reference of type C that refers (at runtime, of course) to an object of type D. Is it ever possible for D to be a superclass of C?

b. no

Assume that c is a reference of type C that refers to an object of type D. What happens with the code below?
D d = (D) c;   
a.    runtime error
    b.    runs fine

Assume that c is a reference of type C that refers to an object of type C. If D is a subclass of C, What happens with the code below?    
D d = (D) c;   
a.    runtime error  
    b.    runs fine