Java as a high-level programming language, inheritance, polymorphism, encapsulation, abstraction is its four characteristics. In this article, let's look at inheritance first.
First, let's look at the definition of inheritance: inheritance is to build a new class based on the existing class, the new class is called the subclass of the existing class, the original class is called the parent class, and the subclass extends its own methods and properties on the basis of inheriting the properties and methods of the parent class. So subclasses can reuse the methods and properties of the parent class. We will explain the rewriting of the method below. In Java, only single inheritance is allowed, that is, a class can have only one parent class, and cannot have two or more parent classes.
Method Override: A method override occurs between a subclass and a parent class, with the same method name as the parent class, the same argument list, and the same return type. When the method is called, the subclass override method is called first. If the subclass does not override the method of the parent class, then the method inherited from the parent class is called. Detects whether subclasses override the parent class's method with @override, and if the subclass does not override the parent class's method, it will report a compilation error. Note that when a subclass overrides a parent class method, the access modifier of the subclass method is greater than or equal to the access modifier of the parent class method, and the private method defaults to final, and cannot be overridden by subclasses (cannot be inherited).
The range size of the access modifier can look like this:public>protected> default >private. Where a child class object contains a complete parent class object, private properties and method subclasses can be inherited. Only, it cannot be accessed directly, but can be accessed through the parent class public method. That is: the Get/set method. Write-only get methods represent read-only, write-only set methods that represent write-only, which guarantees the security of private properties, which can be guaranteed by restricting the set method.
Super statement function and usage: Super represents the object of the parent class, super () is the first sentence of the subclass construction method, which indicates that the child class object must be created by super () to call the parent class's constructor, resulting in the parent class object. The first sentence of the subclass construction method uses Super () by default, calling the parent class without a parameter construction method to produce the parent class object. However, if the parent class does not provide an parameterless construction method, the child class must display the call through Super (argument).
Inheritance of four features of Java object-oriented