What is inheritance (extends)?
Inheritance is: a newly defined class is a phenomenon of obtaining attributes and methods from existing classes. This existing class is called the parent class, and the class that obtains properties and methods from this parent class is called a subclass.
ExtendsDemo1.java
The code copy is as follows:
/*What is inheritance*/
public class ExtendsDemo1 {
public static void main(String[] args) {
Truck t = new Truck();
t.size = 100; //This is not recommended. It is best to use constructors to initialize member variables, or provide set() and get() interfaces.
// An instance of the truck class inherits the size and color attributes from the car class.
//Which truck has one more cargo box than car
}
}
class Car { //Car
int size; //Body body size
String color; //color
}
class Truck extends Car { //Truck
String packingBox; //Cargo box
}
The benefits of inheritance
1. Improved code reusability
2. Let a relationship between classes creates a condition for polymorphism.
Inherited (extends) format
The code copy is as follows:
class SubClass extends SuperClass {
//Execute statement;
}
super keywords
1. The usage of the super keyword is the same as this
2. This represents the reference of this class, and super represents the reference of the parent class
3. When a member of the same name appears in the subclass and parent class, you can use super and this to distinguish it.
SuperDemo.java
The code copy is as follows:
/* Usage of super
* Output result:
* super.i = 10, this.i = 20
*/
public class SuperDemo {
public static void main(String[] args) {
new SubClass().showMessage();
}
}
class SuperClass {
int i = 10;
}
class SubClass extends SuperClass {
int i = 20;
public void showMessage() {
System.out.printf("super.i = %d, this.i = %d/n", super.i, this.i);
}
}
Override methods inherited from parent class (Override)
1. When a method exactly the same as the parent class appears in a subclass (return value, function name, formal parameters), an override operation will occur.
OverrideDemo1.java
The code copy is as follows:
/* How to override the method inherited from the parent class (Override)
* Output result:
* SuperClass: I am good~
* SubClass: I am excellent~~~
*/
public class OverrideDemo1 {
public static void main(String[] args) {
SubClass sc = new SubClass();
sc.speak();
}
}
class SuperClass {
public void speak() {
System.out.println("SuperClass: I am good~");
}
}
class SubClass extends SuperClass {
@Override //@Override means that the following method will have a rewrite operation. The compiler checks it. If the rewrite condition is not met, an error will be reported. Increased code security to some extent
and robustness
public void speak() {
super.speak();
System.out.println("SubClass: I am excellent~~~");
}
}
2. When override the parent class method, the child class does not allow methods with the same name and parameters as the parent class but with different return types.
OverrideDemo2.java
The code copy is as follows:
/* Methods with the same name and parameters as the parent class but with different return types are not allowed in subclasses.
* Error message:
* OverrideDemo.java:20: error: f() in SubClass cannot override f() in SuperClass
* public int f() {
* ^
* return type int is not compatible with void
* OverrideDemo.java:19: error: method does not override or implement a method from a supertype
* @Override
* ^
* 2 errors
*/
public class OverrideDemo2 {
public static void main(String[] args) {
}
}
class SuperClass {
public void f() {
}
}
class SubClass extends SuperClass {
@Override
public int f() {
return 1;
}
}
3. The access permission of the method of the subclass rewrites the parent class must be greater than or equal to the method of the rewrite in the parent class.
OverrideDemo3.java
The code copy is as follows:
/* The access permission of the method of the subclass overrides the parent class must be greater than or equal to the access permission of the parent class method
* Error message:
* OverrideDemo.java:18: error: f() in SubClass cannot override f() in SuperClass
* protected void f() {
* ^
* attempting to assign weaker access privileges; was public
* 1 error
*/
public class OverrideDemo3 {
public static void main(String[] args) {
}
}
class SuperClass {
public void f() {
}
}
class SubClass extends SuperClass {
@Override
protected void f() { //Change the access permission here to public
}
}
Access Controller
Access control characters are: public, protected, default, private
Some require a little knowledge of packages, so I will talk about it when I talk about packages.