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

Java: Inheritance and Polymorphism public class Blue extends Green { public void

ID: 3705173 • Letter: J

Question

Java: Inheritance and Polymorphism

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

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

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

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

The following variables are defined:

Green var1 = new Blue();
Green var2 = new Red();
Blue var3 = new Yellow();
Object var4 = new Green();

Indicate the output produced by the statements below. If the statement causes an error, indicate as such.

var1.one();
var1.two();
var1.three();
var2.one();
var2.two();
var2.three();
var3.two();
var4.one();
((Blue) var1).one();
((Yellow) var1).two();
((Red) var2).three();
((Yellow) var2).two();   
((Green) var4).three();
((Yellow) var4).two();

Explanation / Answer

var1.one();
Output :

Blue 1

Green 1
==============================================================


var1.two(); //Error: because two is not defined
==================================================

var1.three();

Output :

Green 3
==============================================================


var2.one();

Blue 1

Green 1

Red 1



==============================================================
var2.two();
Error: methof two() undefined for type gREEN

===========================================================

var2.three();

Red 2

Yellow 2

Yellow 3



======================================================

var3.two();
Error: methof two() undefined for type Blue
==========================================================================
var4.one();
Error: methof two() undefined for type Object
================================================================================
((Blue) var1).one();

Blue 1

Green 1


==============================================
((Yellow) var1).two();
Error: Run time exception, Blue cannot be case to Yellow
================================================================
((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: Run time exception, Green cannot be case to Yellow
======================================================


Thanks, let me know if there is any concern. PLEASE UPVOTE If its helpful

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