Javaにおけるoverridingのルール

6372 ワード

Rules for method overriding:
  • The argument list should be exactly the same as that of the overridden method.
  • パラメータリストは、
  • に一致する必要があります.
  • The return type should be the same or a subtype of the return type declared in the original overridden method in the superclass.
  • returnのタイプは同じでもよいし、サブクラスreturnのタイプは親のサブクラス
  • である.
  • The access level cannot be more restrictive than the overridden method's access level. For example: if the superclass method is declared public then the overridding method in the sub class cannot be either private or protected.
  • アクセス・レベルは、親よりも緩和されます.Javaのアクセス・レベルは、public、package level(具体的には何も書かない)、protected、private
  • の順です.
  • Instance methods can be overridden only if they are inherited by the subclass.
  • インスタンスメソッドは、親から継承するメソッド
  • のみを上書きできます.
  • A method declared final cannot be overridden.
  • finalメソッドは
  • を上書きできません.
  • A method declared static cannot be overridden but can be re-declared.
  • staticメソッドは上書きできませんが、
  • を再宣言できます.
  • If a method cannot be inherited, then it cannot be overridden.
  • メソッドが継承できない場合、メソッド野は
  • をカバーできません.
  • A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final.
  • 子クラス扶親クラスが同じパッケージの下にある場合、子クラスは親クラスを上書きすることができるので、privateまたはfinalではない方法
  • A subclass in a different package can only override the non-final methods declared public or protected.
  • 子クラス扶親クラスが異なるパッケージの下にある場合、子クラスは親クラスをカバーすることができるので、非final同時public/protectedの方法
  • .
  • An overriding method can throw any uncheck exceptions, regardless of whether the overridden method throws exceptions or not. However the overriding method should not throw checked exceptions that are new or broader than the ones declared by the overridden method. The overriding method can throw narrower or fewer exceptions than the overridden method.
  • 親が異常を投げ出すかどうかにかかわらず、子が上書きする方法は検査しない異常を投げ出すことができる.親が例外を放出する場合、子が上書きする方法は、親よりも小さい例外
  • しか放出できません.
  • Constructors cannot be overridden.
  • 構造関数は
  • を上書きできません.
    上記の説明が理解できない場合は、以下の例に参加してください.
    1.親
    public class TestA {
    	
    	protected void test1(int a, int b) {
    		System.out.println("supper");
    		return;		
    	}
    	
    	protected long test2(int a, int b) {
    		System.out.println("supper");
    		return 0;		
    	}
    	
    	protected TestB test3(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test4(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test5(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test6(int a, TestA b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test6(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected static TestA test7(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test8(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test9(int a, int b) throws Exception{
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected TestA test10(int a, int b) throws NullPointerException{
    		System.out.println("supper");
    		return null;		
    	}
    	
    	protected static TestA test11(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    
    }

    2.サブクラス
    public class TestB extends TestA{
    	
    	@Override
    	public void test1(int a, int b) {
    		System.out.println("sub");
    		return;		
    	}
    	
    	@Override
    	public long test2(int a, int b) {
    		System.out.println("sub");
    		return 0;		
    	}
    
    // fail	
    //	@Override
    //	protected TestA test3(int a, int b) {
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	@Override
    	protected TestB test3(int a, int b) {
    		System.out.println("sub");
    		return null;		
    	}
    	
    	@Override
    	protected TestB test4(int a, int b) {
    		System.out.println("sub");
    		return null;		
    	}
    	
    	@Override
    	public TestB test5(int a, int b) {
    		System.out.println("sub");
    		return null;		
    	}
    	
    //	fail
    //	@Override
    //	protected TestA test6(int a, TestB b) {
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	@Override
    	protected TestA test6(int a, TestA b) {
    		System.out.println("sub");
    		return null;		
    	}
    	
    	@Override
    	protected TestA test6(int a, int b) {
    		System.out.println("sub");
    		return null;		
    	}
    	
    //	fail: This instance method cannot override the static method
    //	protected TestA test7(int a, int b) {
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    //	fail: can not throw checked exception
    //	@Override
    //	protected TestA test8(int a, int b) throws Exception {
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	@Override
    	protected TestA test8(int a, int b) throws NullPointerException {
    		System.out.println("sub");
    		return null;		
    	}
    	
    // fail:Exception Throwable is not compatible with throws 
    //	 clause in TestA.test8(int, int)
    //	@Override
    //	protected TestA test8(int a, int b) throws Throwable {
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	
    //	OK
    //	@Override
    //	protected TestA test9(int a, int b) throws Exception{
    //		System.out.println("sub");
    //		return null;		
    //	}
    //	OK
    //	@Override
    //	protected TestA test9(int a, int b) throws Error{
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	@Override
    	protected TestA test9(int a, int b) throws NullPointerException{
    		System.out.println("sub");
    		return null;		
    	}
    	
    //	fail
    //	@Override
    //	protected TestA test9(int a, int b) throws Throwable{
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	@Override
    	protected TestA test10(int a, int b) throws NullPointerException{
    		System.out.println("sub");
    		return null;		
    	}
    	
    //	fail
    //	@Override
    //	protected TestA test10(int a, int b) throws Exception{
    //		System.out.println("sub");
    //		return null;		
    //	}
    	
    	protected static TestA test11(int a, int b) {
    		System.out.println("supper");
    		return null;		
    	}
    	
    	
    }
    

    http://www.tutorialspoint.com/java/java_overriding.htm
    http://docs.oracle.com/javase/tutorial/java/IandI/override.html