Exercise 8        Name __________________        Score __/19

Modified: 

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


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

      class Child extends 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 );
   }
}  

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 extends A
{

   public C (String theS, int theI)
   {
      super(theS, theI);
   }

  public void cp ()
  {
     ap();
  }
}  

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 );
 }
}  

 

public class One
{
 private String S;

 public One (String theS)
 {
      S = theS;
 }
 

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

public class B extends One
{
 private double D;

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

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

 

public class C extends One
{
 private int I;

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

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

 

public class A extends B
{
 private int I;

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

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