Three characteristics of object-oriented: inheritance, encapsulation and polymorphism
inherit
Inheritance makes it easier to implement class extensions. For example, if we define a human, then we only need to extend the human to define the Boy class. The code reuse is realized, and don't need to reinvent wheels.
Knowledge points
- Parent class is also called base class and superclass
- In Java, classes have no multiple inheritance, only single inheritance, and interfaces have multiple inheritance
- The child class inherits the parent class and can get all the properties and methods of the parent class (except the construction method of the parent class), but not all of them can be accessed
You can use Ctrl+T to easily view the inheritance hierarchy of a class
extend keyword
From the literal meaning of English, extends means "extension". A subclass is an extension of a parent class.
instanceof keyword
instanceof is a binary operator, with objects on the left and classes on the right. When an object is created by a class or subclass on the right, it returns true. Otherwise, it returns false.
package OOprogress; /** * Test inherits extend and instanceof * @author DELL * */ public class testExtendAndInstanceof { public static void main(String[] argd) { studentSimple stu = new studentSimple(); stu.name = "Yang Zhiyuan"; stu.height = 180; stu.rest(); studentSimple sut1 = new studentSimple(); studentSimple sut2 = new studentSimple("Xi Xi", 6, "Excavator specialty"); //test instanceof System.out.println(stu instanceof studentSimple); System.out.println(stu instanceof person); System.out.println(stu instanceof Object); System.out.println(new person() instanceof studentSimple); } } class person{ String name; int height; String major; public void rest() { System.out.println("Take a break ! "); } } class Student{ String name; int height; String major; public void study() { System.out.println("Two hours of study !"); } public void rest() { System.out.println("Take a break ! "); } } //The above Student class contains many properties in the person class, which can be simplified by inheritance class studentSimple extends person { String major; //After a custom constructor is added, the default constructor will be masked and needs to be added manually. Otherwise, the default constructor cannot be called public studentSimple(String name, int height, String major) { this.name = name; this.height = height; this.major = major; } //Add the default constructor manually public studentSimple() { } public void study() { System.out.println("Two hours of study !"); } }
Select the derived class and press Ctrl+T to see the inheritance structure
override of method
By overriding the method of the parent class, the child class can replace the behavior of the parent class with its own behavior. Method rewriting is a necessary condition for polymorphism.
Method rewriting needs to meet the following three points:
1. "= =": the method name and parameter list are the same. 2. "≤": return value type and declared exception type, subclass less than or equal to parent class. 3. "≥": access permission. The subclass is greater than or equal to the parent.
package OOprogress; /* * Test override / override */ import OOprogress.testExtendAndInstanceof; public class testOverload { public static void main(String[] argd) { Horse h = new Horse(); h.run(); } } class Vehicle{ public void run() { System.out.println("run..."); } public void stop() { System.out.println("Stop it!!!"); } public person whoIsPassenger() { return new person(); } } class Horse extends Vehicle{ //run method of covering super class public void run() { System.out.println("Horseshoe flying,To be careful..."); } //studentSimple->person->Object //Rewrite the run method of superclass /*error greater than public Object whoIsPassenger() { return new Object(); } */ /*right Be equal to public person whoIsPassenger() { return new person(); } */ //The type of return value should be less than or equal to that of the parent class public studentSimple whoIsPassenger() { return new studentSimple(); } }
s