encapsulation
I definition
Packaging in the general sense can be understood as packaging.
In the object-oriented process, we understand encapsulation as hiding some information of some attributes inside the class, which does not allow external programs to access directly, but through the methods provided by the class to realize the operation and access to the hidden information.
For example, if a person asks you to get resources, you won't directly let him copy them on your computer, but you copy them to him. In this process, we can regard resources as attributes, others as other classes, and you as methods. You must pass your hand when others take you.
Benefits:
1. In this way, the data can only be accessed through the specified methods.
2. Hide the implementation details of the class to facilitate modification and implementation.
II Access modifier permission and encapsulation implementation
From the above figure, we can know that the permissions of different modifiers are different, and we will use the private access modifier to ensure the privacy of attributes in the process of encapsulation. And we need to write methods (setter s and getter s) so that other classes can get the value of the property. (this keyword plays a great role in encapsulation)
The following is the encapsulated code
public class Person { /* * Encapsulation of attributes * A person's name, gender and age are all private attributes of a person */ private String name ; //Private property, so it cannot be modified after creating objects in different classes private String sex ; private int age ; /* * setter(),getter()It is the external development interface of the object */ public String getName() { return name; } public void setName(String name) {//This method can modify the name attribute of objects in different classes this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
inherit
I Definition and overview
1. Inheritance is the technology of creating new classes based on existing classes. The definition of a new class can add new data or new functions, or use the functions of the parent class, but cannot selectively inherit the parent class.
2. In java, if inheritance exists, the inherited class is called the parent class (superclass, base class), and the inherited class is called the subclass (derived class). The subclass can inherit the properties or methods in the parent class:
3. Subclasses inherit things from the parent class: if the parent class and subclass are in the same package, they can inherit as long as they are not modified by private
4. If the parent and child classes are in different packages, they can only inherit public and protected modified
II Implementation of inheritance and several keywords
1. Extensions (used to implement inheritance)
//Class subclass extends parent class class Police extends Persom{ ······ }
2.super
Generally speaking, when creating a subclass object, the parent object will be accessed first. The execution order of Java is
Parent object attribute initialization - > parent object construction method - > child object attribute initialization - > child object construction method
So our super keyword has been implicitly used in this process. This is because if the constructor of the subclass does not display the constructor of calling the parent class, the system will call the constructor of the parent class without parameters by default. Then, if you use the super keyword to call the constructor of the parent class in the subclass, it must be in the first line of the constructor of the subclass.
Used inside an object, it can represent a parent object.
1. Access the properties of the parent class: super age
2. Method to access the parent class: super eat()
class Country { String name; void value() { name = "China" ; } } class City extends Country { String name; void value() { name = "Shanghai" ; super .value(); //Call the method of the parent class System.out.println(name); System.out.println( super .name); } public static void main(String[] args) { City c= new City(); c.value(); } }
3.final
- final modifies a variable, which is called a constant and cannot be modified at runtime.
- Final can modify a class. Once final modifies a class, the class is the final class and cannot be inherited.
- Final can also modify the method, which is the final method and does not allow overriding
About Override:
The subclass rewrites the existing method name in the parent class in the subclass
be careful:
The return value type, method name, type and number must be the same as the method inherited by the parent class, which is called method override.
polymorphic
I Definition and overview
Polymorphism refers to the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable. It is not determined during programming, but only during the running of the program, that is, a reference variable will point to the instance object of which class, and the method call issued by the reference variable is the method implemented in which class, It can only be determined during the running of the program. Because the specific class is determined only when the program is running, the reference variable can be bound to various class implementations without modifying the source program code, resulting in the change of the specific method called by the reference, that is, the specific code bound when the program is running can be changed without modifying the program code, so that the program can select multiple running states, This is polymorphism.
II Implementation conditions of polymorphism
There are three necessary conditions for Java polymorphism: inheritance, rewriting and upward transformation.
- Inheritance: there must be subclasses and parent classes with inheritance relationship in polymorphism.
- Override: a subclass redefines some methods in the parent class. When these methods are called, the subclass's methods will be called.
- Upward Transformation: in polymorphism, you need to assign the reference of the subclass to the parent object. Only in this way can the reference have the skills to call the methods of the parent class and the subclass.
Examples of upward transformation
public class Person { public void display(){ System.out.println("Play Person..."); } static void display(Person person){ person.display(); } } public class Policeman extends Person{ public static void main(String[] args) { Policeman police = new Policeman(); Person.display(police); //police is of type person. } }
III Implementation of polymorphism
1. Polymorphism based on inheritance implementation
The implementation mechanism based on inheritance is mainly reflected in the rewriting of some methods by the parent class and one or more subclasses inheriting the parent class. The rewriting of the same method by multiple subclasses can show different behaviors.
The polymorphism based on inheritance implementation can be summarized as follows: for the parent type that references a subclass, when processing the reference, it is applicable to all subclasses that inherit the parent class. Different subclass objects have different implementation of methods and different behaviors generated by executing the same action.
If the parent class is an abstract class, the child class must implement all the abstract methods in the parent class. In this way, all the child classes of the parent class must have a unified external interface, but their internal specific implementations can be different. In this way, we can use the unified interface provided by the top-level class to deal with the methods of this level.
2. Abstract class
Definition: an abstract class is an abstract class if it is decorated with the abstract keyword.
Note the following points when using abstract classes:
- Abstract classes constrain what methods a subclass must have, regardless of how the subclass implements them.
- Abstract class application scenarios:
a. In some cases, a parent class only knows what methods its subclasses should contain, but it cannot accurately know how these subclasses implement these methods (dynamic polymorphism can be realized).
b. An abstract class is abstracted from multiple classes with the same characteristics, and the abstract class is used as the template of the subclass, so as to avoid the randomness of subclass design.
3. Abstract classes define abstract methods. They only have declarations and do not need to be implemented. Abstract methods do not have a method body ending with a semicolon. Abstract methods must be decorated with the abstract keyword. For example:
public abstract class Telephone{ public abstract void call();//Abstract method. The method ends with a semicolon. It has only a declaration and does not need to be implemented }
4. Classes that contain abstract methods are abstract classes. Abstract classes can contain ordinary methods or no abstract methods. For example:
public abstract class Telephone{ //This abstract class can have no abstract methods public void message(){ System.out.println("I am an ordinary method of an abstract class") }//Abstract classes contain ordinary methods }
5. Abstract classes cannot be created directly. You can define reference variables to point to subclass objects to implement abstract methods. Take the above Telephone abstract class as an example:
public abstract class Telephone { public abstract void call();//Abstract method. The method body ends with a semicolon. It has only a declaration and does not need to be implemented public void message(){ System.out.println("Abstract class common method"); }//Abstract classes contain ordinary methods }
public class Phone extends Telephone { public void call() {//Subclasses that inherit abstract classes must override abstract methods // TODO Auto-generated method stub System.out.println("Overriding abstract class methods"); } }
public class train{ public static void main(String[] argus){ Telephone t = new Telephone();//Abstract classes cannot be created directly Telephone p = new Phone();//Define reference variables to point to subclass objects p.cell(); p.message(); } }