Latest web development tutorials

Java rewrite (Override) and overloading (Overload)

Rewrite (Override)

Rewriting is a subclass of the implementation process to allow access to the methods of the parent class to re-write! Return values ​​and parameters are not changed. That is the same case, the core rewrite!

Rewriting advantage of subclasses as needed, define specific in their actions.

That is a subclass of a parent class method can be realized as needed.

In object-oriented principles, the rewriting means that you can override any existing methods. Examples are as follows:

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("狗可以跑和走");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象

      a.move();// 执行 Animal 类的方法

      b.move();//执行 Dog 类的方法
   }
}

The above examples compiled results are as follows:

动物可以移动
狗可以跑和走

In the above example it can be seen, even though b belongs to Animal types, but it is a move operation method Dog class.

This is because at compile time, but checking reference type parameter.

However, at run time, the type of the Java Virtual Machine (JVM) of the specified object and method of operation of the object.

So in the above example, the compiler has been able to succeed because the move method exists Animal class, but running, running is a method for a particular object.

Consider the following examples:

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      System.out.println("狗可以跑和走");
   }
   public void bark(){
      System.out.println("狗可以吠叫");
   }
}

public class TestDog{

   public static void main(String args[]){
      Animal a = new Animal(); // Animal 对象
      Animal b = new Dog(); // Dog 对象

      a.move();// 执行 Animal 类的方法
      b.move();//执行 Dog 类的方法
      b.bark();
   }
}

The above examples compiled results are as follows:

TestDog.java:30: cannot find symbol
symbol  : method bark()
location: class Animal
                b.bark();
                 ^

The program will throw a compilation error because there is no b reference type Animal bark method.


The method of rewriting rules

  • Parameter list must be exactly the same as the method to be rewritten;
  • The return type must be completely rewritten with the return type of the method is the same;
  • Access is not lower than the parent class access overridden methods. For example: If a parent class is declared public, then override this method in a subclass can not be declared as protected.
  • Members of the parent class can only be overridden its subclasses.
  • Declared as final methods can not be overridden.
  • Declared as static methods can not be rewritten, but can be declared again.
  • The parent class and subclass in the same package, the subclass can override the parent class of all methods, other than statements of private and final approach.
  • Subclass and superclass is not the same package, so only subclasses can override declaration of non-final methods of the parent class public and protected for.
  • Overridden method can throw any non-mandatory exception, regardless of whether the method has been rewritten throw an exception. However, the method can not override the mandatory exception throw new or broader than the mandatory declaration is overridden method exception, otherwise you can.
  • Constructor can not be rewritten.
  • If a method can not be inherited, it can not override this method.

Using the Super keyword

When you need to be rewritten to call the parent class method in a subclass, to use the super keyword.

class Animal{

   public void move(){
      System.out.println("动物可以移动");
   }
}

class Dog extends Animal{

   public void move(){
      super.move(); // 应用super类的方法
      System.out.println("狗可以跑和走");
   }
}

public class TestDog{

   public static void main(String args[]){

      Animal b = new Dog(); // Dog 对象
      b.move(); //执行 Dog类的方法

   }
}

The above examples compiled results are as follows:

动物可以移动
狗可以跑和走

Overload (Overload)

Overload (overloading) is in a class inside the method of the same name, but with different parameters. The return type may be the same or different.

Each overloaded method (or constructor) must have a unique list of argument types.

Only overloaded constructors

Overloading rules

  • Overloaded method must change the parameter list;
  • Overloaded methods can change the return type;
  • Overloaded methods may change access modifiers;
  • Overloaded methods can declare a new or broader checked exceptions;
  • The method can be in the same class or be overloaded in a subclass.

Examples

public class Overloading {
 
	public int test(){
		System.out.println("test1");
		return 1;
	}
 
	public void test(int a){
		System.out.println("test2");
	}	
 
	//以下两个参数类型顺序不同
	public String test(int a,String s){
		System.out.println("test3");
		return "returntest3";
	}	
 
	public String test(String s,int a){
		System.out.println("test4");
		return "returntest4";
	}	
 
	public static void main(String[] args){
		Overloading o = new Overloading();
		System.out.println(o.test());
		o.test(1);
		System.out.println(o.test(1,"test3"));
		System.out.println(o.test("test4",1));
	}
}

Rewriting the difference between overloading

Point of difference Overloaded methods Overriding methods
Parameter List You must be modified Must not be modified
Return Type You can modify Must not be modified
abnormal You can modify Can be reduced or removed, you must not throw new or broader exception
access You can modify Must not be more stringent restrictions (limit can be reduced)