1. Single Inheritance
Java inheritance is a single inheritance (only a parent class), a is-a relationship between a derived class and a base class (that is, "cat" is a "animal"). 2. Benefits of Inheritance
The subclass has all the properties and methods of the parent class (the modifiers for the property and method cannot be private)
Implementing code Reuse 3. Grammar rules
Class Subclass extends Parent class
For example:
Class Dog extends animal{
.....
4. Override of Method
Subclasses can override methods that inherit the parent class, calling methods that give precedence to the subclasses.
Requirements for rewriting:
return value types, method names, parameter types, and numbers are the same methods that the parent class inherits.
Parent class
Package Duotai;
Public class Transportation {public
String transport;
public int maxnum;
public void Detail () {
System.out.println ("detail:" +transport+ "-" + maxnum);
Sub-class
package duotai;
public class Bus extends transportation {public
void detail () {
System.out.println ("Busdetail: +transport+" -"+ Maxnum");
5. Abstract classApplication scenario: The method of constraint class and the implementation of unconstrained method
The parent class knows which methods the subclass contains, but the parent class does not know how the subclasses implement these methods. An abstract class is abstracted from a class with the same characteristics, and the abstract class is the template of subclasses to avoid the arbitrariness of the subclass design. Working with rules
Abstract defines an abstraction class. Abstract defines an abstraction method that is only declared and does not need to be implemented. The class that contains the abstract method is an abstract class. An abstract class can contain either a common method or an abstract method. Abstract classes cannot be created directly, and reference variables can be defined.
Abstract class: Shape subclass: Rectangle, Circle
Abstract class: Shape public
abstract class Shape {
//abstract method has no method body ending public
abstract void length () with a semicolon;
public abstract void Area ();
}
Subclass: Rectangle Public
Class Rectangle extends shape {public
void length () {
System.out.println ("Lengtg of Rectangle ");
}
public void Area () {
System.out.println (' area of Rectangle ');
}
Subclass: Circle Public
class Circle extends shape {public
void length () {
System.out.println ("Lengtg of Circle ");
}
public void Area () {
System.out.println (' area of Circle ');
}
Main function Public
class Initail {public
static void Main (string[] args) {
shape SHA1 = new Rectangle ();
Sha1.length ();
Sha1.area ();
Shape SHA2 = new Circle ();
Sha2.length ();
Sha2.area ();
}
6 Initialization order of inheritance
7 Final keyword the relationship between final and class, method, attribute and variable
Final modifier class: This class is not allowed to be inherited.
Final Cosmetic method: This method is not allowed to be overridden (overwritten)
Final modifier Properties:
The properties of this class cannot be implicitly initialized, that is, the initialization property of the class must have a value.
or assign a value in the constructor method.
Final modifier variable, the value of this variable can only be assigned one time and cannot be changed. is the constant 8 Super keyword
Use Super in Java classes to refer to the composition of the parent class, using this to refer to the current object.
Parent class public class
Animal {public
int num = ten;
public String driver;
public void Car () {
System.out.println ("Super"). Drivernumis: "+num);
}
}
Subclass public
Class Dog extends Animal {public
int num =;
Override method public
void car () {
System.out.println ("Drivernumis:" +num);
}
public void Method () {
System.out.println ("num is:" +num);//The value of the subclass
System.out.println ("Spuer.num is:" + Super.num);//super call the value of the parent class car
();
Super.car ()///The method of calling the parent class through Super} is
initialized: Public
class Init_pro {public
static void main ( String[] args) {
Dog Dog =new Dog ();
Dog.method ();
}
Results:
num is:20
spuer.num is:10
drivernumis:20
Super. Drivernumis:10
9 Object Class (parent class of all classes)ToString () method
The hash code (object address string) of the object returned when the ToString () method is defined inside the object class.
public static void Main (string[] args) {
Dog Dog =new Dog ();
System.out.println (dog);
}
Result:
COM.JICHENG.DOG@659E0BFD
Equals () method
Compares whether a reference to an object refers to the same memory address. The return value is a Boolean type.
public static void Main (string[] args) {
Dog Dog =new Dog ();
Dog dog2 = new Dog ();
if (Dog.equals (dog2)) {
System.out.println ("Same");
} else {
System.out.println ("Dif");
}
}
Result: Dif
All two methods can be generated automatically in the Source menu bar.
A description of the automatically generated equals () method
The address of public boolean equals (Object obj) {
if (this = = obj)//object is the same as return
true;
if (obj = = null)//One of the addresses is null return
false;
if (GetClass ()!= obj.getclass ())
//Get a class object specifically see the following figure return
false;
Dog other = (Dog) obj; Convert to Dog class
if (num!= other.num) return
false;
return true;
}
After you override the Equals () method in a subclass, you can compare the values of the variables in the class to the same value.
The data types in Java can be grouped into two categories:
1. The basic data type, also known as the original data type. Byte,short,char,int,long,float,double,boolean
The comparison between them applies the double equals sign (= =) and compares them to their values.
2. Composite data type (Class)
When they compare with (= =), they compare their store address in memory, so unless the same object is new, their comparison results to true, otherwise the result is false. All classes in Java are inherited from the base class of object, and a method of equals is defined in the base class in object, and the initial behavior of this method is to compare the memory address of the object, but in some class libraries the method is overwritten (rewritten). (= =) is not overridable.
= = compares the addresses of 2 objects, while equals compares the contents of 2 objects.