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

Consider the following classes: public class Frodo extends Bilbof public void me

ID: 3881153 • Letter: C

Question

Consider the following classes: public class Frodo extends Bilbof public void method10 System.out.printin"Frodo 1 super.methodIO public void method30 System.out println Frodo 3") public class Gandalf public void methodI0 Systemout.printin"GandalfI": public void method20 System.out.println"Gandalf2"): methodIO public class Bilbo extends Gandalf public void methodi0 System.out.,println"Bilbo 1); public class Gollum extends Gandalf public void method30 f System.out.printin"Gollum 3"); Suppose the following variables are defined Gandalfvarl = new Frodo; Gandalf var2 -new Bilbo0: Gandalfvar3 new Gandalf0 Object var4 new Bilbo0: Bilbo var5 new Frodo0: Object var6-new GollumO: var1.method1); var2.method1); var3.method1); var4.method1);

Explanation / Answer

In one java class only one class must me public so we have to remove public keywoord for reamaining classes except the class containd main method.

class Fr extends Bib
{
public void method1()
{
System.out.println("Fr 1");
super.method1();
}
public void method3()
{
System.out.println("Fr 3");
}
}
class Gand{

public void method1()
{
System.out.println("Gand 1");
}
public void method2()
{
System.out.println("Gand 2");
method1();
}
}
class Bib extends Gand{

public void method1()
{
System.out.println("Bibo 1");
}
}
class Gol extends Gand{
public void method3()
{
System.out.println("Gol 3");
}
}

public class Ale
{
public static void main(String args[])
{
Gand var1 = new Fr();
Gand var2 = new Bib();
Gand var3 = new Gand();
//Object var4 = new Bib(); ---> this is not valid so write it as  Bib var4 = new Bib();

Bib var4 = new Bib();
Bib var5 = new Fr();
//Object var6 = new Gol();   ---> this is not valid so write it as Gol var6 = new Gol();

Gol var6 = new Gol();
var1.method1();
var2.method1();
var3.method1();
//var4.method1();
var5.method1();
//var6.method1();
var1.method2();
var2.method2();
var3.method2();
//var4.method2();
var5.method2();

//var6.method2();


//((Bib) var1).method3();
((Gand) var1).method2();
//((Fr) var4).method2(); --> Bib cannot be cast to Fr
((Gand) var6).method2();
((Gand) var4).method1();   
// ((Fr) var6).method3(); --> Gol cannot be converted to Fr   
// ((Fr) var3).method3(); --> Gand cannot be cast to Fr   
((Fr) var5).method3();
}
}

Output:

Fr 1
Bibo 1
Bibo 1
Gand 1
Fr 1
Bibo 1
Gand 2
Fr 1
Bibo 1
Gand 2
Bibo 1
Gand 2
Gand 1
Gand 2
Fr 1
Bibo 1
Gand 2
Fr 1
Bibo 1
Gand 2
Gand 1
Bibo 1
Fr 3

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