The best Java rewrite (Override) and overloading (Overload) Tutorial In 2024, In this tutorial you can learn Rewrite (Override),The method of rewriting rules,Using the Super keyword,Overload (Overload),Rewriting the difference between overloading,
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.
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 (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
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)); } }
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) |