Object-oriented programming allows new classes to be defined from existing classes, which is called inheritance.
The process-oriented paradigm focuses on the design of the method, while the object-oriented paradigm combines the data and methods in the object. The object-oriented paradigm of software design focuses on the object and the operation on the object. The object-oriented approach combines the power of a process-oriented paradigm and further integrates data and operations into objects.
Inheritance allows you to define a generic class (that is, the parent class) and then extend the class to a more specific class (that is, a subclass). In Java terminology, if the class C1 extends from another class C2, then C1 is called the secondary class, and C2 is called the superclass. A superclass is also called a parent or base class, and a secondary class is called a subclass, an extension class, or a derived class.
Subclasses inherit accessible data fields and methods from its parent class, and you can add new data fields and new methods.
If there is a private data field in the parent class, the class is inherited, and the private data fields in the parent class are not accessible to the class, and the only way to read and change them is through their get and set methods.
Unlike traditional understanding, subclasses are not a subset of the parent class. In fact, a subclass usually contains more information and methods than its parent class.
Private data fields in the parent class are inaccessible outside of the class. Therefore, it cannot be used directly in subclasses. However, if the public accessor/modifier methods are defined in the parent class, they can be accessed and modified through these public accessors/modifiers.
Not all "is one" relationships should be modeled with inheritance. Inheritance is used to model A "is a" relationship. Don't blindly extend a class just for the sake of reusing the method.
Some programming languages are allowed to derive a subclass from several classes. This ability becomes multiple inheritance. However, multiple inheritance is not allowed in Java. A Java class can only be inherited directly from a parent class. This restriction is called single inheritance. If you use the Extends keyword to define a subclass, it allows only one parent class. However, multiple inheritance can be achieved through an interface.
The keyword super refers to the parent class, which can be used to invoke common methods and construction methods in the parent class.
Construction methods are used to construct an instance of a class. Unlike attributes and normal methods, the parent class's construction method does not inherit from the quilt class. They can only be called from the constructor of a subclass using the keyword super. The syntax for calling the parent class constructor is: Super (), or super (parameters), the statement super () calls the parameterless constructor of the parent class, and the statement super (arguments) invokes the constructor of the parent class that matches the argument. The statement super () and super (arguments) must appear in the first row of the subclass construction method, which is the only way to show that the parent class constructor method is called.
To invoke the constructor of the parent class, you must use the keyword super, and the call must be the first statement of the constructed method. Calling the parent class in a subclass to construct the name of the method will cause a syntax error.
The constructor method can call the overloaded constructor method or the construction method of the parent class. If none of them are called, the compiler automatically takes super () as the first statement of the construction method.
Subclasses must use super to invoke the constructor of the parent class, and if no calls are made, use super (), call the parent class without arguments, and if the parent does not have a constructor without parameters, it must display the super with the parameter (parameters) Invokes the constructor method of the parent class.
In general, it is better to provide a parameterless construction method for each class so that the class can be extended to avoid errors.
In any case, when you construct an instance of a class, the constructor of all the parent classes along the inheritance chain is called. When you construct an object of a subclass, the subclass constructor method first calls the constructor of its parent class before completing its own task. If the parent class inherits from another class, the parent class construction method will then call its own constructor of the parent class before completing its own task. This process continues until the last constructed method along the inheritance architecture is called. This is the construction method chain.
The keyword Super can refer not only to the constructor of the parent class, but also to the method of the parent class.
Subclasses inherit methods from the parent class. Sometimes a subclass needs to modify the implementation of a method defined in the parent class, which is called a method override.
Method overrides: To override a method, you need to define the method by using the same signature as the parent class and the same return value type in the subclass.
It can be overwritten only if the instance method is accessible. Because the private method is inaccessible outside of itself, it cannot be overwritten. If the methods defined in the subclass are private in the parent class, then the two methods are completely non-relational.
As with instance methods, static methods can also be inherited. However, static methods cannot be overwritten. If the static method defined in the parent class is redefined in the subclass, the static method defined in the parent class is hidden. You can use the syntax: parent class name. Static method names invoke hidden static methods.
Overloading means defining multiple methods with the same name but with different signatures. Overloading means providing a new implementation of the method in the subclass.
Method overrides occur in different classes that are related through inheritance, and method overloads can occur in the same class. can also occur in different classes that are related to inheritance.
A method override has the same signature and return value type, and the method overload has the same name but a different argument list.
Overloaded methods allow the same names to be used to define different methods, as long as their signatures are different. (The method name and the parameter list together form the method signature, where the method name is the same, and the overloaded method requires a different argument list.) )
The overloaded method must have a different parameter list. Methods cannot be overloaded based on different modifiers or return value types.
---restore content ends---
Java Basics-Inheritance and polymorphism