SDSU CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Doc 16, Finally and Inheritance

[To Lecture Notes Index]
San Diego State University -- This page last updated Oct 17, 1996
----------

Contents of Doc 16, Finally and Inheritance


Doc 16, Finally and Inheritance Slide # 1
References

The Java Programming Language, Arnold and Gosling, Chapter 7, page 140

The Java Language Specification, Gosling, Joy, Steele, 1996, section 8.4.4 page 163-4, 8.4.6.3 page 166



If anything can go wrong, it will.
Finagle's Law
(often incorrectly attributed to Murphy, whose law is rather different - which only goes to show that Finagle was right)

Doc 16, Finally and Inheritance Slide # 2Listen Here!
Finally is Master

A finally block is entered with a reason, such as end of the method, an exception was thrown

If the finally block creates it own reason to leave ( throws an exception, executes a return or break) then the original reason is forgotten
class  FinalExample
   {
   public static void explainThis()
      {
      int  students[] = new int[5];
      try  
         {
         System.out.println( "Start Try"  );
         students[  10  ]  =  1;
         }  
      finally   
         {
         System.out.println(  "In Final Block"  );
         return;
         }
      }
      
   public  static  void  main( String  args[]  )
      {
      try
         {
         explainThis();
         }
      catch  (ArrayIndexOutOfBoundsException missed) 
         {
         System.out.println(  "In Catch"  );
         }
      System.out.println(  "After try in main"  );
      }
   }
Output
Start Try
In Final Block
After try in main

Doc 16, Finally and Inheritance Slide # 3Listen Here!
Exceptions and Inheritance
The rule
A method that overrides another method may not be declared to throw more checked exceptions than the overridden method
The Details
Let class Child be a subclass of class Parent

Let foo be a method of Parent that is overridden by child

If Child.foo has a throws clause that mentions any checked exceptions, then Parent.foo must have a throws clause.

For every checked exception listed in Child.foo, that same exception class or one of its superclasses must occur in the throws clause of Parent.foo
class Parent
   {
   public void foo() throws IOException {};

   public void bar() {};
   }

class Child extends Parent
   {
   public void foo() {};         // OK

   public void bar() throws IOException{};   // Compile Error
   }

Doc 16, Finally and Inheritance Slide # 4Listen Here!
Inheritance of Access Levels

The access modifier of an overriding method must provide at least as much access as the overridden method
Note

A private method is not accessible to subclasses, so can not be overridden in the technical sense.

Thus a subclass can declare a method with the same signature as a private method in one of its superclasses and there is no requirement that the return type or throws clause of the method bear any relationship to those of the private method in the superclass
class Parent
   {
   protected void foo()  {};

   public void bar() {};

   private void noSeeMe() {};
   }

class Child extends Parent
   {
   public void foo() {};         // OK

   protected void bar() {};      // Compile Error

   public int noSeeMe() throws IOException{ return 5;};   //OK

   }

Doc 16, Finally and Inheritance Slide # 5
Rules Are Different for Fields
class Parent
   {
   protected int foo;

   public int bar;
   }

class Child extends Parent
   {
   public int foo;         // OK

   protected int bar;      // OK
   }


----------