Rewriting, Overriding, Overloading, Polymorphism Difference Analysis

override->override (=override), overload->overload, polymorphism -> polymorphism
 
Override is to override (override) a method to implement a different function. It is generally used for subclasses to override (reimplement) methods in the parent class when inheriting the parent class.

Rules for overriding (overriding):
   1. The parameter list of the overriding method must be exactly the same as that of the overridden method, otherwise it cannot be called overriding but overloading.
   2. The access modifier of the overriding method must be Greater than the access modifier of the overridden method (public>protected>default>private).
   3. The return value of the overridden method must be the same as the return of the overridden method;
   4. The exception thrown by the overridden method must be the same as the exception thrown by the overridden method, or a subclass thereof ;
   5. The overridden method cannot be private, otherwise it just defines a new method in its subclass without overriding it.
   6. Static methods cannot be rewritten as non-static methods (compile errors).
 
Overload is overloading, which is generally used to implement several overloaded methods in a class. These methods have the same name but different parameter forms.
Overloading rules:
   1. When overloading is used, it can only be implemented through the same method name and different parameter forms. Different parameter types can be different parameter types, different number of parameters, and different parameter sequences (parameter types must be different);
   2. It cannot be overloaded by access rights, return types, and thrown exceptions;
   3. Methods The type and number of exceptions will not affect overloading;
 
The concept of polymorphism is more complex, and there are multiple meanings of polymorphism. An interesting but imprecise statement is: inheritance is the method that the subclass uses the superclass, and polymorphism is the method that the superclass uses the subclass.
In general, we use polymorphism to avoid a lot of overloading in the parent class that will cause the code to be bloated and difficult to maintain.

Case program:

public class Shape
{
   public static void main(String[] args){
	   
     Triangle tri = new Triangle();
     System.out.println("Triangle is a type of shape? " + tri.isShape()); // The method of inheriting the parent class is also inherited
     
     Shape shape = new Triangle(); // parent class refers to child class object
     System.out.println("My shape has " + shape.getSides() + " sides."); // Runtime polymorphism due to rewriting (overwriting)
     
     Rectangle Rec = new Rectangle();
     Shape shape2 = Rec;
     System.out.println("My shape has " + shape2.getSides(Rec) + " sides."); //Overload compile-time polymorphism
     
   }
   public boolean isShape(){
     return true;
   }
   public int getSides(){
     return 0 ;
   }
   public int getSides(Triangle tri){ //overload
     return 3 ;
   }
   public int getSides(Rectangle rec){ //overload
    return 4 ;
   }
}
class Triangle extends Shape
{
   public int getSides() { //Rewrite, implement polymorphism
     return 3;
   }
}
class Rectangle extends Shape
{
   public int getSides(int i) { //overload
    return i;
   }
}

result:



Original text: https://blog.csdn.net/ericbaner/article/details/3857268 

Guess you like

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