Inheritance, polymorphism and method overriding, overloading

  • 1. Polymorphism:

    • Polymorphism refers to the invocation of the same method, which may behave differently depending on the object. In real life, the implementation of the same method is completely different.
    • The main points of polymorphism:
      • (1) Polymorphism is polymorphism of methods, not polymorphism of attributes (polymorphism has nothing to do with attributes);
      • (2) There are three necessary conditions for the existence of polymorphism: inheritance, method overriding, and parent class references pointing to subclass objects;
      • (3) After the parent class reference points to the subclass object, use the parent class reference to call the method overridden by the subclass, and polymorphism appears at this time.
/**
 * - - - (1)多态是方法的多态,不死属性的多态(多态与属性无关);

 * - - - (2)多态的存在要有3个必要条件:继承、方法重写、父类引用指向子类对象;

 * - - - (3)父类引用指向子类对象后,用该父类引用调用子类重写的方法,此时多态就出现了
 * 
 *
 */


public class TestPolm {
    public static void main(String[] args) {
        Animal a = new Animal();
        animalCry(a);
        Dog dog = new Dog();
        animalCry(dog);
        Cat cat = new Cat();
        animalCry(cat);
        Animal cat2 = new Cat();
        animalCry(cat2);
    }
    
    static void animalCry(Animal a){
        a.shut();
    }
}

class Animal{
    public void shut(){
        System.out.println("叫了一下!");
    }
}

class Dog extends Animal{//继承条件满足
    public void shut(){//方法重写条件满足
        System.out.println("汪汪汪");
    }
}

class Cat extends Animal{//继承条件满足
    public void shut(){//方法重写条件满足
        System.out.println("喵喵喵");
    }
}

/**运行结果:
 * 叫了一下!
 * 汪汪汪
 * 喵喵喵
 * 喵喵喵
 */
 

  • 2. The main points of inheritance:

    • The parent class is also called super class, base class, etc.;
    • There is only single inheritance in java, there is no multiple inheritance like C++. Multiple inheritance can cause confusion, making inheritance too complicated and the system difficult to maintain;
    • There is no multiple inheritance for classes in java, but multiple inheritance for interfaces;
    • The subclass inherits the parent class and can get all the properties and methods of the parent class (except the constructor of the parent class), but not necessarily direct access (for example, the private properties and methods of the parent class);
    • If you define a class without calling extends, its parent class is: java.lang.Object.
  • 3. The instanceof operator:

    • instanceof is a binary operator, the left is the object, the right is the class; when the object is an object created by the right class or subclass, it returns true; otherwise, it returns false.
public class Test{
    public static void main(String[] args) {
        Student s = new Student();
        System.out.println(s instanceof Person);  //Student 继承自Person
        System.out.println(s instanceof Student);
    }
}

  • 4. Upward automatic transformation, upward forced transformation:


public class TestPolm {
    public static void main(String[] args) {
        
        Animal cat = new Cat();//自动向上转型
        animalCry(cat);
        
        Cat cat2 = (Cat)cat; //强制向下转型
    }
    
    static void animalCry(Animal a){
        a.shut();
    }
}

class Animal{
    public void shut(){
        System.out.println("叫了一下!");
    }
}

class Dog extends Animal{//继承条件满足
    public void shut(){//方法重写条件满足
        System.out.println("汪汪汪");
    }
}

class Cat extends Animal{//继承条件满足
    public void shut(){//方法重写条件满足
        System.out.println("喵喵喵");
    }
}

  • 5. The ordinary methods of the parent class can be inherited and overridden, and there is not much explanation. If the subclass inherits the parent class, and the subclass does not override the parent class's methods, the subclass will have methods inherited from the parent class.

    • Static methods can be inherited, but not overridden. If there is a static method in the parent class, and the subclass also has a method with the same method name, parameter type, and number of parameters, and it is also modified with the static keyword, then the method of the subclass will inherit the original parent class. The method is hidden, not overridden. In layman's terms, the method of the parent class and the method of the subclass are two unrelated methods, and which method is called depends on which object is referenced; this kind of parent-child method does not have the property of polymorphism. It is mentioned in "Java Programming Ideas" that "only ordinary method calls can be polymorphic". The following code is an example:
public class StaticTest  
{  
    public static void main(String[] args)  
    {  
        M m = new N();  
        m.output();  
    }  
}  
  
class M  
{  
    public static void output()  
    {  
        System.out.println("M");  
    }  
}  
  
class N extends M  
{  
    public static void output()  
    {  
        System.out.println("N");  
    }  
}  

The result of the above execution is "M", which is called by a reference of type M. If you modify the code in the main method:

N n = new N();

n.output();

Then the result of the execution is "N", which is called by a reference of type N.


  • 6. The subclass can inherit all members of the parent class. Using reflection, it can be seen that the subclass inherits the private method of the parent class (whether it is final or not), but it is not possible to directly call the private method of the parent class, but using The way of reflection can be called. Fields are the same.


  • 7. Overriding and overloading of methods:

  • Overloading, simply put, is the situation where functions or methods have the same name but different parameter lists. Such functions or methods with the same name and different parameters are called overloaded functions or methods.

    • (1) Method overloading is a means for a class to handle different types of data in a uniform way. Multiple functions with the same name exist at the same time, with different number/types of parameters. Overloading Overloading is a manifestation of polymorphism in a class.
    • (2) Java's method overloading means that multiple methods can be created in a class, they have the same name, but have different parameters and different definitions. When calling methods, it is determined which method to use by the different number of parameters and parameter types passed to them, which is polymorphism.
    • (3) When overloading, the method name must be the same, but the parameter type and number are different, and the return value type can be the same or different. The return type cannot be used to distinguish overloaded functions.
  • In object-oriented programming, subclasses can inherit methods from the parent class without rewriting the same methods. But sometimes the subclass does not want to inherit the method of the parent class intact, but wants to make certain modifications, which requires method rewriting. Method overriding is also known as method overriding.

    • (1) The polymorphism between the parent class and the child class redefines the functions of the parent class. If a
      method is defined in a subclass with the same name and parameters as its parent class, we say the method is overriding. In Java, subclasses can inherit methods from the parent class without rewriting the same methods. But sometimes the subclass does not want to inherit the method of the parent class intact, but wants to make certain modifications, which requires method rewriting. Method overriding is also known as method overriding.
    • (2) If the method in the subclass has the same method name, return type and parameter list as a method in the parent class, the new method will overwrite the original method. If you need the original method in the parent class, you can use the super keyword, which refers to the parent class of the current class.
    • (3) The access modification authority of the subclass function cannot be less than that of the parent class;

      The need to override the method is because: the function of the parent class cannot meet the needs of the subclass.

The premise of method overriding: There must be an inheritance relationship.

Method rewriting: The sub-parent class has a function with the same name, which we call method rewriting.

When to use method overriding: When the function of the parent class cannot meet the needs of the child class.

Points to note when overriding methods:

    • 1. When a method is overridden, the method name and the formal parameter list must be consistent.
    • 2. When the method is overridden, the permission modifier of the subclass must be greater than or equal to the permission modifier of the parent class.
    • 3. When the method is overridden, the return value type of the subclass must be less than or equal to the return value type of the parent class.
    • 4. When the method is overridden, the exception type thrown by the subclass should be less than or equal to the exception type thrown by the parent class.
      • Exception (worst)
      • RuntimeException(小坏)

Method overloading: There are two or more functions with the same name in a class, which is called method overloading.

Method overloading requirements

      1. The function name must be the same.
      1. Inconsistent parameter lists (inconsistent number of parameters or types of parameters)
      1. Regardless of the return value type.

  • 8. About the construction method:

    • Constructors cannot be modified by static, final, synchronized, abstract, or native, but can be modified by public, private, and protected;
    • Constructors cannot be inherited

Guess you like

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