Concept of Inheritance
-Inheritance is one of the three features of object-oriented and an important means to achieve software reuse.
-Through inheritance, you can define new classes based on existing classes. New classes have all the functions (attributes and methods) of existing classes)
-Java inheritance is implemented by using the extends keyword. The inherited class is called a subclass (derived class), and the inherited class is called a parent class (base class, superclass)
-The relationship between the parent class and the subclass is a general and special relationship.
Inherited syntax
Class childClass extends parentClass
{
// Class body
}
Example
Public class Person {
Public String name;
Public String getName (){
Return name;
}
}
Public class Employee extends Person {
Public int employeeNumber;
Public int getEmployeeNumber (){
Return employeeNumber;
}
}
Public class Manager extends Employee {
Public String responsibilities;
Public String getResponsibilities (){
Return responsibilities;
}
}
Note: The subclass cannot directly access the private attributes and methods inherited from the parent class, but the public (and protected) methods can be used for access.
Public class B {
Public int a = 10;
Privateintb = 20;
Protected int c = 30;
Public intgetB () {return B ;}
}
Public class A extends B {
Public int d;
Public void tryVariables (){
System. out. println (a); // allow
System. out. println (B); // not allowed
System. out. println (getB (); // allow
System. out. println (c); // allow
}
}
3. Method overloading and overwriting
Heavy Load
-Java allows multiple methods with the same name to be defined in the same class, as long as the parameter list is different. If the same class contains two or more methods with the same method name but different parameters, it is called method overload.
-There are two different requirements for method overloading: the same class has the same Chinese name and the parameter list is different. Other parts of the method, such as the type and modifier of the return value of the method, have nothing to do with method overloading.
-Because the return value can be ignored in Java method calls, the return type cannot be used to differentiate the overloaded methods.
Override)
-If the subclass does not need to use the function of inheriting methods from the parent class, it can declare its own method with the same name, called method override.
-The return type, method name, number and type of parameters of the override method must be the same as that of the overwritten method.
-You only need to use different class names or object names before the method names to distinguish between the overwriting method and the overwriting method.
-The access permission of the overwriting method can be loose but not stricter than that of the overwriting method.
Application scenarios of method Overwriting
-Sub-classes implement the same functions as parent classes, but different algorithms or formulas are used.
-In methods with the same name, do more things than the parent class.
-Remove the method inherited from the parent class in the subclass.
Method that must be overwritten
-The derived class must overwrite the abstract methods in the base class. Otherwise, the derived class itself becomes an abstract class.
Method that cannot be overwritten
-Final termination method in the base class
Call the overwriting Method
-Super. overriddenMethodName ();
Public class Ostrich extends Bird {
Public void fly (){
System. out. println ("I can only run on the ground ...");
}
Public void callOverridedMethod (){
// Fly ();
Super. fly ();
}
Public static void main (String args []) {
Ostrich OS = new Ostrich ();
// Do something
}
}
Parent class objects are implicitly created when subclass objects are created.
Class Father {
Public Father (){
System. out. println ("Father Create ");
}
}
Public class Child extends Father {
Public Child (){
System. out. println ("Child Create ");
}
Public static void main (String args []) {
Father fa = new Fahter ();
Child ch = new Child ();
}
}
Output:
Father Create
Father Create
Child Create
The following method is a method of the base class:
Void method (){}
Which of the following is the correct form of overwriting the method in the subclass?
Select all the correct answers (multiple answers ):
A. void method (){}
B. int method () {return 0 ;}
C. void method (inti ){}
D. private void method (){}
A. The basic principle of method overwriting is to keep the parameter list and return value type unchanged. B is incorrect because the return types are inconsistent. C is wrong because the parameter list is inconsistent. D is wrong, because the override method in the subclass reduces the access control scope.
Constructor methods for inheritance follow the following principles:
-The subclass cannot inherit the constructor from the parent class.
-A good programming method is to call a parent class constructor In the constructor of the subclass. The Calling statement must appear in the first line of the subclass constructor. The super keyword can be used.
-If the parent class constructor is not explicitly called in the subclass constructor declaration, the system automatically calls the default constructor of the parent class when executing the constructor of the subclass (that is, the constructor without parameters)
Differences between heavy load and overwrite
-The overload methods are independent of each other, but they are essentially different methods. The overwriting and overwriting methods are substituted.
-The number of overloaded methods in the same class is unlimited. The overwrite method and the overwrite method exist in the subclass and parent class, and each subclass has a maximum of override methods.
-Duplicate methods must have different parameter lists, while overwrite and overwrite methods must have the same parameter lists.
Hide and encapsulate
-The State information of an object is hidden inside the object. External programs are not allowed to directly access the internal information of the object. Instead, the internal information is operated and accessed through the methods provided by this class.
-For example, the age attribute of the Person object can only be increased as the time passes. Generally, the age attribute of the Person object cannot be modified at will.
To achieve good encapsulation:
-Hide the implementation details of a class
-Allows users to access data only through pre-defined methods, so that they can add control logic to this method to limit unreasonable access to attributes.
-Data can be checked to ensure the integrity of object information.
-Easy to modify, improving Code maintainability
Access Controller
Public protected default private
Similar O
Same package O
Subclass O
General O
-The setter and getter methods of attributes in Java classes are of great significance. For example, a class contains an attribute abc, the corresponding setter and getter methods should be setAbc and getAbc. If every attribute of a Java class is modified using private, and the public modifier setter and getter methods are provided for each attribute, this class is a class conforming to the JavaBean specification. Therefore, JavaBean is always a well-encapsulated class.
L in order to achieve good encapsulation, we need to consider two aspects:
-Hides the attributes and implementation details of an object and does not allow direct external access.
-Expose methods to allow methods to operate or access these attributes.