I. Inheritance (inheritance) 1. Concept of inheritance
inheritance: in the process of object-oriented programming, a new class is created by extending an existing class and inheriting the class's properties and behavior.
Inheritance is one of the most important features of object-oriented programming.
Benefits of Inheritance:
1). Avoid a lot of duplicate code.
2). Inheritance is the expansion of functions, making the structure clear. Easier to maintain and modify.
Parent class: Previously existing classes, also known as base class, superclass (superclass)
Subclass: A new class, also called a derived class.
1. Subclasses inherit everything from the parent class (not including private-decorated classes) methods and properties.
Only single inheritance in 2.Java can be inherited by extends.
Test Design: An object has a property (has a), an object executes (does) a behavior.
2. Judging inheritance using "is a" relationship
When using inheritance, if it is said that the subclass "is a" parent class, is established. Then the inheritance relationship is right.
3. Inheritance of Class (extends) "3W", "1H":
What : inheritance of Parent class methods and properties in Java-functional expansion
Where: When there is a lot of duplicate code
Why : avoid a lot of repetitive code, make the code structure clear, easier to maintain and modify.
How : Use the keyword extends to inherit the parent class.
1) Parent class (base class/superclass): Provides inheritable properties and methods for subclasses.
code example:
public class Pet { String name; int age; String color; Public Pet () { System.out.println (in the constructor); }
/** in the form of parameters: * Public Pet (String name) {* System.out.println (name); * }
* /public void Show () { }}
2) Subclass: subclasses inherit everything from the parent class (excluding private-decorated classes) methods and properties.
code example:
public class Cat extends pet{public Cat () { super (); --------------------- Call the non-parametric constructor of the parent class //or super (name); ------------ Call the parent class with the parameter constructor System.out.println ("Meow to eat Fish"); } @Override public Void Show () { System.out.println (name+ "," +age "); System.out.println ("Meow Meow");} }
Only single inheritance in 1.Java can be inherited by extends.
2. If the parent class has arguments, the child class must also have parameters and return to the parent class. Otherwise, the program cannot execute
3.this (); In conflict with super () , they must all be placed in the first line to take effect.
4. Single inheritance and multiple inheritance
Some OOP languages, such as C + +, allow multiple inheritance.
However, only single inheritance can be allowed in Java, inherited through extends.
Therefore, there is only one parent class for each Java class.
5. Root classes for all classes
There is a special class object in the Java language API, which is the root class in the entire Java class hierarchy.
The object class is the parent class of each Java class (either a direct parent class or an indirect parent class). So, regardless of the class type, the methods in object can be called by any object.
6. Overloading and rewriting of methods
1) Overloading: only related to the number and type of parameters. Constructors can also overload
code example:
public void Show (int a) { System.out.println (a); } public void Show (int a,int b) { System.out.println (a+b); } public void Show (int a,double b) { System.out.println (a+b); }
Overloading can be easily handled for different situations of the same behavior.
2) Rewrite: subclasses can override methods inherited from the parent class, allowing subclasses to add or change the behavior of the methods in the parent class, which is one of the OOP features.
◇ Rules:
A. The return value type, method name, and formal parameter list of a method of a subclass must be the same as the parent class.
B. The access modifier must be not less than the access modifier of the parent class.
C. An exception that is overridden in a subclass cannot throw more exceptions than the parent class.
public class Pet { String name; int age; String color; Public Pet () { System.out.println (in the constructor); } /** in the form of a parameter: * Public Pet (String name) {* System.out.println (name); * }*/public Void Show () { }} public class Cat extends pet{public Cat () { super (); ----------------------- Call the non-parametric constructor of the parent class //or super (name); ------------ Call the parent class with the parameter constructor System.out.println ("Meow to eat Fish"); } @Override public Void Show () { System.out.println (name+ "," +age "); System.out.println ("Meow Meow");} }
1. The overridden method name and parameter must be identical. Must occur between the parent class and the child class.
2. Overrides are required when the parent class's methods do not fit in the subclass.
7.super keywords
1) This reference: Each object has a reference to itself, the this reference.
2) Super Reference: A class can use the Super keyword to explicitly refer to member variables and methods inherited from the parent class. At the same time, if there are arguments, the arguments are passed to the parent class.
1. When the subclass overrides the parent class method, the Super keyword is required if you need to invoke the method of the parent class.
Both 2.this and Super keywords must be written in the first line of code, so you can select only one.
8.final keywords
1) Final constant: The value cannot be modified.
2) Final class: A class can be declared as the final class. The final class cannot be inherited by another class.
Public final class Dog extends pet{}
3) Final method: a method can be declared as the final method. The final method cannot be overridden.
public class pet{ String name; int age; Public final void Show () {} System.out.println (name+ "," +age);}
9. The process of instantiation
When an object is instantiated:
1) The new operator calls the constructor of the subclass
2) Subclasses call other constructors of subclasses with the This keyword. Finally, the constructor in the first line of code in the subclass that is not this () is called.
3) The constructor of the parent class must be called with the Super keyword before any statements in the child class's constructor are executed. If you do not use the super call explicitly, the compiler calls the parameterless parent constructor with super ().
4) If the parent class is also a subclass of another class, the parent class constructor must use super to call the constructor of a higher-level parent class before any statements in the parent class's constructor are executed.
5) This procedure continues until it reaches the top of the class hierarchy tree, which is the object class.
6) The constructor in the object class executes, and then the control flow goes to the constructor of the class under object in the inheritance hierarchy tree.
7) The constructor is executed down the inheritance hierarchy tree, and the last constructor to be executed is actually the one that was called first in the first step.
In each constructor, if you want to call super () or this (), the calling code must be the first line of code in the constructor
JAVA Object-oriented inheritance (inheritance)