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

4. [2 points] The following problems all involve three distinct classes, A, B, a

ID: 3756738 • Letter: 4

Question

4. [2 points] The following problems all involve three distinct classes, A, B, and C a. Suppose that the following code compiles and runs without any exceptions. What can you say about the relationship between the classes A, B, and C? A x1new CO B x2 = x1; b. Now suppose the code in part (a) fails to compile, but if we remove the second line it compiles and runs without any exceptions. What can you say about the relationship between the classes A, B, and C? c. Now suppose that the code in (a) fails to compile, and when we remove the second and fourth lines it compiles, but causes a ClassCastException at runtime. What can you say about the relationship between the classes A, B, and C?

Explanation / Answer

a)The following code will only compile if

public class B {

}

public class A extends B{

}

public class C extends A{

}

b)If we remove the second line to make the code compile the relationship between A,B and C should be

public class A{

}

public class B extends A{

}

public class C extends B{

}

c)When we remove the second and fourth lines to get class cast exception the relationship should be

public class A{

}

public class B extends C{

}

public class C extends A{

}

we get classcastexception if we try to cast a parent class to child class.object.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote