Rewritten (the override) and overload (overload), virtual methods, mandatory exception

Note: If not correct place, like him to understand and welcome criticism. Reproduced, please link: https://www.cnblogs.com/pmbb/p/11409443.html

Rewrite (Override)

Rewriting implementation is a subclass of method allow access to the parent class is re-written, the return value, and can not change parameter. That same shell, core rewrite!

Rewrite the benefits that subclasses as needed, define specific in their actions. That subclass the parent class can implement as needed.

Overriding methods can not throw checked exceptions or declare a new broader exceptions than the overridden method. For example: a parent class affirms a checked exception IOException, but can not override this method when thrown Exception exception because IOException Exception is the parent class, subclass can throw IOException anomaly.

In object-oriented principles, the means to rewrite any existing method can be overridden. Examples are as follows:

TestDog.java file code:
. 1  class Animal {
 2     public  void Move () {
 . 3        System.out.println ( "animal may move" );
 . 4     }
 . 5  }
 . 6   
. 7  class Dog the extends Animal {
 . 8     public  void Move () {
 . 9        System.out.println ( "dog can run and go" );
 10     }
 . 11  }
 12 is   
13 is  public  class TestDog {
 14     public  static  void main (String args []) {
 15        Animal A = new newAnimal (); // Animal objects 
16        Animal B = new new Dog (); // Dog objects 
. 17   
18 is        a.move (); // perform a method Animal class 
. 19   
20 is        b.move (); // performed Dog class method 
21     }
 22 }

The above examples compiled results are as follows:

Animals can move the dog can run and walk


It can be seen in the example above, although the type b Animal belongs, but it is a move operation method of class Dog.

This is because at compile time, just check the parameters of a reference type.

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

Therefore, in the above example, the compiler is able to succeed because the presence of the move method in class Animal, however runtime, the specific method of operation is the object.

Consider the following example:

TestDog.java file code:
. 1  class Animal {
 2     public  void Move () {
 . 3        System.out.println ( "animal may move" );
 . 4     }
 . 5  }
 . 6   
. 7  class Dog the extends Animal {
 . 8     public  void Move () {
 . 9        System.out.println ( "dog can run and go" );
 10     }
 . 11     public  void Bark () {
 12 is        System.out.println ( "dog barking can" );
 13 is     }
 14  }
 15   
16  public  class{TestDog
 . 17     public  static  void main (String args []) {
 18 is        Animal A = new new Animal (); // Animal objects 
. 19        Animal B = new new Dog (); // Dog the object 
20 is   
21 is        a.move (); // Animal method of performing class 
22 is        b.move (); // method performed Dog class 
23 is        b.bark ();
 24     }
 25 }

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 the reference type b Animal not bark method.

If you want to run bark () method, then the

 b.bark();

Change

Dog c = (Dog) b; // switch down

 c.bark();

To

Method of rewriting rules

Exactly the same argument list must be rewritten methods.

The return type and return type to be overridden method may vary, but must return a derived class is the parent class values ​​(java5 and earlier return to the same type, java7 and later can be different).

Access can not be lower than the parent class is rewritten access method. For example: If a parent class is declared as public, so override this method in a subclass can not be declared as protected.

Members of the parent class method can only be rewritten its subclasses.

Declared final method 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, then the subclass can override the parent of all methods, except for the private method declaration and final.

Sub-classes and superclasses are not the same package, then the subclass can only override the parent class declared as non-final public or protected methods are.

Overridden method can throw any unchecked exceptions, regardless of the method being overridden if an exception is thrown. However, the method can not be overridden throw new mandatory exception, or wider than the mandatory declaration to be rewritten for exception, otherwise you can.

The method of construction can not be rewritten.

If you can not inherit a method, you can not override this method.

Using the Super keyword

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

. 1  class Animal {
 2     public  void Move () {
 . 3        System.out.println ( "animal may move" );
 . 4     }
 . 5  }
 . 6   
. 7  class Dog the extends Animal {
 . 8     public  void Move () {
 . 9        Super .move (); // methods super class 
10        System.out.println ( "dog can run and go" );
 . 11     }
 12 is  }
 13 is   
14  public  class TestDog {
 15     public  static  voidmain (String args []) {
 16   
. 17        Animal B = new new Dog (); // Dog objects 
18 is        b.move (); // perform a method Dog class 
. 19   
20 is     }
 21 }

The above examples compiled results are as follows:

Animals can move the dog can run and walk


Overload (Overload)

Overload (Overloading) is a class in which process the same name, but different parameters. Return type may be the same or different.

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

The most common place is overloaded constructor.

Overloading rules:

  • Overloaded method must be changed parameter list (number of parameters or not the same type);
  • Overloaded method may change the return type;
  • The method may be varied overloaded access modifiers;
  • Overloaded methods can declare a new or broader checked exceptions;
  • Or method can be overridden in a subclass in the same class.
  • Type as the return value can not distinguish between a standard function overloading.

Examples

Overloading.java file code:
 1 public class Overloading {
 2     public int test(){
 3         System.out.println("test1");
 4         return 1;
 5     }
 6  
 7     public void test(int a){
 8         System.out.println("test2");
 9     }   
10  
11     //以下两个参数类型顺序不同
12     public String test(int a,String s){
13         System.out.println("test3");
14         return "returntest3";
15     }   
16  
17     public String test(String s,int a){
18         System.out.println("test4");
19         return "returntest4";
20     }   
21  
22     public static void main(String[] args){
23         Overloading o = new Overloading();
24         System.out.println(o.test());
25         o.test(1);
26         System.out.println(o.test(1,"test3"));
27         System.out.println(o.test("test4",1));
28     }
29 }

重写与重载之间的区别

区别点 重载方法 重写方法
参数列表 必须修改 一定不能修改
返回类型 可以修改 一定不能修改
异常 可以修改 可以减少或删除,一定不能抛出新的或者更广的异常
访问 可以修改 一定不能做更严格的限制(可以降低限制)

总结

方法的重写(Overriding)和重载(Overloading)是java多态性的不同表现,重写是父类与子类之间多态性的一种表现,重载可以理解成多态的具体表现形式。

  • (1)方法重载是一个类中定义了多个方法名相同,而他们的参数的数量不同或数量相同而类型和次序不同,则称为方法的重载(Overloading)。
  • (2)方法重写是在子类存在方法与父类的方法的名字相同,而且参数的个数与类型一样,返回值也一样的方法,就称为重写(Overriding)。
  • (3)方法重载是一个类的多态性表现,而方法重写是子类与父类的一种多态性表现。

虚方法

我们先了解什么是“基类”?在面向对象设计中,被定义为包含所有实体共性的class类型,被称为“基类”。

什么是派生类?利用继承机制,新的类可以从已有的类中派生。那些用于派生的类称为这些特别派生出的类的“基类”。

什么是虚方法?

虚方法可以有实现体,若一个实例方法的声明中含有 virtual 修饰符,则称该方法为虚方法。使用了 virtual 修饰符后,不允许再有 static、abstract 或者 override 修饰符。

简单工厂模式中虚方法代码:

1 //用于运算后的结果
2 public virtual double GetResult()
3 {
4     double result = 0;
5     return result;
6 }

可以看出该类中有一个 GetResult() 的方法,GetResult() 方法中带有一个 virtual 修饰符,该修饰符表明:该基类的派生类可以重载该方法。 GetResult() 方法的作用:输出语句 "这是一个虚方法!" 到控制台。

一个虚方法的实现可以由派生类取代。取代所继承的虚方法的实现的过程称为重写该方法;在一个虚方法调用中,该调用所设计的那个实例运行时的类型确定了要被调用的究竟是该方法的哪一个实现。

 

抽象方法和虚方法的区别

 

  • 1.虚方法必须有实现部分,抽象方法没有提供实现部分,抽象方法是一种强制派生类覆盖的方法,否则派生类将不能被实例化。
  • 2.抽象方法只能在抽象类中声明,虚方法不是。如果类包含抽象方法,那么该类也是抽象的,也必须声明类是抽象的。
  • 3.抽象方法必须在派生类中重写,这一点和接口类似,虚方法不需要再派生类中重写。

 

简单说,抽象方法是需要子类去实现的。虚方法是已经实现了的,可以被子类覆盖,也可以不覆盖,取决于需求。

抽象方法和虚方法都可以供派生类重写。

强制性异常

 java中的异常分为两大类,强制性异常(CheckedException)和非强制性异常(UncheckedException)。而java中除了运行时异常(RuntimeException)外,都是强制性异常。 

 非强制性异常:所谓非强制性异常就和上面相反了。不过你当然也可以try catch或者thows,只不过这不是强制性的。

 

 

 

 

 

 

 

 参考网址:

重写与重载:https://www.runoob.com/java/java-override-overload.html

虚方法:https://www.runoob.com/w3cnote/what-is-a-virtual-method.html

强制性异常:https://blog.csdn.net/qq_39536158/article/details/83093781

 

Guess you like

Origin www.cnblogs.com/pmbb/p/11409443.html