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

[JAVA] Study Guide Problems 6. Inheritance and Polymorphism Consider the followi

ID: 3834453 • Letter: #

Question

[JAVA] Study Guide Problems

6. Inheritance and Polymorphism

Consider the following classes:

public class Blue extends Green
{
public void one()
{
System.out.println("Blue 1");
super.one();
}
}

public class Red extends Yellow
{
public void one()
{
super.one();
System.out.println("Red 1");
}

public void two()
{
System.out.println("Red 2");
super.two();
}
}

public class Yellow extends Blue
{
public void two()
{
System.out.println("Yellow 2");
}

public void three()
{
two();
System.out.println("Yellow 3");
}
}

public class Green
{
public void one()
{
System.out.println("Green 1");
}

public void three()
{
System.out.println("Green 3");
}
}

The following variables are defined:
Green var1 = new Blue();
Green var2 = new Red();
Blue var3 = new Yellow();
Object var4 = new Green();

In the table below, indicate in the right-hand column the output produced by the statement in the left-hand column. If the statement produces more than one line of output, indicate the line breaks with slashes as in "a / b / c" to indicate three lines of output with "a" followed by "b" followed by "c". If the statement causes an error, fill in the right-hand column with the phrase "error" to indicate this.

Statement Output

var1.one();    ________________________

var1.two();   ________________________

var1.three();   ________________________

var2.one();    ________________________

var2.two();   ________________________

var2.three();   ________________________

var3.two();   ________________________

var3.three();   ________________________

var4.one();   ________________________

((Blue) var1).one();   ________________________

((Yellow) var1).two();   ________________________

((Red) var2).three();   ________________________

((Yellow) var2).two();   ________________________

((Green) var4).three();   ________________________

((Yellow) var4).two();    ________________________

Link to Source PDF:

https://www.dropbox.com/s/1lrjfjs38plzska/Practice_Final_Exam.pdf?dl=0

Explanation / Answer

Please find my answer:

Statement Output
var1.one(); Blue 1/Green 1

var1.two(); error

var1.three(); Green 3

var2.one(); Blue 1/Green 1/Red 1

var2.two(); error

var2.three(); Red 2/Yellow 2/Yellow 3

var3.two(); error
var3.three(); Yellow 2/Yellow 3

var4.one();    error

((Blue) var1).one(); Blue 1/Green 1

((Yellow) var1).two(); error

((Red) var2).three(); Red 2/Yellow 2/Yellow 3

((Yellow) var2).two(); Red 2/Yellow 2

((Green) var4).three(); Green 3

((Yellow) var4).two(); error

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