COP 2800 (Java Programming I) Homework Assignment #5

 

Due: by the start of class on the date shown on the syllabus

Description:

Answer the following questions as briefly (but completely) as possible:

  1. What is the printout of running the class C (via the command “java C”) in the code below (saved in C.java)?  (And, is that what you expected?)
    class A {
      public A () {
        System.out.println( "A's no-arg constructor is invoked" );
      }
    }
    
    class B extends A {
    }
    
    public class C {
      public static void main ( String [] args ) {
        B b = new B();
      }
    }
  2. What problem do you expect to see if compiling the program below?  What was the error(s), if any, actually produced?
    class A {
      public A ( int x ) {
      }
    }
    
    class B extends A {
      public B () {
      }
    }
    
    public class C {
      public static void main ( String [] args ) {
        B b = new B();
      }
    }
  3. Which of the follow statements are true?  Which are false?  Explain why.
    1. A subclass is a subset of a superclass.
    2. When invoking a constructor from a subcass, its superclass's no-arg constructor is always invoked.
    3. You can override a private method defined in a superclass.
    4. You can override a static method defined in a superclass.
  4. What is the benefit of using the @Override annotation?
    1. Show the output of the following program:
      public class Test {
         public static void main ( String [] args ) {
            A a = new A( 3 );
         }
      }
      
      class A extends B {
         public A ( int t ) {
            System.out.println( "A's constructor is invoked" );
         }
      }
      
      class B {
         public B () {
            System.out.println( "B's constructor is invoked" );
         }
      }
    2. Is the no-arg constructor of Object invoked when new A(3) is invoked?
  5. Indicate true or false for the follow statements:
    1. You can always successfully cast an instance of a subclass to a superclass.
    2. You can always successfully cast an instance of a superclass to a subclass.
  6. What's wrong with the following code?
    public class Test {
       public static void main ( String [] args ) {
          Object fruit = new Fruit();
          Object apple = (Apple) fruit;
       }
    }
    
    class Apple extends Fruit {
    }
    
    class Fruit {
    }
  7. When overriding the equals method, a common mistake is mistyping its signature in the subclass.  For example, the equals method is incorrectly written as equals(Circle circle) as shown in (a) below.  It should be written as equals(Object circle), as shown in (b) below.  Show the output of running class Test using the Circle class first from (a), and then from (b).  Explain the output.
    public class Test {
       public static void main ( String [] args ) {
          Object circle1 = new Circle();
          Object circle2 = new Circle();
          System.out.println( circle1.equals( circle2 ) );
       }
    }
    1. class Circle {
         double radius;
         public boolean equals ( Circle circle ) {
            return this.radius == circle.radius;
         }
      }
    2. class Circle {
         double radius;
         public boolean equals ( Object circle ) {
            return this.radius == ((Circle) circle).radius;
         }
      }

    Next, try adding the “@Override” annotation to the equals method in (a), and then try compiling.  Repeat with (b).  What are the results, and are they as you expected?

  8. How would you prevent a class from being extended?  How would you prevent a method from being overridden?
  9. Which of the following classes define legal abstract classes?
    1. class A {
         abstract void unfinished ( ) {
         }
      }
    2. public class abstract A {
         abstract void unfinished ( ) {
         }
      }
    3. class A {
         abstract void unfinished ( ) ;
      }
    4. abstract class A {
         protected void unfinished ( ) ;
      }
    5. abstract class A {
         abstract void unfinished ( ) ;
      }
    6. class A {
         abstract int unfinished ( ) ;
      }
  10. Suppose A is an interface.  Can you create an instance using “new A()”?
  11. Which of the following (if any) is a correct interface declaration?  (Assume I1 and I2 are correctly defined elsewhere.)
    1. interface A {
         void print () {
         };
      }
    2. abstract interface A extends I1, I2 {
         abstract void print () {
         };
      }
    3. abstract interface A {
         print () ;
      }
    4. interface A {
         void print () ;
      }

To be turned in:

Email your homework assignment, by copy-and-paste (no attachments please), to (homework submission).  If possible use the “text” and not the “HTML” mode of your email program.  Please use the subject similar to “Java Programming I Homework Assignment #5 Submission”, so I can tell which emails are submitted homework assignments.

Homework assignments will not be returned.  Please do not send as attachments.  Refer to the Homework and the Submitting Assignments sections of your syllabus for more information.

Confused?  Send questions about the homework assignment to (homework questions).  Please use a subject similar to “Java Programming I Homework Assignment #5 Questions” so I can tell which emails are questions about assignment quiz (and not submissions).