Java
 Computer >> コンピューター >  >> プログラミング >> Java

Javaでのメソッドのオーバーライドに関する例外処理のルールは何ですか?


スーパークラスメソッドはオーバーライド中に例外をスローしますが、特定のルールに従う必要があります。

同じ例外またはサブタイプをスローする必要があります

スーパークラスメソッドが特定の例外をスローする場合、サブクラスのメソッドは同じ例外またはそのサブタイプをスローする必要があります。

次の例では、スーパークラスのreadFile()メソッドがIOEXceptionをスローし、サブクラスのreadFile()メソッドがFileNotFoundException例外をスローします。

FileNotFoundException例外はIOExceptionのサブタイプであるため、このプログラムはエラーなしでコンパイルおよび実行されます。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public String readFile(String path) throws IOException {
      throw new IOException();
   }
}
public class ExceptionsExample extends Super {
   @Override
   public String readFile(String path) throws FileNotFoundException {
      Scanner sc = new Scanner(new File("E://test//sample.txt"));
      String input;
      StringBuffer sb = new StringBuffer();
      while (sc.hasNextLine()) {
         input = sc.nextLine();
         sb.append(" "+input);
      }
      return sb.toString();
   }
   public static void main(String args[]) {
      String path = "E://test//sample.txt";
      ExceptionsExample obj = new ExceptionsExample();
      try {
         System.out.println(obj.readFile(path));
      }catch(FileNotFoundException e) {
         System.out.println("Make sure the specified file exists");
      }
   }
}

出力

Tutorials Point is an E-learning company that set out on its journey to provide knowledge to that class of readers that responds better to online content. With Tutorials Point, you can learn at your own pace, in your own space. After a successful journey of providing the best learning content at tutorialspoint.com, we created our subscription based premium product called Tutorix to provide Simply Easy Learning in the best personalized way for K-12 students, and aspirants of competitive exams like IIT/JEE and NEET.

同様に、サブクラスがスーパークラスと同じ例外をスローした場合、プログラムは正常にコンパイルおよび実行されます。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public void sampleMethod()throws FileNotFoundException {
      System.out.println("Method of superclass");
   }
}
public class ExceptionsExample extends Super {
   public void sampleMethod()throws FileNotFoundException {
      System.out.println("Method of Subclass");
   }
   public static void main(String args[]) {
      ExceptionsExample obj = new ExceptionsExample();
      obj.sampleMethod();
   }
}

出力

Method of Subclass

スーパータイプの例外をスローしないでください

スーパークラスメソッドが特定の例外をスローする場合、サブクラスのメソッドはそのスーパータイプをスローしないようにする必要があります。

次の例では、スーパークラスのreadFile()メソッドがFileNotFoundException例外をスローし、サブクラスのreadFile()メソッドがFileNotFoundExceptionのスーパータイプであるIOExceptionをスローします。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public String readFile(String path)throws FileNotFoundException {
      throw new FileNotFoundException();
   }
}
public class ExceptionsExample extends Super {
   @Override
   public String readFile(String path)throws IOException {
      //method body ......
   }
}

コンパイル時エラー

コンパイル時に、上記のプログラムは次の出力を提供します-

ExceptionsExample.java:13: error: readFile(String) in ExceptionsExample cannot override readFile(String) in Sup
   public String readFile(String path)throws IOException {
                 ^
   overridden method does not throw IOException
1 error

例外をスローせずに

スーパークラスメソッドが特定の例外をスローした場合、例外をスローせずにオーバーライドできます。

次の例では、スーパークラスのsampleMethod()メソッドはFileNotFoundException例外をスローし、sampleMethod()メソッドは例外をまったくスローしません。それでも、このプログラムはエラーなしでコンパイルおよび実行されます。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
abstract class Super {
   public void sampleMethod()throws FileNotFoundException {
      System.out.println("Method of superclass");
   }
}
public class ExceptionsExample extends Super {
   public void sampleMethod() {
      System.out.println("Method of Subclass");
   }
   public static void main(String args[]) {
      ExceptionsExample obj = new ExceptionsExample();
      obj.sampleMethod();
   }
}

出力

Method of Subclass

  1. JavaでのStringintern()メソッドの役割は何ですか?

    文字列は、文字のシーケンスを格納するJavaのクラスであり、 java.langに属します。 パッケージ。 Stringオブジェクトを作成すると、それらを変更することはできません(不変)。 ストレージ すべてのStringオブジェクトは、String Constant poolと呼ばれるヒープ領域の別のメモリ位置に格納されます。 。 文字列値を定義するときはいつでも、JVMは文字列定数プールに指定された値で文字列オブジェクトを作成します。したがって、上記のプログラムを実行すると、2つの文字列値が文字列定数プールに作成されます。 intern()メソッド このメソッドは、一意

  2. Javaのpaint()メソッドとrepaint()メソッドの違いは何ですか?

    Paint()とRepaint() paint(): このメソッドは、このコンポーネントをペイントするための命令を保持します。 Java Swingでは、paintが paintBorder()を呼び出すときに、paint()メソッドの代わりにpaintComponent()メソッドを変更できます。 paintComponent() およびpaintChildren() メソッド。このメソッドを直接呼び出すことはできません。代わりに、 repaint()を呼び出すことができます。 。 repaint() :このメソッドはオーバーライドできません。 paint()を制御します サイ