Java object-oriented (inheritance and polymorphism)

1. Inheritance

Concept of inheritance

Inheritance means that the subclass inherits the characteristics and behaviors of the parent class, so that the subclass object (instance) has the instance domain and methods of the parent class, or the subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class.

Insert picture description here
Although both herbivores and carnivores are animals, there are differences in the attributes and behaviors of the two, so the sub-category will have the general characteristics of the parent category and will also have its own characteristics.

Use of inheritance

The key word inherited in Java is extends
to extract the public properties and behaviors and place them in the parent class, and the subclass extends the parent class to achieve more features. This saves a lot of code, the program becomes more concise, and easy to expand. .

E.g

class 父类 {
    
    
}
 
class 子类 extends 父类 {
    
    
}

It should be noted that Java does not support multiple inheritance, but supports multiple inheritance.

Insert picture description here

Two, polymorphism

Polymorphism is the ability to have multiple different manifestations or forms of the same behavior.

Advantages of polymorphism:

  1. Replaceability
  2. Scalability
  3. Interface
  4. flexibility
  5. Simplification

Three necessary conditions for the existence of polymorphism

  1. inherit
  2. Rewrite
  3. The parent class reference points to the child class object

Method coverage (overwrite)

The subclass needs to improve the operation method of the parent class and become the subclass's own operation method. This requires rewriting the operation method in the subclass to override the operation method of the parent class. This approach is called method coverage in JAVA.

For example, defined in the parent class

public String show(){
    
    
    return "";
}

And in the subclass

public String show(){
    
    
        return "名称:"+super.getName()+"\n价格:"+super.getPrice()+"元"+"\n大小:"+super.getSize()+"寸";
    }

The difference between method overloading and overriding:
overloading can occur in any class, and overriding can only be the subclass overriding the parent class method.

Super keyword

The Super keyword refers to the properties and methods in the direct parent class of the current object (used to access the hidden properties and methods in the direct parent class, and is often used when the base class and derived classes have the same property and method definitions)
except for calling the parent In addition to methods with the same name, super is also widely used in construction methods.
We can write super() freely, and we must follow certain principles:
1. Super must be placed in the first line
2. The construction method directed by super must be in the parent class.

public PizzaSeafood(String name,int size, int price) {
    
    
        super(name,size, price);
        ...
    }

Final keywords

  1. Final modification of the class, then this class cannot be inherited.
    Insert picture description hereInsert picture description here

  2. Final modification method, then this method cannot be overridden.
    Insert picture description hereInsert picture description here

  3. Final modifies the attribute so the attribute value cannot be modified,

  4. Final modifies the variable. Once the variable is assigned, the variable cannot be modified

Guess you like

Origin blog.csdn.net/s001125/article/details/110095492