5.6 Exercise 4 : Brain Bender 

Previous Index Next 


 Write down the output of the following Java application:
public class BrainBender
 {
  public static void main(String[] args)
   {
    BrainBender b = new BrainBender();
 
    b.bend();
   }
 
  protected void bend()
   {
    A c = new A();
    B b = new B();
    C a = new C();
 
    a.say("Hello");
    b.say("Hello");
    c.say("Hello");
 
    int x = 10;
 
    a.say(x);
    c.say(x);
 
    double z = 1.3;
 
    c.say((int)z);
    b.say(z);
    a.say((int)z);
  }
 }
 
 
class A
 {
  public void say(String aMessage)
   {
    System.out.println( aMessage );
   }
 
  public void say(int a)
   {
    say("The integer is :" + a);
   }
 }
 
class B extends C
 {
  public void say(double c)
   {
    say("The double is :" + c);
   }
 }
class C extends A
 {  
  public void say(String aMessage)
   {
    super.say( "C Says: " +aMessage );
   }
 }

1