Inherited
1, the concept of inheritance
Inheritance is a cornerstone of Java object-oriented programming technology because it allows classes of hierarchical hierarchies to be created.
Inheritance is the child class inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class's instance domain and method, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.
1. Class inheritance Format
In Java, the extends keyword can be used to declare that a class is inherited from another class in the following general form:
class classextends parent class {}
2. Characteristics of inheritance
-
-
Subclasses have properties that are not private to the parent class, methods.
-
Subclasses can have their own properties and methods, that is, subclasses can extend the parent class.
-
Subclasses can implement methods of the parent class in their own way.
-
Java inheritance is a single inheritance, but can be multiple inheritance, single inheritance is a subclass can inherit only one parent class, multiple inheritance is, for example, Class A inherits Class B, Class B inherits Class C, so the Class C is the parent class of Class B, and Class B is the parent class of Class A, which is a feature that differs from C + + inheritance.
-
Improved coupling between classes (inheritance disadvantage, high coupling will result in a link between the code)
3. Inherit keywords
Inheritance can use the two keywords extends and implements to implement inheritance, and all classes are inherited from Java.lang.Object, when a class does not inherit two keywords,
The object is inherited by default (this class is in the Java.lang package, so there is no need for an import) ancestor class.
Extends keywords
In Java, the inheritance of a class is a single inheritance, that is, a subclass can only have one parent class, so extends can inherit only one class.
public class Animal { private String name; private int id; Public Animal (String myName, String myID) { //Initialize property value } public void Eat () { //the specific implementation of the eating method } public void Sleep () {//Specific implementation of sleep method }} public class Penguin extends animal{}
Implements keywords
The use of the Implements keyword can be disguised to make Java has multiple inheritance, using the scope of the class to inherit the interface, you can inherit multiple interfaces (interfaces and interfaces separated by commas).
Implements Key Words Public Interface A { publicvoid eat (); Public void public interface B { publicvoid Public class Implements A, b {}
2, Java constructor 1, the concept
Also known as a construction method, the constructor constructor is used to construct an instance of the class.
A constructor is a special method that is called automatically when an object is created in order to initialize it. The name of the constructor should match the name of the class.
When objects are created, the system initializes the properties of the object by default, the value of the base type property is 0 (numeric type), False (Boolean type), and all reference types are set to NULL.
The constructor can change this default initialization.
2. Detailed description of the syntax format of the constructor
modifier : The modifier can be omitted, or it can be one of the public,protected,private
Constructor Name: The constructor name must be the same as the class name
formal parameter list : Identical to the format of the definition method parameter list
Indicates that the constructor cannot define a return value type declaration, and cannot declare that the void definition constructor does not have a return value.
If a return value type is defined for the constructor, or if a void is used to define the constructor without a return value, compile without error
But Java will treat this so-called constructor as a method.
3, the difference between the constructor and the method
4. Application
Example Demo: Calculate the distance from a coordinate point
Entity class Point Code:
1 Public classPoint {2 3//properties of points horizontal ordinate4intx;5inty;6 7//default no parameter constructor8 PublicPoint () {9 System.out.println ("Default construct. ");10 }11 12//The structure of the reference 113 PublicPoint (intAintb) {System.out.println ("with a reference structure. ");15//Assigning a value to an objectx =A;y =b;18 }19//Parametric Construction 2 (heavy duty)20 PublicPoint (inta) {21st//System.out.println ();22 This(A,a);//the overloaded call above has the parameter construct 1 (this syntax is specifically used to construct a call between methods and must be written on the first line of the constructor method)23 }24 25/**26 * Point Distance calculation method 27 * Parameters are: Non-reference point sitting Punctuation Object 28 * Method overload*/30 Public DoubleDistance () {//No reference31returnMath.sqrt (Math.pow ( This. x, 2) + Math.pow ( This. Y, 2));//the distance from the original point32 }33 34//the distance to a point35 Public DoubleDistanceintAintb) {//parameter is point coordinate36returnMath.sqrt (Math.pow ( This. X-a, 2) +math.pow ( This. Y-b, 2));37 }38 39 Public DoubleDistance (point P) {//parameter is a point object40returndistance (p.x, p.y);41 }42 43}
Printdemo
1 Public classPointdemo {2 Public Static voidMain (string[] args) {3 4 Point p1 =NewPoint (3,2); 5 System.out.println ("P1 coordinates are:" + "(" + p1.x + "," + P1.y + ")"); 6 7 Point P2 =NewPoint (5); 8 System.out.println ("P2 coordinates are:" + "(" + p2.x + "," + P2.y + ")"); 9 10/**11 * Point distance*/Point P =NewPoint (6,8);System.out.println ("Distance to Origin:" + p.distance ());//the distance from the original pointThe distance from System.out.println ("to another point is:" + p.distance (3, 3));//distance to (3,3)System.out.println ("Distance to a Point object:" + p.distance (p2));//distance to point object P217 }18}
The output is:
There is a reference structure.
The coordinates of P1 are: (3,2)
There is a reference structure.
The coordinates of P2 are: (5,5)
There is a reference structure.
The distance from the origin point is: 10.0
Distance to another point is: 5.830951894845301
The distance to a point object is: 3.1622776601683795
3. This and super 1, this keyword in Java
The This keyword represents the current object. Can be used to:
-
- Invokes the constructor of the current class, and must be the first statement of the method. such as: this (); Call the default constructor method. This (parameter); Call the Take parameter construction method.
- Restricts the data field variables of the current object. Typically used in cases where local variables within a method have the same name as the object's data field variable. such as This.num = num. This.num represents the data field variable num for the current object, and num represents a local variable in the method.
2. Super keyword
Super represents the parent class of the class that uses it. Super can be used to:
-
- Call the constructor method of the parent class;
- The method that invokes the parent class (when the subclass overrides the method of the parent class);
- Access the data fields of the parent class (this can be used, but not necessary).
Call the constructor method syntax for the parent class:
Super (); or Super(parameter list);
Note : The super statement must be the first statement of the subclass construction method. You cannot use the parent class to construct a method name in a subclass to invoke the parent class constructor method. The construction method of the parent class does not inherit from the quilt class.
The only way to invoke the constructor of the parent class is to use the Super keyword, and if it is not explicitly called in the subclass, the compiler will automatically turn super (); The first statement to construct the method as a subclass.
This forms a chain of construction methods.
A super keyword cannot be used in a static method.
Call the method syntax of the parent class:
Super. Method name (parameter list);
If the method is inherited, it is not necessary to use super to invoke, directly can be called. However, if a subclass overrides or overrides a method of the parent class, only super can be used to invoke the overridden method in the parent class in the subclass.
Java Fundamentals-Inheritance