Exercise 8        Name __________________        Score __/19

Modified: 

  1. (2)
    1. Give the smallest possible definition for a class named Child.



       
    2. Give the smallest possible definition for a class named Child that inherits from the class named Parent.



       
  2. (3) Give the inheritance hierarchy diagram for the following:
     

    class A { }
    class B extends A { }
    class C extends A { }
    class D extends B { }
    class E extends C { }
    class F extends E { }
    class G extends D { }
    class H extends E { }
     

     

  3. (6) Rewrite the following classes to use inheritance and remove duplication. Each class should still perform the same.
A C
public class A
{
   private String S;
   private int I;

   public A (String theS, int theI)
   {
      S = theS;
      I = theI;
   }

   public void ap ()
   {
      System.out.println( S );
      System.out.println( I );
   }
}

public class C
{
   private String S;
   private int I;

   public C (String theS, int theI)
   {
      S = theS;
      I = theI;
   }

   public void cp ()
   {
      System.out.println( S );
      System.out.println( I );
   }
}  

4. (8) Rewrite the following classes to use inheritance and remove duplication. Each class should still perform the same.

A B C
public class A
{
 private String S;
 private int I;
 private double D;

 public A (String theS, int theI, double theD)
 {
      S = theS;
      I = theI;
      D = theD;
 }

 public void ap ()
 {
      System.out.println( I );
      System.out.println( S );
      System.out.println( D );
 }
}

public class B
{
 private String S;
 private double D;

 public B (String theS, double theD)
 {
      S = theS;
      D = theD;
 }


 

 public void bp ()
 {
      System.out.println( S );
      System.out.println( D );
 }
}  

public class C
{
 private String S;
 private int I;

 public C (String theS, int theI)
 {
      S = theS;
      I = theI;
 }

 public void cp ()
 {
      System.out.println( S );
      System.out.println( I );
 }
}