Second, Java object-oriented (8) _ inheritance idea - method coverage

2018-05-01

 

method overriding (overriding)

 

override:

The problem solved by overriding: When a behavior (method) of the parent class does not conform to the specific characteristics of the child class, the parent class needs to redefine the parent class method and rewrite the method body.

Note: Only methods have the concept of overriding, fields do not.

 

The principle of method overriding:

  1. The instance method signatures must be the same. (method signature = method name + method parameter list)

  2. The subclass method cannot return a larger data type than the superclass method. The return value type of the subclass method must be the same as the return value type of the superclass method, or the return value type of the subclass method is the return value of the superclass method. A subclass of type.

   (Subclasses can return more specific data types, such as the parent class returns Object, the subclass can return String)

  3. The subclass method cannot be lower than the access authority of the parent class method 

  4. The subclass method cannot throw more than the parent class method. If there are many exceptions, the exception thrown by the subclass method must be the same as the exception thrown by the superclass method, or the exception class thrown by the subclass method is a subclass of the exception class thrown by the superclass method.

  5. Method coverage only exists between subclasses and superclasses (including direct superclasses and indirect superclasses). Methods in the same class can only be overloaded, not overridden.

 

Suggestion: Copy the parent class method directly to the child class, and then rewrite its method body.

Judgment of method coverage: write the @override tag before the method.

         If the coverage is successful, the compilation is passed, otherwise the compilation fails.

 

Code example:

1  // bird 
2  class Bird
 3  {
 4      public  void fly(){
 5          System.out.println("free fly" );
 6      }
 7  }
 8  // Penguin 
9  
10  class Penguin extends Bird
 11  {
 12      @override
 13      public  void fly(){
 14          System.out.println("Angel with broken wings" );
 15      }
 16  }
 17  
18  
19  
20  //Method Override 
21  class OverrideDemo 
 22  {
 23      public  static  void main(String[] args) 
 24      {
 25          // Create a penguin object 
26          Penguin p = new Penguin();
 27          // Call the fly method 
28          p.fly();
 29      }
 30 }

 

Reference blog:

http://ihyperwin.iteye.com/blog/1553609

-----------------------------------------------------------------------------------------------------------------------

 

The difference between method overriding and method overloading:

override : override (override)

overload : overload

Actually the two have nothing to do with each other.

 

 

Method overloading definition: To define multiple methods with the same name in a class, the parameter lists between the methods are required to be different.

方法重载的作用:完成一组任务相似但参数的类型或参数的个数不同的方法。(屏蔽同一功能的方法由于参数不同而造成方法名称不同的差异)

方法重载的调用:Java编译器能通过检查调用的方法的参数类型和个数选择一个恰当的方法。

方法重载的判断:两同一不同

        两同:同一个类,方法名称相同

        一不同:方法的参数列表不一致(参数类型、参数个数、参数顺序)

注意:方法重载和方法返回值类型无关,只是一般要求同一功能的方法返回值类型一致

   参数列表和参数名称无关,也就是说方法重载和形参无关

 

Java编译器能通过检查调用的方法的参数类型和个数选择一个恰当的方法。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325123326&siteId=291194637